Skip to content

Commit

Permalink
Merge pull request #20 from edwinvautier/feat_project-config
Browse files Browse the repository at this point in the history
Create config file when initializing new project
  • Loading branch information
Edwin Vautier authored Mar 29, 2021
2 parents a95fa2f + 1dfff30 commit 9a95e71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func initConfig() {
// Search config in home directory with name ".go-cli" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".go-cli")
viper.SetDefault("auth-module", false);
}

viper.AutomaticEnv() // read in environment variables that match
Expand All @@ -95,4 +94,4 @@ func createConfig(homeDir string) {
log.Error("Couldn't create config file : ", err)
}
initConfig()
}
}
27 changes: 27 additions & 0 deletions services/createCommand/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/edwinvautier/go-cli/services"
"github.com/edwinvautier/go-cli/services/filesystem"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

// InitProject creates the directory for a new project and all needed structure depending on the config given
Expand All @@ -26,6 +27,8 @@ func InitProject(config *config.CreateCmdConfig) error {
}
log.Info("git initialized")

createProjectConfig(workingDirectory + "/" + config.AppName, config)

if err := generateTemplates(*config); err != nil {
return err
}
Expand Down Expand Up @@ -58,3 +61,27 @@ func createProjectDir(path string) error {

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

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

viper.AddConfigPath(workdir)
viper.SetConfigName(".go-cli-config")

// Set config defaults
viper.Set("package", config.GoPackageFullPath)
viper.Set("database", config.DBMS);
viper.Set("use_docker", config.UseDocker);
viper.SetDefault("modules.auth-module", false);

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Info("Using config file : ", viper.ConfigFileUsed())
}
viper.WriteConfig()
}

0 comments on commit 9a95e71

Please sign in to comment.