-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
139 lines (110 loc) · 4.08 KB
/
flags.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
136
137
138
139
package commands
import (
"fmt"
"os"
"strings"
"github.com/gruntwork-io/gruntwork-cli/logging"
"github.com/gruntwork-io/health-checker/options"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const DEFAULT_LISTENER_IP_ADDRESS = "0.0.0.0"
const DEFAULT_LISTENER_PORT = 5500
const DEFAULT_SCRIPT_TIMEOUT_SEC = 5
const ENV_VAR_NAME_DEBUG_MODE = "HEALTH_CHECKER_DEBUG"
var portFlag = cli.IntSliceFlag{
Name: "port",
Usage: fmt.Sprintf("[One of port/script Required] The port number on which a TCP connection will be attempted. Specify one or more times. Example: 8000"),
}
var scriptFlag = cli.StringSliceFlag{
Name: "script",
Usage: fmt.Sprintf("[One of port/script Required] The path to script that will be run. Specify one or more times. Example: \"/usr/local/bin/health-check.sh --http-port 8000\""),
}
var scriptTimeoutFlag = cli.IntFlag{
Name: "script-timeout",
Usage: fmt.Sprintf("[Optional] Timeout, in seconds, to wait for the scripts to complete. Example: 10"),
Value: DEFAULT_SCRIPT_TIMEOUT_SEC,
}
var singleflightFlag = cli.BoolFlag{
Name: "singleflight",
Usage: fmt.Sprintf("[Optional] Enable singleflight mode, which makes concurrent requests share the same check."),
}
var listenerFlag = cli.StringFlag{
Name: "listener",
Usage: fmt.Sprintf("[Optional] The IP address and port on which inbound HTTP connections will be accepted."),
Value: fmt.Sprintf("%s:%d", DEFAULT_LISTENER_IP_ADDRESS, DEFAULT_LISTENER_PORT),
}
var logLevelFlag = cli.StringFlag{
Name: "log-level",
Usage: fmt.Sprintf("[Optional] Set the log level to `LEVEL`. Must be one of: %v", logrus.AllLevels),
Value: logrus.InfoLevel.String(),
}
var defaultFlags = []cli.Flag{
portFlag,
scriptFlag,
scriptTimeoutFlag,
singleflightFlag,
listenerFlag,
logLevelFlag,
}
// Return true if no options at all were passed to the CLI. Note that we are specifically testing for flags, some of which
// are required, not just args.
func allCliOptionsEmpty(cliContext *cli.Context) bool {
return cliContext.NumFlags() == 0
}
// Parse and validate all CLI options
func parseOptions(cliContext *cli.Context) (*options.Options, error) {
logger := logging.GetLogger("health-checker")
// By default logrus logs to stderr. But since most output in this tool is informational, we default to stdout.
logger.Out = os.Stdout
logLevel := cliContext.String(logLevelFlag.Name)
level, err := logrus.ParseLevel(logLevel)
if err != nil {
return nil, InvalidLogLevel(logLevel)
}
logger.SetLevel(level)
ports := cliContext.IntSlice("port")
scriptArr := cliContext.StringSlice("script")
scripts := options.ParseScripts(scriptArr)
if len(ports) == 0 && len(scripts) == 0 {
return nil, OneOfParamsRequired{portFlag.Name, scriptFlag.Name}
}
singleflight := cliContext.Bool("singleflight")
scriptTimeout := cliContext.Int("script-timeout")
listener := cliContext.String("listener")
if listener == "" {
return nil, MissingParam(listenerFlag.Name)
}
return &options.Options{
Ports: ports,
Scripts: scripts,
ScriptTimeout: scriptTimeout,
Singleflight: singleflight,
Listener: listener,
Logger: logger,
}, nil
}
// Some error types are simple enough that we'd rather just show the error message directly instead of vomiting out a
// whole stack trace in log output. Therefore, allow a debug mode that always shows full stack traces. Otherwise, show
// simple messages.
func isDebugMode() bool {
envVar, _ := os.LookupEnv(ENV_VAR_NAME_DEBUG_MODE)
envVar = strings.ToLower(envVar)
return envVar == "true"
}
// Custom error types
type InvalidLogLevel string
func (invalidLogLevel InvalidLogLevel) Error() string {
return fmt.Sprintf("The log-level value \"%s\" is invalid", string(invalidLogLevel))
}
type MissingParam string
func (paramName MissingParam) Error() string {
return fmt.Sprintf("Missing required parameter --%s", string(paramName))
}
type OneOfParamsRequired struct {
param1 string
param2 string
}
func (paramNames OneOfParamsRequired) Error() string {
return fmt.Sprintf("Missing required parameter, one of --%s / --%s required", string(paramNames.param1), string(paramNames.param2))
}