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
8 changed files with 75 additions and 24 deletions

View File

@ -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())
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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"`
}