-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
131 lines (119 loc) · 3.63 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package cmd handles the commandline user interface and interactions.
// nolint:gochecknoglobals
package cmd
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/gookit/color" //nolint:misspell
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/Defacto2/df2/lib/config"
"github.com/Defacto2/df2/lib/logs"
"github.com/Defacto2/df2/lib/str"
)
var simulate bool
var (
// ErrCmd invalid command.
ErrCmd = errors.New("invalid command, please use one of the available commands")
// ErrNoID no id.
ErrNoID = errors.New("requires an id or uuid argument")
// ErrID bad id.
ErrID = errors.New("invalid id or uuid specified")
)
var (
configName = ""
panics = false // debug log
quiet = false // quiet disables most printing or output to terminal
)
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "df2",
Example: " df2 new",
Short: "A tool to optimise and manage defacto2.net",
Long: fmt.Sprintf("%s\nCopyright © %v Ben Garrett\n%v",
color.Info.Sprint("A tool to optimise and manage defacto2.net"),
copyright(),
color.Primary.Sprint("https://github.com/Defacto2/df2")),
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(color.Warn.Sprintf("%s", err))
e := err.Error()
if strings.Contains(e, "required flag(s) \"name\"") {
logs.Println("see Examples for usage or run to list setting choices:", color.Bold.Sprintf("%s config info", rootCmd.CommandPath()))
}
os.Exit(1)
}
config.Check()
}
func init() { // nolint:gochecknoinits
cobra.OnInitialize()
initConfig()
rootCmd.PersistentFlags().StringVar(&configName, "config", "", fmt.Sprintf("config file (default is %s)", config.Filepath()))
rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "suspend feedback to the terminal")
rootCmd.PersistentFlags().BoolVar(&panics, "panic", false, "panic in the disco")
if err := rootCmd.PersistentFlags().MarkHidden("panic"); err != nil {
logs.Fatal(err)
}
}
// copyright returns a © Copyright year, or a range of years.
func copyright() string {
var y, c int = time.Now().Year(), 2020
if y == c {
return strconv.Itoa(c) // © 2020
}
return fmt.Sprintf("%s-%s", strconv.Itoa(c), time.Now().Format("06")) // © 2020-21
}
// filterFlag compairs the value of the filter flag against the list of slice values.
func filterFlag(t interface{}, flag, val string) {
if val == "" {
return
}
if t, ok := t.([]string); ok {
sup := false
for _, value := range t {
if value == val || (val == value[:1]) {
sup = true
break
}
}
if !sup {
fmt.Printf("%s %s\n%s %s\n", color.Warn.Sprintf("unsupported --%s flag value", flag),
color.Bold.Sprintf("%q", val),
color.Warn.Sprint("available flag values"),
color.Primary.Sprint(strings.Join(t, ",")))
os.Exit(1)
}
}
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
logs.Panic = panics
logs.Quiet = quiet
cf := config.Filepath()
if cf != "" {
viper.SetConfigFile(cf)
} else {
home, err := os.UserHomeDir()
if err != nil {
logs.Fatal(err)
}
viper.AddConfigPath(home)
viper.SetConfigName(config.Config.Name)
}
viper.AutomaticEnv() // read in environment variables that match
// if a config file is found, read it in
if err := viper.ReadInConfig(); err != nil {
config.Config.Errors = true
} else if !quiet {
if !str.Piped() {
logs.Println(str.Sec(fmt.Sprintf("config file in use: %s", viper.ConfigFileUsed())))
}
}
}