Skip to content

Commit

Permalink
Fix config path overriding from --config flag #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ttauveron committed Jul 9, 2019
1 parent 35f3658 commit c903ab0
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,38 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {

// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".git-follow-up" (without extension).
configPath = home + "/.git-follow-up"
gitPath = configPath + "/git/"

// Create repositories folder if not exists
_ = os.MkdirAll(configPath+"/git", 0700)

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".git-follow-up" (without extension).
configPath = home + "/.git-follow-up"
gitPath = configPath + "/git/"
viper.AddConfigPath(configPath)
viper.SetConfigName("config")
}

// Create repositories folder if not exists
_ = os.MkdirAll(configPath+"/git", 0700)

//If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
err = viper.Unmarshal(&config)
for i := 0; i < len(config.Repositories); i++ {
config.Repositories[i].LocalPath = gitPath + config.Repositories[i].Name
}
//If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
err = viper.Unmarshal(&config)
for i := 0; i < len(config.Repositories); i++ {
config.Repositories[i].LocalPath = gitPath + config.Repositories[i].Name
}

if err != nil {
panic("Unable to unmarshal config")
}
if err != nil {
panic("Unable to unmarshal config")
}
}

Expand Down

0 comments on commit c903ab0

Please sign in to comment.