From 1dfff30f5eee70104662a2f0623666e4ad668369 Mon Sep 17 00:00:00 2001 From: edwinvautier Date: Mon, 29 Mar 2021 14:08:09 +0200 Subject: [PATCH] feat(config): Create a new config file in a new project when created. --- cmd/root.go | 3 +-- services/createCommand/init_project.go | 27 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7a1757a..75aa3de 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -95,4 +94,4 @@ func createConfig(homeDir string) { log.Error("Couldn't create config file : ", err) } initConfig() -} +} \ No newline at end of file diff --git a/services/createCommand/init_project.go b/services/createCommand/init_project.go index 23a088f..b32eb2b 100644 --- a/services/createCommand/init_project.go +++ b/services/createCommand/init_project.go @@ -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 @@ -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 } @@ -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() +} \ No newline at end of file