@@ -19,7 +19,6 @@ package cmd
1919import (
2020 "flag"
2121 "fmt"
22- "os"
2322
2423 "github.com/mitchellh/go-homedir"
2524 "github.com/spf13/cobra"
@@ -33,10 +32,12 @@ var MGBanner string = "mg " + MGVersion
3332
3433// @todo: This should be moved to it's own package to avoid cyclic dependencies since both cmd and modules package use them.
3534var (
36- Namespace string
37- OName string // Flag for overriding application name.
38- Config string // Viper config override
39- Verbose bool = false
35+ Namespace string
36+ OName string // Flag for overriding application name.
37+ ConfigPath string // Viper config override
38+ ConfigFile string = "config"
39+ ConfigFormat string = "yaml"
40+ Verbose bool = false
4041 // Output flag, makes mg output generated kubernetes resources in json or yaml.
4142 Output bool = false
4243 Version string
@@ -77,28 +78,29 @@ datastructure and help you generate kubernetes primitives`,
7778}
7879
7980func init () {
80- RootCmd .PersistentFlags ().StringVar (& Config , "config" , "" , "config file (default is $HOME/.config/mg/mg .yaml)" )
81+ RootCmd .PersistentFlags ().StringVar (& ConfigPath , "config" , "" , "config file (default is $HOME/.config/mg/config .yaml)" )
8182 pflag .CommandLine .AddGoFlagSet (flag .CommandLine )
8283 cobra .OnInitialize (initConfig )
8384}
8485
8586func initConfig () {
86- viper .SetConfigType ("yaml" )
8787
8888 home , err := homedir .Dir ()
8989 if err != nil {
9090 fmt .Println (err )
91- os . Exit ( 1 )
91+ log . Fatal ( err )
9292 }
9393
94- if Config != "" {
95- fmt . Printf ( "Using configfile: %v %v" , os . Stdout , Config )
96- viper .SetConfigFile (Config )
94+ viper . SetConfigType ( "yaml" )
95+ if len ( ConfigPath ) > 0 {
96+ viper .SetConfigFile (ConfigPath )
9797 } else {
98+ ConfigPath = home + "/.config/mg/"
9899 //fmt.Println(os.Stderr, "Using default config file: ~/.config/mg/config.yaml")
99- viper .AddConfigPath (home + "/.config/mg/" )
100+ viper .AddConfigPath (ConfigPath )
100101 viper .SetConfigName ("config" )
101102 }
103+ log .Infof ("Using configfile: %v" , ConfigPath )
102104
103105 viper .AutomaticEnv ()
104106 if err := viper .ReadInConfig (); err != nil {
0 commit comments