From 645344fa5c29d001531762e03c767b53bb27d6fc Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Fri, 5 Aug 2022 14:20:00 +0000 Subject: [PATCH] feat(config): Write and read config + refacto --- cmd/init.go | 29 ++++++---- go.mod | 1 + go.sum | 2 + helpers/api_types/base/get_user_input.go | 2 +- .../api_types/go_swagger/get_user_input.go | 6 +- helpers/api_types/interface.go | 2 +- helpers/config.go | 55 ++++++++++++++++--- helpers/models/config.go | 2 + 8 files changed, 75 insertions(+), 24 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 79c7c5d..4480d8f 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -89,7 +89,10 @@ func runInitAction(cmd *cobra.Command, args []string) { return } - // ask which API type we want to use + // let's start to work on project configuration + config := &models.Config{} + + // ask which API type we want to use. possibleAPITypes := make([]string, 0) for _, apiType := range models.GetListOfAPITypeName() { possibleAPITypes = append(possibleAPITypes, string(apiType)) @@ -105,6 +108,8 @@ func runInitAction(cmd *cobra.Command, args []string) { log.Error("Impossible to load that API Type generator") } + config.ProjectType = apiTypeName + log.Debug("Check before init for the selected API type") err = apiType.CheckInitialize() @@ -113,35 +118,32 @@ func runInitAction(cmd *cobra.Command, args []string) { return } - // Initialize a default userInput - userInput := models.UserInputParams{} - // ask project directory log.Info("Name of the project directory:") - userInput.ProjectDirectory = helpers.AlphanumericalInput() + projectDirectory := helpers.AlphanumericalInput() // ask project name log.Info("Name of the project:") - userInput.ProjectName = helpers.StringInput() + config.ProjectName = helpers.StringInput() // ask project description log.Info("Description of the project:") - userInput.ProjectDescription = helpers.StringInput() + config.ProjectDescription = helpers.StringInput() // ask project owner log.Info("Mail address of the developer team:") - userInput.ProjectOwner = helpers.StringInput() + config.ProjectOwner = helpers.StringInput() // launch GetInitializeUserInput from selected api type log.Debug("Get user input for the selected API type") - _, err = apiType.GetInitializeUserInput(&userInput) + config, err = apiType.GetInitializeUserInput(config) if err != nil && !errors.Is(err, errors.NotImplemented) { log.Errorf("Fail to get all the required input. The following error happen: %s", err.Error()) return } - projectDirPath := filepath.Join(currentPath, userInput.ProjectDirectory) + projectDirPath := filepath.Join(currentPath, projectDirectory) log.Debugf("Our project directory path is: %s", projectDirPath) @@ -160,7 +162,7 @@ func runInitAction(cmd *cobra.Command, args []string) { } // Move to project directory. - log.Infof("Moving to the project directory: %s", userInput.ProjectDirectory) + log.Infof("Moving to the project directory: %s", projectDirectory) err = os.Chdir(projectDirPath) if err != nil { @@ -179,6 +181,11 @@ func runInitAction(cmd *cobra.Command, args []string) { } log.Info("Creating gouick configuration file") + err = helpers.WriteConfig(projectDirPath, config) + if err != nil { + log.Errorf("Fail to write configuration. The following error happen: %s", err.Error()) + return + } log.Info("Creating API skeleton") } diff --git a/go.mod b/go.mod index 40bcb97..0677290 100644 --- a/go.mod +++ b/go.mod @@ -13,4 +13,5 @@ require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f4fa194..3e3fb82 100644 --- a/go.sum +++ b/go.sum @@ -26,3 +26,5 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JC golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helpers/api_types/base/get_user_input.go b/helpers/api_types/base/get_user_input.go index 0676e18..e726b34 100644 --- a/helpers/api_types/base/get_user_input.go +++ b/helpers/api_types/base/get_user_input.go @@ -7,6 +7,6 @@ import ( ) // GetInitializeUserInput will ask user to provide information that allow initialization of a project. -func (a APIType) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) { +func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config, error) { return nil, errors.NotImplementedf("GetInitializeUserInput not implemented for %s", a.GetName()) } diff --git a/helpers/api_types/go_swagger/get_user_input.go b/helpers/api_types/go_swagger/get_user_input.go index 06894f4..fbb3fc1 100644 --- a/helpers/api_types/go_swagger/get_user_input.go +++ b/helpers/api_types/go_swagger/get_user_input.go @@ -12,7 +12,7 @@ import ( ) // GetInitializeUserInput will ask user to provide information that allow initialization of a project. -func (a APIType) GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) { +func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config, error) { log.Debugf("Starting %s user input", a.GetName()) // Get current path @@ -33,11 +33,11 @@ func (a APIType) GetInitializeUserInput(params *models.UserInputParams) (*models log.Debug("We are not in GoPath, ask extra info") goModulePath := helpers.StringInput() log.Info("Go Module name:") - params.GoModuleName = &goModulePath + params.ModuleName = &goModulePath } log.Info("Do you want to enable database models auto generation ?") - params.DatabaseModels = helpers.YesOrNoInput() + params.Features.DatabaseModels.Enabled = helpers.YesOrNoInput() return params, nil } diff --git a/helpers/api_types/interface.go b/helpers/api_types/interface.go index 75b3cc1..8c5f361 100644 --- a/helpers/api_types/interface.go +++ b/helpers/api_types/interface.go @@ -6,5 +6,5 @@ import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models" type APITypeInterface interface { CheckInitialize() error GetName() models.APITypeName - GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) + GetInitializeUserInput(params *models.Config) (*models.Config, error) } diff --git a/helpers/config.go b/helpers/config.go index 420ad40..ce1847c 100644 --- a/helpers/config.go +++ b/helpers/config.go @@ -1,22 +1,61 @@ package helpers import ( + "io/ioutil" + "os" + "path/filepath" + + "github.com/juju/errors" + "gopkg.in/yaml.v3" + "git.home.m-and-m.ovh/mderasse/gouick/helpers/models" + + log "github.com/sirupsen/logrus" ) -const configFile = ".gouick.yaml" +const configFile = ".gouick.yml" -// ReadConfig will search the config file in the given path, read it and return a Config object +// ReadConfig will search the config file in the given path, read it and return a Config object. func ReadConfig(path string) (*models.Config, error) { - return nil, nil + + configFilePath := filepath.Join(path, configFile) + + log.Debugf("Opening configuration file in %s", configFilePath) + + //nolint:gosec // we did compute the file path + f, err := os.ReadFile(configFilePath) + if err != nil { + return nil, errors.Trace(err) + } + + var config models.Config + + log.Debug("Unmarshal config file") + if err := yaml.Unmarshal(f, &config); err != nil { + return nil, errors.Trace(err) + } + + return &config, nil } -// WriteConfig will write the new or updated config +// WriteConfig will write the new or updated config. func WriteConfig(path string, config *models.Config) error { - return nil -} -// WriteConfigFromUserInputParams -func WriteConfigFromUserInputParams(path string, userInput *models.UserInputParams) error { + configFilePath := filepath.Join(path, configFile) + + log.Debug("Marshal config into YAML Format") + + data, err := yaml.Marshal(config) + if err != nil { + return errors.Trace(err) + } + + log.Debugf("Writing config to %s", configFilePath) + + err = ioutil.WriteFile(configFilePath, data, 0600) + if err != nil { + return errors.Trace(err) + } + return nil } diff --git a/helpers/models/config.go b/helpers/models/config.go index 1b52fd0..fd34ee7 100644 --- a/helpers/models/config.go +++ b/helpers/models/config.go @@ -5,6 +5,8 @@ type Config struct { ProjectName string `yaml:"project_name"` ProjectDescription string `yaml:"project_description"` ProjectOwner string `yaml:"project_owner"` + ProjectType APITypeName `yaml:"project_type"` + ModuleName *string `yaml:"module_name"` Features FeaturesConfig `yaml:"features"` }