-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
135 lines (112 loc) · 3.09 KB
/
config.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
132
133
134
135
package check
import (
"fmt"
"os"
"path"
"reflect"
flag "github.com/spf13/pflag"
)
// Config represents a configuration for a monitoring plugin's CLI
type Config struct {
// Name of the monitoring plugin
Name string
// README represents the help text for the CLI usage
Readme string
// Output for the --version flag
Version string
// Default for the --timeout flag
Timeout int
// Default for the --verbose flag
Verbose bool
// Default for the --debug flag
Debug bool
// Enable predefined --version output
PrintVersion bool
// Enable predefined default flags for the monitoring plugin
DefaultFlags bool
// Enable predefined default functions (e.g. Timeout handler) for the monitoring plugin
DefaultHelper bool
// Additional CLI flags for the monitoring plugin
FlagSet *flag.FlagSet
}
// NewConfig returns a Config struct with some defaults
func NewConfig() *Config {
c := &Config{}
c.Name = path.Base(os.Args[0])
c.FlagSet = flag.NewFlagSet(c.Name, flag.ContinueOnError)
c.FlagSet.SortFlags = false
c.FlagSet.SetOutput(os.Stdout)
c.FlagSet.Usage = func() {
fmt.Printf("Usage of %s\n", c.Name)
if c.Readme != "" {
fmt.Println()
fmt.Println(c.Readme)
}
fmt.Println()
fmt.Println("Arguments:")
c.FlagSet.PrintDefaults()
}
// set some defaults
c.DefaultFlags = true
c.Timeout = 30
c.DefaultHelper = true
return c
}
// ParseArguments parses the command line arguments given by os.Args
func (c *Config) ParseArguments() {
c.ParseArray(os.Args[1:])
}
// ParseArray parses a list of command line arguments
func (c *Config) ParseArray(arguments []string) {
if c.DefaultFlags {
c.addDefaultFlags()
}
err := c.FlagSet.Parse(arguments)
if err != nil {
ExitError(err)
}
if c.PrintVersion {
fmt.Println(c.Name, "version", c.Version)
BaseExit(Unknown)
}
if c.DefaultHelper {
c.EnableTimeoutHandler()
}
}
// addDefaultFlags adds various default flags to the monitoring plugin
func (c *Config) addDefaultFlags() {
c.FlagSet.IntVarP(&c.Timeout, "timeout", "t", c.Timeout, "Abort the check after n seconds")
c.FlagSet.BoolVarP(&c.Debug, "debug", "d", false, "Enable debug mode")
c.FlagSet.BoolVarP(&c.Verbose, "verbose", "v", false, "Enable verbose mode")
c.FlagSet.BoolVarP(&c.PrintVersion, "version", "V", false, "Print version and exit")
c.DefaultFlags = false
}
// LoadFromEnv can be used to load struct values from 'env' tags.
// Mainly used to avoid passing secrets via the CLI
//
// type Config struct {
// Token string `env:"BEARER_TOKEN"`
// }
func LoadFromEnv(config interface{}) {
configValue := reflect.ValueOf(config).Elem()
configType := configValue.Type()
for i := range configValue.NumField() {
field := configType.Field(i)
tag := field.Tag.Get("env")
// If there's no "env" tag, skip this field.
if tag == "" {
continue
}
envValue := os.Getenv(tag)
if envValue == "" {
continue
}
// Potential for adding different types, for now we only use strings
// since the main use case is credentials
// nolint: exhaustive, gocritic
switch field.Type.Kind() {
case reflect.String:
configValue.Field(i).SetString(envValue)
}
}
}