Skip to content

Commit

Permalink
refactor: move parts of config init into its dedicated function
Browse files Browse the repository at this point in the history
Parts of config initialization were done in init and not in initConfig
function.
  • Loading branch information
nikoksr committed Jun 1, 2020
1 parent cbd31f4 commit 97e952d
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cmd/proji/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,25 @@ func init() {
projiEnv = &env{Svc: nil, ConfigFolderPath: "", ExcludedPaths: make([]string, 0), Version: "0.20.0"}
}

if len(os.Args) > 1 && os.Args[1] != "init" && os.Args[1] != "version" && os.Args[1] != "help" {
cobra.OnInitialize(initConfig, initStorageService)
}
}

func initConfig() {
// Set platform specific config path
var err error
projiEnv.ConfigFolderPath, err = config.GetBaseConfigPath()
if err != nil {
log.Fatalf("Error: %v\n", err)
}
viper.AddConfigPath(projiEnv.ConfigFolderPath)

viper.AddConfigPath(projiEnv.UserConfigPath)
// Config name
viper.SetConfigName("config")
viper.AutomaticEnv()

if len(os.Args) > 1 && os.Args[1] != "init" && os.Args[1] != "version" && os.Args[1] != "help" {
cobra.OnInitialize(initConfig, initStorageService)
}
}

func initConfig() {
err := viper.ReadInConfig()
// Read in config
err = viper.ReadInConfig()
if err != nil {
log.Fatalf("Error: %v\n\nTry and execute: proji init\n", err)
}
Expand Down

0 comments on commit 97e952d

Please sign in to comment.