-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
66 lines (56 loc) · 1.42 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cmd
import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"log"
"os"
"path/filepath"
)
var (
// Used for flags.
cfgFile string
rootCmd = &cobra.Command{
Use: "goose",
Short: "A utility program",
Long: `A utility program`,
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.goose/goose-config.yaml)")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
_ = viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
for _, k := range viper.AllKeys() {
//zap.S().Debugw("config",k, viper.GetString(k))
log.Println("config", k, viper.GetString(k))
}
}
func er(msg interface{}) {
zap.S().Fatalw("Error:", msg)
os.Exit(1)
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
er(err)
}
// Search config in $HOME/.goose dir with name "goose-config" (without extension).
viper.AddConfigPath(filepath.Join(home, ".goose"))
viper.SetConfigName("goose-config")
}
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
log.Println("Using config file:", viper.ConfigFileUsed())
}
}