/
root.go
68 lines (61 loc) · 1.88 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
67
68
package cmd
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type settings struct {
// logrus log level
loglevel string
}
type rootCmd struct {
cmd *cobra.Command
cfg *settings
}
func Execute(version string, args []string) {
newRootCmd(version).Execute(args)
}
func (cmd *rootCmd) Execute(args []string) {
cmd.cmd.SetArgs(args)
if err := cmd.cmd.Execute(); err != nil {
// if get to this point and don't fatally log in the subcommand,
// the Usage help will be printed before the error,
// which may or may not be the desired behavior
log.Fatalf("failed with error: %s", err)
}
}
func setGlobalSettings(cfg *settings) {
cfg.loglevel = viper.GetString("loglevel")
setLogLevel(cfg.loglevel)
}
func newRootCmd(version string) *rootCmd {
root := &rootCmd{cfg: &settings{}}
cmd := &cobra.Command{
Use: "qvm",
Short: "quarto version manager",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// need to set the config values here as the viper values
// will not be processed until Execute, so can't
// set them in the initializer.
// If persistentPreRun is used elsewhere, should
// remember to setGlobalSettings in the initializer
setGlobalSettings(root.cfg)
},
}
cmd.Version = version
// without this, the default version is like `cmd version <version>` so this
// will just print the version for simpler parsing
cmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)
cmd.PersistentFlags().String("loglevel", "info", "log level")
viper.BindPFlag("loglevel", cmd.PersistentFlags().Lookup("loglevel"))
cmd.AddCommand(newDebugCmd(root.cfg))
cmd.AddCommand(newManCmd().cmd)
cmd.AddCommand(newLsCmd().cmd)
cmd.AddCommand(newPathCmd().cmd)
cmd.AddCommand(newInstallCmd().cmd)
cmd.AddCommand(newUseCmd().cmd)
cmd.AddCommand(newInitCmd().cmd)
cmd.AddCommand(newRmCmd().cmd)
root.cmd = cmd
return root
}