feat(config): Write and read config + refacto
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Matthieu 'JP' DERASSE 2022-08-05 14:20:00 +00:00
parent 2304a11465
commit 645344fa5c
Signed by: mderasse
GPG Key ID: 55141C777B16A705
8 changed files with 75 additions and 24 deletions

View File

@ -89,7 +89,10 @@ func runInitAction(cmd *cobra.Command, args []string) {
return 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) possibleAPITypes := make([]string, 0)
for _, apiType := range models.GetListOfAPITypeName() { for _, apiType := range models.GetListOfAPITypeName() {
possibleAPITypes = append(possibleAPITypes, string(apiType)) 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") log.Error("Impossible to load that API Type generator")
} }
config.ProjectType = apiTypeName
log.Debug("Check before init for the selected API type") log.Debug("Check before init for the selected API type")
err = apiType.CheckInitialize() err = apiType.CheckInitialize()
@ -113,35 +118,32 @@ func runInitAction(cmd *cobra.Command, args []string) {
return return
} }
// Initialize a default userInput
userInput := models.UserInputParams{}
// ask project directory // ask project directory
log.Info("Name of the project directory:") log.Info("Name of the project directory:")
userInput.ProjectDirectory = helpers.AlphanumericalInput() projectDirectory := helpers.AlphanumericalInput()
// ask project name // ask project name
log.Info("Name of the project:") log.Info("Name of the project:")
userInput.ProjectName = helpers.StringInput() config.ProjectName = helpers.StringInput()
// ask project description // ask project description
log.Info("Description of the project:") log.Info("Description of the project:")
userInput.ProjectDescription = helpers.StringInput() config.ProjectDescription = helpers.StringInput()
// ask project owner // ask project owner
log.Info("Mail address of the developer team:") log.Info("Mail address of the developer team:")
userInput.ProjectOwner = helpers.StringInput() config.ProjectOwner = helpers.StringInput()
// launch GetInitializeUserInput from selected api type // launch GetInitializeUserInput from selected api type
log.Debug("Get user input for the 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) { if err != nil && !errors.Is(err, errors.NotImplemented) {
log.Errorf("Fail to get all the required input. The following error happen: %s", err.Error()) log.Errorf("Fail to get all the required input. The following error happen: %s", err.Error())
return return
} }
projectDirPath := filepath.Join(currentPath, userInput.ProjectDirectory) projectDirPath := filepath.Join(currentPath, projectDirectory)
log.Debugf("Our project directory path is: %s", projectDirPath) log.Debugf("Our project directory path is: %s", projectDirPath)
@ -160,7 +162,7 @@ func runInitAction(cmd *cobra.Command, args []string) {
} }
// Move to project directory. // 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) err = os.Chdir(projectDirPath)
if err != nil { if err != nil {
@ -179,6 +181,11 @@ func runInitAction(cmd *cobra.Command, args []string) {
} }
log.Info("Creating gouick configuration file") 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") log.Info("Creating API skeleton")
} }

1
go.mod
View File

@ -13,4 +13,5 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

2
go.sum
View File

@ -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= 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/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.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=

View File

@ -7,6 +7,6 @@ import (
) )
// GetInitializeUserInput will ask user to provide information that allow initialization of a project. // 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()) return nil, errors.NotImplementedf("GetInitializeUserInput not implemented for %s", a.GetName())
} }

View File

@ -12,7 +12,7 @@ import (
) )
// GetInitializeUserInput will ask user to provide information that allow initialization of a project. // 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()) log.Debugf("Starting %s user input", a.GetName())
// Get current path // 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") log.Debug("We are not in GoPath, ask extra info")
goModulePath := helpers.StringInput() goModulePath := helpers.StringInput()
log.Info("Go Module name:") log.Info("Go Module name:")
params.GoModuleName = &goModulePath params.ModuleName = &goModulePath
} }
log.Info("Do you want to enable database models auto generation ?") log.Info("Do you want to enable database models auto generation ?")
params.DatabaseModels = helpers.YesOrNoInput() params.Features.DatabaseModels.Enabled = helpers.YesOrNoInput()
return params, nil return params, nil
} }

View File

@ -6,5 +6,5 @@ import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
type APITypeInterface interface { type APITypeInterface interface {
CheckInitialize() error CheckInitialize() error
GetName() models.APITypeName GetName() models.APITypeName
GetInitializeUserInput(params *models.UserInputParams) (*models.UserInputParams, error) GetInitializeUserInput(params *models.Config) (*models.Config, error)
} }

View File

@ -1,22 +1,61 @@
package helpers package helpers
import ( import (
"io/ioutil"
"os"
"path/filepath"
"github.com/juju/errors"
"gopkg.in/yaml.v3"
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models" "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) { 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 { func WriteConfig(path string, config *models.Config) error {
return nil
}
// WriteConfigFromUserInputParams configFilePath := filepath.Join(path, configFile)
func WriteConfigFromUserInputParams(path string, userInput *models.UserInputParams) error {
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 return nil
} }

View File

@ -5,6 +5,8 @@ type Config struct {
ProjectName string `yaml:"project_name"` ProjectName string `yaml:"project_name"`
ProjectDescription string `yaml:"project_description"` ProjectDescription string `yaml:"project_description"`
ProjectOwner string `yaml:"project_owner"` ProjectOwner string `yaml:"project_owner"`
ProjectType APITypeName `yaml:"project_type"`
ModuleName *string `yaml:"module_name"`
Features FeaturesConfig `yaml:"features"` Features FeaturesConfig `yaml:"features"`
} }