Skip to content

Commit

Permalink
refacto: Rewrite logs of all CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin Vautier committed Apr 25, 2021
1 parent af3afd3 commit 438e52c
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 36 deletions.
5 changes: 2 additions & 3 deletions bundles/authenticator/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (

// GoDotEnvVariable loads a variable from the .env file
func goDotEnvVariable(key string) string {

// load .env file
err := godotenv.Load(".env")

if err != nil {
log.Error("Error loading .env file", err)
log.Error("Error loading .env file: ", err)
}

envVariable, variableExists := os.LookupEnv(key)
if !variableExists {
log.Error("Couldn't find variable : ", key)
log.Error("Couldn't find variable: ", key)
}

return envVariable
Expand Down
4 changes: 2 additions & 2 deletions bundles/authenticator/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func initRsaKeys() error {
rsa.PrivateKeyPath = goDotEnvVariable("RSA_PRIVATE_PATH")

if err := parsePublicKey(&rsa.PublicKey); err != nil {
log.Error("Couldnt parse public.pem", err)
log.Error("Couldnt parse public.pem: ", err)
return err
}

if err := parsePrivateKey(&rsa.PrivateKey); err != nil {
log.Error("Couldn't parse private.pem", err)
log.Error("Couldn't parse private.pem: ", err)
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (cqs *ConsumeQueueService) Consume(results chan<- interface{}) {
url = "amqp://user:bitnami@rabbitmq:5672"
}

log.Info(" CONSUMING QUEUE:", cqs.Queue)
log.Info("CONSUMING QUEUE: ", cqs.Queue)

cfg := Config{
URL: url,
Expand Down
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var createCmd = &cobra.Command{
config.InitCreateCmdConfig(&commandConfig)

if err := createCommand.InitProject(&commandConfig); err != nil {
log.Error("create command failed : ", err)
log.Error("project initialization failed: ", err)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var installCmd = &cobra.Command{
continue
}
if err := installCommand.InstallBundle(bundleName); err != nil {
log.Error(err)
log.Error(bundleName, " install failed: ", err)
}
}
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ var makeCmd = &cobra.Command{
switch args[0] {
case "model":
if err := makeCommand.MakeModel(args[1]); err != nil {
log.Fatal(err)
log.Fatal("make model ", args[1], " failed: ", err)
}
case "crud":
if err := makeCommand.MakeCrud(args[1]); err != nil {
log.Fatal(err)
log.Fatal("make crud for ", args[1], " failed: ", err)
}
case "fixtures":
if err := makeCommand.MakeFixtures(args[1]); err != nil {
log.Fatal(err)
log.Fatal("make fixtures for ", args[1], " failed: ", err)
}
case "tests":
if err := makeCommand.MakeTests(args[1]); err != nil {
log.Fatal(err)
log.Fatal("make tests for ", args[1], " failed: ", err)
}
default:
log.Fatal(args[0], " is not a make command!")
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func initConfig() {
} else {
home, err := homedir.Dir()
if err != nil {
log.Error("couldn't get your homedir")
log.Error("couldn't get your home directory")
}
createConfig(home)
}
Expand All @@ -91,7 +91,7 @@ func initConfig() {
func createConfig(homeDir string) {
_, err := os.Create(homeDir + "/.go-gadgeto.yaml")
if err != nil {
log.Error("Couldn't create config file : ", err)
log.Error("couldn't create config file: ", err)
}
initConfig()
}
1 change: 0 additions & 1 deletion config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type CommandConfigInterface interface {

func initBasicConfig() baseConfig {
if err := InitViper(); err != nil {
log.Error(err)
return baseConfig{}
}
workdir := filesystem.GetWorkdirOrDie()
Expand Down
6 changes: 3 additions & 3 deletions config/create_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func getGitUsername() string {
userName := services.GetGitUsername()
if userName == "" {
if err := prompt.AskGitUsername(&userName); err != nil {
log.Fatal(err)
log.Fatal("go-gadgeto needs your git username to initialize a new project")
}
viper.Set("git-username", userName)
}
Expand All @@ -47,14 +47,14 @@ func getDBMS() string {
// Get the desired DB management system
dbms := ""
if err := prompt.AskDBMS(&dbms); err != nil {
log.Fatal(err)
log.Fatal("go-gadgeto needs you to choose a database management system")
}
return dbms
}

func chooseToUseDocker() bool {
// Ask user wether to use docker or not
wantsDocker := false
wantsDocker := true
prompt.AskToUseDocker(&wantsDocker)

return wantsDocker
Expand Down
8 changes: 3 additions & 5 deletions config/make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func AddModelToConfig(newModel modelPrompt.NewModel) error {
models := viper.GetStringMap("models")
models[newModel.Name] = newModel
viper.Set("models", models)
log.Info("Using config file : ", viper.ConfigFileUsed())
viper.WriteConfig()

return nil
Expand All @@ -68,7 +67,7 @@ func AddModelToConfig(newModel modelPrompt.NewModel) error {
func InitModelConfig(config *MakeCmdConfig) error {
// Get model from config
if err := InitViper(); err != nil {
log.Fatal("couldn't read config, try again")
return errors.New("couldn't load config, try again")
}

if !IsInConfig(config.Model.Name) {
Expand All @@ -78,7 +77,7 @@ func InitModelConfig(config *MakeCmdConfig) error {
modelData := viper.GetStringMap("models." + config.Model.Name)
var model modelPrompt.NewModel
if err := mapstructure.Decode(modelData, &model); err != nil {
return errors.New("error while decoding " + config.Model.Name)
return errors.New("error while decoding " + config.Model.Name + " from config")
}
config.Model = model
configBase := initBasicConfig()
Expand Down Expand Up @@ -127,7 +126,6 @@ func InitMakeTestsCmdConfig(config *MakeCmdConfig) error {
// IsInConfig returns a boolean telling wether the modelName was found in config or not
func IsInConfig(modelName string) bool {
if err := InitViper(); err != nil {
log.Error("error when loading viper")
return false
}
modelsStructs := viper.GetStringMap("models")
Expand All @@ -145,7 +143,7 @@ func InitUpdateModelConfig(config *MakeCmdConfig) error {
modelData := viper.GetStringMap("models." + config.Model.Name)
var model modelPrompt.NewModel
if err := mapstructure.Decode(modelData, &model); err != nil {
return errors.New("error while decoding " + config.Model.Name)
return errors.New("error while decoding " + config.Model.Name + " from config")
}
config.Model = model

Expand Down
2 changes: 1 addition & 1 deletion config/update_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func updateModels() error {
var model modelPrompt.NewModel
model.Name = modelName
if err := generateModel(&model); err != nil {
log.Error("couldn't add " + modelName + "to config")
log.Error("couldn't parse " + modelName + " from files")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion services/createCommand/clean_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func CleanAllFiles(config *config.CreateCmdConfig) error {
if !config.UseDocker {
if err := removeDockerFiles(config); err != nil {
log.Error("Could'nt remove docker files", err)
log.Error("Could'nt remove docker files: ", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions services/createCommand/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func createProjectDir(path string) error {
if !filesystem.DirectoryExists(path) {
return os.Mkdir(path, os.ModePerm)
}
log.Warn("A directory with this name already exists.")
log.Warn("a directory with this name already exists.")

wantsOverride := false
prompt.AskToOverride(&wantsOverride)
Expand All @@ -50,13 +50,13 @@ func createProjectDir(path string) error {
return filesystem.RemoveDirAndFiles(path)
}

return errors.New("Couldn't create project directory")
return errors.New("couldn't create project directory")
}

func createProjectConfig(workdir string, config *config.CreateCmdConfig) {
_, err := os.Create(workdir + "/.go-gadgeto-config.yaml")
if err != nil {
log.Error("Couldn't create project config : ", err)
log.Error("couldn't create project config : ", err)
}

viper.AddConfigPath(workdir)
Expand All @@ -72,7 +72,7 @@ func createProjectConfig(workdir string, config *config.CreateCmdConfig) {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Info("Using config file : ", viper.ConfigFileUsed())
log.Info("using config file : ", viper.ConfigFileUsed())
}
viper.WriteConfig()
}
5 changes: 2 additions & 3 deletions services/files/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Generate(path string, name string, outputName string, commandConfig config.
// Get template content as string
templateString, err := commandConfig.GetBox().FindString(path + name)
if err != nil {
log.Error(err)
log.WithField("path", path+name).Error("couldn't get template")
return
}

Expand All @@ -24,7 +24,7 @@ func Generate(path string, name string, outputName string, commandConfig config.

err = executeTemplate(commandConfig, outputName, commandConfig.GetProjectPath()+"/"+path, templateString)
if err != nil {
log.Error("oups", err)
log.WithField("path", path+outputName).Error("couldn't create file")
return
}
}
Expand All @@ -33,7 +33,6 @@ func executeTemplate(config config.CommandConfigInterface, outputName string, pa
// Create the file
file, err := os.Create(path + outputName)
if err != nil {
log.Error(err)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion services/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func RemoveSingle(path string) error {
func GetWorkdirOrDie() string {
workdir, err := os.Getwd()
if err != nil {
log.Fatal("Couldn't get workdir")
log.Fatal("couldn't get workdir")
}

return workdir
Expand Down
3 changes: 1 addition & 2 deletions services/filesystem/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func GetModelsList() []string {
workdir := GetWorkdirOrDie()
files, err := ioutil.ReadDir(workdir + "/api/models")
if err != nil {
log.Fatal(err)
log.Fatal("couldn't read models directory: ", err)
}

models := make([]string, 0)
Expand All @@ -37,7 +37,6 @@ func GetFixturesModelsList() []string {
for _, file := range files {
name := helpers.UpperCaseFirstChar(strings.Split(file.Name(), ".go")[0])
if !strings.Contains(name, "json") && !strings.Contains(name, "Fixture") {
log.Info(name)
models = append(models, name)
}
}
Expand Down
1 change: 0 additions & 1 deletion services/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func getFromGit() string {
cmd := exec.Command("git", "config", "user.name")
stdout, err := cmd.Output()
if err != nil {
log.Error(err)
return ""
}

Expand Down

0 comments on commit 438e52c

Please sign in to comment.