-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootcmd.go
105 lines (89 loc) · 3.03 KB
/
rootcmd.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"os"
"path"
"github.com/adrg/xdg"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
// Used for flags.
cfgFile string
cfgDir string = path.Join(xdg.StateHome, "unmarked")
defaultCfgFile string = path.Join(cfgDir, "unmarked.yaml")
userLicense string
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "unmarked",
Short: "Unmarked marks windows for use with shortcuts",
Long: `"Unmarked marks windows for use with shortcuts"`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
},
}
)
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
var debug bool
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug logging")
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfgFile, "default config file")
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
// Cobra also supports local flags, which will only run when this
// action is called directly.
// res, err := rootCmd.Flags().GetBool("toggle")
// rootCmd.Flags().BoolP("toggle", "t", false, "Set a toggle")
}
// InitCobra adds all child commands to the root command and sets flags appropriately.
// This is called by main.init(). It only needs to happen once to the rootCmd.
func InitCobra() error {
return rootCmd.Execute()
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(xdg.Home)
viper.AddConfigPath(path.Join(xdg.ConfigHome, AppName))
// viper.AddConfigPath(path.Join(xdg.StateHome, AppName))
viper.SetConfigType("yaml")
viper.SetConfigName(AppName)
}
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Warnf("Warning: config file not found: %v, %v", cfgFile, err)
} else {
log.Warnf("Warning: Error in processing config file: %v, %v", cfgFile, err)
}
}
log.Printf("Using config file: %v", viper.ConfigFileUsed())
rootCmd.Flags().VisitAll(func(f *pflag.Flag) {
// Determine the naming convention of the flags when
// represented in the config file
configName := f.Name
log.Printf("Processing flag: %v", f.Name)
// Apply the viper config value to the flag
// when the flag is not set and viper has a value
if !f.Changed && viper.IsSet(configName) {
val := viper.Get(configName)
rootCmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
}
}