-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
65 lines (54 loc) · 1.2 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
package commands
import (
"fmt"
"os"
log "github.com/buglloc/simplelog"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/buglloc/rip/v2/pkg/cli"
)
var RootCmd = &cobra.Command{
Use: "rip",
Short: "Wildcard DNS",
SilenceUsage: true,
PreRunE: parseRootConfig,
}
func init() {
cobra.OnInitialize(initConfig)
flags := RootCmd.PersistentFlags()
flags.String("config", "",
"config file (default is $HOME/.rip.toml)")
flags.Bool("verbose", false,
"verbose output")
viper.AutomaticEnv()
_ = cli.BindPFlags(flags)
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func initConfig() {
if cfgFile := viper.GetString("config"); cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(home)
viper.AddConfigPath(".")
viper.SetConfigName(".rip")
}
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
func parseRootConfig(cmd *cobra.Command, args []string) error {
if viper.GetBool("Verbose") {
log.SetLevel(log.DebugLevel)
}
return nil
}