Skip to content

Commit

Permalink
fix processing of config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cspargo-apptio committed Apr 27, 2021
1 parent a3e3c83 commit 96950c0
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&clusterDir, "clusterdir", "D", "", "kr8 cluster directory")
RootCmd.PersistentFlags().StringVarP(&componentDir, "componentdir", "X", "", "kr8 component directory")
RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "log more information about what kr8 is doing")
RootCmd.PersistentFlags().BoolVar(&colorOutput, "color", false, "enable colorized output")
RootCmd.PersistentFlags().BoolVar(&colorOutput, "color", true, "enable colorized output (default). Set to false to disable")
RootCmd.PersistentFlags().StringArrayP("jpath", "J", nil, "Directories to add to jsonnet include path. Repeat arg for multiple directories")
RootCmd.PersistentFlags().StringSlice("ext-str-file", nil, "Set jsonnet extvar from file contents")
viper.BindPFlag("base", RootCmd.PersistentFlags().Lookup("base"))
viper.BindPFlag("clusterdir", RootCmd.PersistentFlags().Lookup("clusterdir"))
viper.BindPFlag("componentdir", RootCmd.PersistentFlags().Lookup("componentdir"))
viper.BindPFlag("color", RootCmd.PersistentFlags().Lookup("color"))
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -91,14 +92,19 @@ func initConfig() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: !colorOutput})

viper.SetConfigName(".kr8") // name of config file (without extension)
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.SetEnvPrefix("KR8")
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Debug().Msg("Using config file:" + viper.ConfigFileUsed())
}
colorOutput = viper.GetBool("color")
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: !colorOutput})

baseDir = viper.GetString("base")
log.Debug().Msg("Using base directory: " + baseDir)
clusterDir = viper.GetString("clusterdir")
Expand All @@ -110,9 +116,5 @@ func initConfig() {
componentDir = baseDir + "/components"
}
log.Debug().Msg("Using component directory: " + componentDir)
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Debug().Msg("Using config file:" + viper.ConfigFileUsed())
}

}

0 comments on commit 96950c0

Please sign in to comment.