feat(go-swagger): Allow generate launcher makefile and readme. Refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE
2022-09-20 21:49:37 +00:00
parent 6268aad2ac
commit cda86019bb
15 changed files with 331 additions and 34 deletions

View File

@ -6,8 +6,13 @@ package cmd
import (
"fmt"
"os"
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
"git.home.m-and-m.ovh/mderasse/gouick/models"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
// generateCmd represents the generate command.
@ -24,3 +29,48 @@ You can also generate files for each 'modules' by using the command bellows`,
func init() {
rootCmd.AddCommand(generateCmd)
}
func loadProjectAndConfig() (string, *models.Config, error) {
log.Debugf("Checking dependencies")
if !checkDependencies() {
return "", nil, fmt.Errorf("failed to check dependencies")
}
// Get current path
currentPath, err := os.Getwd()
if err != nil {
log.Errorf("Fail to get current path. The following error happen: %s", err.Error())
return "", nil, err
}
log.Debugf("Working in directory: %s", currentPath)
// Check if we are in a gouick project
log.Debug("Checking if we are in a gouick project")
isGouickProject, err := helpers.IsGouickProject(currentPath)
if err != nil {
log.Errorf("Fail to check if we are in a gouick project. The following error happen: %s", err.Error())
return "", nil, err
}
if !isGouickProject {
log.Error("That command need to be executed in a Gouick Project Directory")
return "", nil, fmt.Errorf("that command need to be executed in a gouick project directory")
}
// Reading project configuration
log.Debug("Reading project configuration")
config, err := helpers.ReadConfig(currentPath)
if err != nil {
log.Errorf("Failed to read configuration file. The following error happen: %s", err.Error())
return "", nil, err
}
log.Debugf("Using api type : %s", string(config.ProjectType))
return currentPath, config, nil
}

View File

@ -5,20 +5,43 @@ Copyright © 2022 Matthieu Derasse <git@derasse.fr>
package cmd
import (
"fmt"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
// launcherCmd represents the launcher command.
var launcherCmd = &cobra.Command{
Use: "launcher",
Short: "Generate Launcher Bash script",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("launcher called")
},
Short: "Generate Launcher entrypoint bash script",
Run: runGenerateLauncherAction,
}
func init() {
generateCmd.AddCommand(launcherCmd)
}
// runGenerateLauncherAction will generate the entrypoint for the current project.
func runGenerateLauncherAction(cmd *cobra.Command, args []string) {
log.Debugf("Starting command GenerateLauncher")
currentPath, config, err := loadProjectAndConfig()
if err != nil {
return
}
apiType, err := api_types.GetAPIType(config.ProjectType)
if err != nil {
log.Error("Impossible to load that API Type generator")
return
}
// Generate Launcher
log.Info("Generating Launcher")
err = apiType.GenerateLauncher(currentPath, config)
if err != nil {
log.Errorf("Fail to generate Launcher. The following error happen: %s", err.Error())
return
}
}

View File

@ -5,20 +5,43 @@ Copyright © 2022 Matthieu Derasse <git@derasse.fr>
package cmd
import (
"fmt"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
// makefileCmd represents the makefile command.
var makefileCmd = &cobra.Command{
Use: "makefile",
Short: "Generate Makefile",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("makefile called")
},
Run: runGenerateMakefileAction,
}
func init() {
generateCmd.AddCommand(makefileCmd)
}
// runGenerateMakefileAction will generate the Makefile for the current project.
func runGenerateMakefileAction(cmd *cobra.Command, args []string) {
log.Debugf("Starting command GenerateMakefile")
currentPath, config, err := loadProjectAndConfig()
if err != nil {
return
}
apiType, err := api_types.GetAPIType(config.ProjectType)
if err != nil {
log.Error("Impossible to load that API Type generator")
return
}
// Generate Makefile
log.Info("Generating Makefile")
err = apiType.GenerateMakefile(currentPath, config)
if err != nil {
log.Errorf("Fail to generate Makefile. The following error happen: %s", err.Error())
return
}
}

46
cmd/generateReadme.go Normal file
View File

@ -0,0 +1,46 @@
/*
Copyright © 2022 Matthieu Derasse <git@derasse.fr>
*/
package cmd
import (
"git.home.m-and-m.ovh/mderasse/gouick/helpers/api_types"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
// readmeCmd represents the readme command.
var readmeCmd = &cobra.Command{
Use: "readme",
Short: "Generate Readme",
Run: runGenerateReadmeAction,
}
func init() {
generateCmd.AddCommand(readmeCmd)
}
// runGenerateReadmeAction will generate the Readme for the current project.
func runGenerateReadmeAction(cmd *cobra.Command, args []string) {
log.Debugf("Starting command GenerateReadme")
currentPath, config, err := loadProjectAndConfig()
if err != nil {
return
}
apiType, err := api_types.GetAPIType(config.ProjectType)
if err != nil {
log.Error("Impossible to load that API Type generator")
return
}
// Generate ReadMe
log.Info("Generating ReadMe")
err = apiType.GenerateReadme(currentPath, config)
if err != nil {
log.Errorf("Fail to generate ReadMe. The following error happen: %s", err.Error())
return
}
}

View File

@ -39,11 +39,7 @@ func init() {
rootCmd.AddCommand(initCmd)
}
// runInitAction
// Steps:
// 1 - Check that we have the dependencies.
// 2 - Check that we are not in project directory.
// 3 - Ask info to the user.
// runInitAction will init a project.
func runInitAction(cmd *cobra.Command, args []string) {
log.Debugf("Starting command Init")
@ -107,6 +103,7 @@ func runInitAction(cmd *cobra.Command, args []string) {
apiType, err := api_types.GetAPIType(apiTypeName)
if err != nil {
log.Error("Impossible to load that API Type generator")
return
}
config.ProjectType = apiTypeName
@ -196,6 +193,23 @@ func runInitAction(cmd *cobra.Command, args []string) {
return
}
// Generate Makefile
log.Info("Generating Makefile")
err = apiType.GenerateMakefile(projectDirPath, config)
if err != nil {
log.Errorf("Fail to generate Makefile. The following error happen: %s", err.Error())
return
}
// Generate Readme
log.Info("Generating Readme")
err = apiType.GenerateReadme(projectDirPath, config)
if err != nil {
log.Errorf("Fail to generate Readme.md. The following error happen: %s", err.Error())
return
}
// Create API Skeleton
log.Info("Creating API skeleton")
err = apiType.CreateProjectSkeleton(projectDirPath, config)
if err != nil {