forked from CiscoCloud/distributive
/
options.go
146 lines (141 loc) · 3.51 KB
/
options.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
140
141
142
143
144
145
146
package main
import (
"github.com/CiscoCloud/distributive/errutil"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"net/url"
"os"
)
// validateFlags ensures that all options passed via the command line are valid
func validateFlags(file string, URL string, directory string) {
// validatePath ensures that something is at a given path
validatePath := func(path string) {
if _, err := os.Stat(path); err != nil {
errutil.CouldntReadError(path, err)
}
} // validateURL ensures that the given URL is valid, or logs an error
validateURL := func(urlstr string) {
if _, err := url.Parse(urlstr); err != nil {
log.WithFields(log.Fields{
"url": urlstr,
"error": err.Error(),
}).Fatal("Couldn't parse URL")
}
}
if URL != "" {
validateURL(URL)
}
if directory != "" {
validatePath(directory)
}
if file != "" {
validatePath(file)
}
}
// initializeLogrus sets the logrus log level according to the specified
// verbosity level, both for packages main and chkutils
func initializeLogrus(verbosity string) {
lvls := "info | debug | fatal | panic | warn"
var logLevel log.Level
logLevel = 0
switch verbosity {
case "info":
logLevel = log.InfoLevel
case "debug":
logLevel = log.DebugLevel
case "fatal":
logLevel = log.FatalLevel
case "error":
logLevel = log.ErrorLevel
case "panic":
logLevel = log.PanicLevel
case "warn":
logLevel = log.WarnLevel
default:
log.WithFields(log.Fields{
"given": verbosity,
"expected": lvls,
}).Fatal("Invalid verbosity option")
}
log.SetLevel(logLevel)
log.WithFields(log.Fields{
"verbosity": verbosity,
}).Debug("Verbosity level specified")
}
// getFlags validates and returns command line options
func getFlags() (f string, u string, d string, s bool) {
lvls := "info | debug | fatal | error | panic | warn"
defaultVerbosity := "warn"
app := cli.NewApp()
app.Name = "Distributive"
app.Usage = "Perform distributed health tests"
app.Version = Version
app.Author = "Langston Barrett"
/* For a newer version of cli:
app.Authors = []cli.Author{
cli.Author{
Name: "Langston Barrett",
Email: "langston@aster.is",
},
}
*/
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "verbosity",
Value: defaultVerbosity,
Usage: lvls,
},
cli.StringFlag{
Name: "file, f",
Value: "",
Usage: "Read a checklist from a file",
},
cli.StringFlag{
Name: "url, u",
Value: "",
Usage: "Read a checklist from a URL",
},
cli.StringFlag{
Name: "directory, d",
Value: "/etc/distributive.d/",
Usage: "Read all of the checklists in this directory",
},
cli.BoolFlag{
Name: "stdin, s",
Usage: "Read data piped from stdin as a checklist",
},
cli.BoolFlag{
Name: "no-cache",
Usage: "Don't use a cached version of a remote check, fetch it.",
},
}
var verbosity string
var file string
var URL string
var directory string
var stdin bool
app.Action = func(c *cli.Context) {
version := c.Bool("version")
if version {
os.Exit(0)
}
verbosity = c.String("verbosity")
file = c.String("file")
URL = c.String("url")
directory = c.String("directory")
stdin = c.Bool("stdin")
log.WithFields(log.Fields{
"file": file,
"URL": URL,
"directory": directory,
"stdin": stdin,
}).Debug("Command line options")
useCache = !c.Bool("no-cache")
}
if verbosity == "" {
verbosity = "warn"
}
app.Run(os.Args) // parse the arguments, execute app.Action
initializeLogrus(verbosity) // set logLevel appropriately for chkutils
return file, URL, directory, stdin
}