From d997c0035d62633b4f1d68bcebe7ee4f6bcab46f Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Sun, 11 Dec 2022 13:43:49 +0000 Subject: [PATCH] feat(api): Implement API generator with stratoscale for go swagger --- cmd/generateAPI.go | 27 ++++++++++++++++- helpers/api_types/base/generate_api.go | 13 ++++++++ helpers/api_types/go_swagger/generate_api.go | 31 ++++++++++++++++++++ helpers/api_types/go_swagger/go_swagger.go | 1 + helpers/api_types/interface.go | 1 + 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 helpers/api_types/base/generate_api.go create mode 100644 helpers/api_types/go_swagger/generate_api.go diff --git a/cmd/generateAPI.go b/cmd/generateAPI.go index aae98cf..7e87461 100644 --- a/cmd/generateAPI.go +++ b/cmd/generateAPI.go @@ -5,12 +5,16 @@ package cmd import ( "github.com/spf13/cobra" + + "git.dev.m-and-m.ovh/mderasse/gouick/helpers/api_types" + + log "github.com/sirupsen/logrus" ) // apiCmd represents the api command. var apiCmd = &cobra.Command{ Use: "api", - Short: "Generate Go-Swagger Files and Controllers", + Short: "Generate API Files and Controllers", Run: runGenerateAPI, } @@ -20,4 +24,25 @@ func init() { // runGenerateAPI will generate api files such as controllers, server, unit test, ... for the current project. func runGenerateAPI(cmd *cobra.Command, args []string) { + + log.Debugf("Starting command GenerateAPI") + + 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 Dockerfile + log.Info("Generating API") + err = apiType.GenerateAPI(currentPath, config) + if err != nil { + log.Errorf("Fail to generate API Files. The following error happen: %s", err.Error()) + return + } } diff --git a/helpers/api_types/base/generate_api.go b/helpers/api_types/base/generate_api.go new file mode 100644 index 0000000..85c4628 --- /dev/null +++ b/helpers/api_types/base/generate_api.go @@ -0,0 +1,13 @@ +package base + +import ( + "github.com/juju/errors" + + "git.dev.m-and-m.ovh/mderasse/gouick/helpers" + "git.dev.m-and-m.ovh/mderasse/gouick/models" +) + +// GenerateAPI will generate api files such as server, controllers, test, .... +func (a APIType) GenerateAPI(path string, config *models.Config) error { + return errors.NotImplementedf("%s not implemented for %s", helpers.GetCurrentFuncName(), a.GetName()) +} diff --git a/helpers/api_types/go_swagger/generate_api.go b/helpers/api_types/go_swagger/generate_api.go new file mode 100644 index 0000000..137726b --- /dev/null +++ b/helpers/api_types/go_swagger/generate_api.go @@ -0,0 +1,31 @@ +package go_swagger + +import ( + "github.com/juju/errors" + + "git.dev.m-and-m.ovh/mderasse/gouick/helpers" + "git.dev.m-and-m.ovh/mderasse/gouick/models" + + log "github.com/sirupsen/logrus" +) + +// GenerateAPI will generate api files such as server, controllers, test, .... +func (a APIType) GenerateAPI(path string, config *models.Config) error { + log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) + + // Launch Go Swagger + err := a.executeGoSwagger(path) + if err != nil { + log.Error("Fail to Execute Go Swagger") + return errors.Trace(err) + } + + // Delete api yaml temporary file + err = a.deleteTempApiYaml(path) + if err != nil { + log.Error("Fail to delete api yaml temporary file") + return errors.Trace(err) + } + + return nil +} diff --git a/helpers/api_types/go_swagger/go_swagger.go b/helpers/api_types/go_swagger/go_swagger.go index 0615e9f..0bd67cd 100644 --- a/helpers/api_types/go_swagger/go_swagger.go +++ b/helpers/api_types/go_swagger/go_swagger.go @@ -95,6 +95,7 @@ func (a APIType) goSwaggerGenerate(path string) error { generateCmd.Server.Models.ModelPackage = "models" generateCmd.Server.Operations.APIPackage = "operations" generateCmd.Server.Shared.StrictResponders = true + generateCmd.Server.Shared.Template = "stratoscale" return generateCmd.Server.Execute(nil) } diff --git a/helpers/api_types/interface.go b/helpers/api_types/interface.go index 93a36b7..b73d56b 100644 --- a/helpers/api_types/interface.go +++ b/helpers/api_types/interface.go @@ -6,6 +6,7 @@ import "git.dev.m-and-m.ovh/mderasse/gouick/models" type APITypeInterface interface { CheckInitialize() error CreateProjectSkeleton(path string, config *models.Config) error + GenerateAPI(path string, config *models.Config) error GenerateDockerfile(path string, config *models.Config) error GenerateLauncher(path string, config *models.Config) error GenerateMakefile(path string, config *models.Config) error