-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
50 lines (39 loc) · 1.16 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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/nextlinux/chronicle/internal/config"
)
var persistentOpts = config.CliOnlyOptions{}
var rootCmd = &cobra.Command{
Short: createCmd.Short,
Long: createCmd.Long,
Args: createCmd.Args,
Example: createCmd.Example,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: createCmd.PreRunE,
RunE: createCmd.RunE,
ValidArgsFunction: createCmd.ValidArgsFunction,
}
func init() {
if err := setGlobalFlags(rootCmd.PersistentFlags()); err != nil {
panic(fmt.Sprintf("unable to set global flags: %+v", err))
}
setCreateFlags(rootCmd.Flags())
}
func setGlobalFlags(flags *pflag.FlagSet) error {
flags.StringVarP(&persistentOpts.ConfigPath, "config", "c", "", "application config file")
flag := "quiet"
flags.BoolP(
flag, "q", false,
"suppress all logging output",
)
if err := viper.BindPFlag(flag, flags.Lookup(flag)); err != nil {
return err
}
flags.CountVarP(&persistentOpts.Verbosity, "verbose", "v", "increase verbosity (-v = info, -vv = debug)")
return nil
}