From 6268aad2aca30ee8bb16190875204ec9d08e8551 Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Sun, 18 Sep 2022 23:58:06 +0000 Subject: [PATCH] fix(go-swagger): Reorganize go swagger directory --- .../go_swagger/create_project_skeleton.go | 2 +- .../go_swagger/delete_temp_api_yaml.go | 38 ------------------ .../api_types/go_swagger/generate_makefile.go | 4 +- .../{execute_go_swagger.go => go_swagger.go} | 0 helpers/api_types/go_swagger/golang.go | 27 +++++++++++++ .../go_swagger/initialize_go_module.go | 39 ------------------- .../{generate_api_yamls.go => yaml.go} | 29 +++++++++++++- 7 files changed, 58 insertions(+), 81 deletions(-) delete mode 100644 helpers/api_types/go_swagger/delete_temp_api_yaml.go rename helpers/api_types/go_swagger/{execute_go_swagger.go => go_swagger.go} (100%) delete mode 100644 helpers/api_types/go_swagger/initialize_go_module.go rename helpers/api_types/go_swagger/{generate_api_yamls.go => yaml.go} (70%) diff --git a/helpers/api_types/go_swagger/create_project_skeleton.go b/helpers/api_types/go_swagger/create_project_skeleton.go index 699da44..8368108 100644 --- a/helpers/api_types/go_swagger/create_project_skeleton.go +++ b/helpers/api_types/go_swagger/create_project_skeleton.go @@ -20,7 +20,7 @@ func (a APIType) CreateProjectSkeleton(path string, config *models.Config) error } // Generate Makefile - err = a.generateMakefile(path, config) + err = a.GenerateMakefile(path, config) if err != nil { log.Error("Fail to generate Makefile") return errors.Trace(err) diff --git a/helpers/api_types/go_swagger/delete_temp_api_yaml.go b/helpers/api_types/go_swagger/delete_temp_api_yaml.go deleted file mode 100644 index c7a303e..0000000 --- a/helpers/api_types/go_swagger/delete_temp_api_yaml.go +++ /dev/null @@ -1,38 +0,0 @@ -package go_swagger - -import ( - "os" - "path/filepath" - - "github.com/juju/errors" - - "git.home.m-and-m.ovh/mderasse/gouick/helpers" - - log "github.com/sirupsen/logrus" -) - -// deleteTempApiYaml will delete the temporary yaml api file generated by go-swagger. -func (a APIType) deleteTempApiYaml(path string) error { - log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) - - tempFilePath := filepath.Join(path, mergeYamlFileName) - - // delete - exist, err := helpers.FileExists(tempFilePath) - if err != nil { - return errors.Trace(err) - } - - if !exist { - log.Debug("File doesn't exist already") - return nil - } - - err = os.Remove(tempFilePath) - if err != nil { - log.Errorf("fail to delete file in %s", tempFilePath) - return errors.Trace(err) - } - - return nil -} diff --git a/helpers/api_types/go_swagger/generate_makefile.go b/helpers/api_types/go_swagger/generate_makefile.go index e0d58c2..905cbe4 100644 --- a/helpers/api_types/go_swagger/generate_makefile.go +++ b/helpers/api_types/go_swagger/generate_makefile.go @@ -16,9 +16,9 @@ type makefileTemplate struct { AppNameKebabCase string } -// generateMakefile will generate makefile based on the given config. +// GenerateMakefile will generate makefile based on the given config. // Launched only at project init or on force. -func (a APIType) generateMakefile(path string, config *models.Config) error { +func (a APIType) GenerateMakefile(path string, config *models.Config) error { log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) templatePath := filepath.Join(templateDirectory, "Makefile.tmpl") diff --git a/helpers/api_types/go_swagger/execute_go_swagger.go b/helpers/api_types/go_swagger/go_swagger.go similarity index 100% rename from helpers/api_types/go_swagger/execute_go_swagger.go rename to helpers/api_types/go_swagger/go_swagger.go diff --git a/helpers/api_types/go_swagger/golang.go b/helpers/api_types/go_swagger/golang.go index e041a0e..16eb285 100644 --- a/helpers/api_types/go_swagger/golang.go +++ b/helpers/api_types/go_swagger/golang.go @@ -7,10 +7,37 @@ import ( "github.com/juju/errors" "git.home.m-and-m.ovh/mderasse/gouick/helpers" + "git.home.m-and-m.ovh/mderasse/gouick/models" log "github.com/sirupsen/logrus" ) +// initializeGoModule will launch a go mod init. +func (a APIType) initializeGoModule(path string, config *models.Config) error { + log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) + + if err := os.Chdir(path); err != nil { + log.Error("Fail to move to the path") + return errors.Trace(err) + } + + modulePath := "" + if config.ModulePath != nil { + modulePath = *config.ModulePath + } + + cmd := exec.Command("go", "mod", "init", modulePath) + output, err := cmd.CombinedOutput() + if err != nil { + log.Errorf("fail to execute go mod init %s", modulePath) + return errors.Trace(err) + } + + log.Debugf("go mod init %s returned the following output: %s", modulePath, string(output)) + + return nil +} + // downloadGoDeps will execute go get ./... in the give repository. func (a APIType) downloadGoDeps(path string) error { log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) diff --git a/helpers/api_types/go_swagger/initialize_go_module.go b/helpers/api_types/go_swagger/initialize_go_module.go deleted file mode 100644 index e73488a..0000000 --- a/helpers/api_types/go_swagger/initialize_go_module.go +++ /dev/null @@ -1,39 +0,0 @@ -package go_swagger - -import ( - "os" - "os/exec" - - "github.com/juju/errors" - - "git.home.m-and-m.ovh/mderasse/gouick/helpers" - "git.home.m-and-m.ovh/mderasse/gouick/models" - - log "github.com/sirupsen/logrus" -) - -// initializeGoModule will launch a go mod init. -func (a APIType) initializeGoModule(path string, config *models.Config) error { - log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) - - if err := os.Chdir(path); err != nil { - log.Error("Fail to move to the path") - return errors.Trace(err) - } - - modulePath := "" - if config.ModulePath != nil { - modulePath = *config.ModulePath - } - - cmd := exec.Command("go", "mod", "init", modulePath) - output, err := cmd.CombinedOutput() - if err != nil { - log.Errorf("fail to execute go mod init %s", modulePath) - return errors.Trace(err) - } - - log.Debugf("go mod init %s returned the following output: %s", modulePath, string(output)) - - return nil -} diff --git a/helpers/api_types/go_swagger/generate_api_yamls.go b/helpers/api_types/go_swagger/yaml.go similarity index 70% rename from helpers/api_types/go_swagger/generate_api_yamls.go rename to helpers/api_types/go_swagger/yaml.go index 91d32b1..e18e0c1 100644 --- a/helpers/api_types/go_swagger/generate_api_yamls.go +++ b/helpers/api_types/go_swagger/yaml.go @@ -1,6 +1,7 @@ package go_swagger import ( + "os" "path/filepath" "github.com/juju/errors" @@ -23,7 +24,7 @@ type apiYamlContact struct { URL string } -// generateAPIYamls will generate a readme based on the given config. +// generateAPIYamls will generate the main api yaml file based on the given config. // Launched only at project init. func (a APIType) generateAPIYamls(path string, config *models.Config) error { log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) @@ -68,3 +69,29 @@ func (a APIType) generateAPIYamls(path string, config *models.Config) error { return nil } + +// deleteTempApiYaml will delete the temporary yaml api file generated by go-swagger. +func (a APIType) deleteTempApiYaml(path string) error { + log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) + + tempFilePath := filepath.Join(path, mergeYamlFileName) + + // delete + exist, err := helpers.FileExists(tempFilePath) + if err != nil { + return errors.Trace(err) + } + + if !exist { + log.Debug("File doesn't exist already") + return nil + } + + err = os.Remove(tempFilePath) + if err != nil { + log.Errorf("fail to delete file in %s", tempFilePath) + return errors.Trace(err) + } + + return nil +}