-
Notifications
You must be signed in to change notification settings - Fork 20
/
options.go
137 lines (130 loc) · 3.34 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
package main
import (
"net/url"
"os"
"github.com/CiscoCloud/distributive/errutil"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
const defaultVerbosity = log.WarnLevel
// 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) {
var levelMap = map[string]log.Level{
"info": log.InfoLevel,
"debug": log.DebugLevel,
"fatal": log.FatalLevel,
"error": log.ErrorLevel,
"panic": log.PanicLevel,
"warn": log.WarnLevel,
}
if v, ok := levelMap[verbosity]; ok {
log.SetLevel(v)
} else {
log.SetLevel(defaultVerbosity)
}
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) {
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: "[email protected]",
},
}
*/
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "verbosity",
Value: "",
Usage: "info | debug | fatal | error | panic | warn",
},
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: "",
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 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)
}
// set logLevel appropriately for chkutils
initializeLogrus(c.String("verbosity"))
file = c.String("file")
URL = c.String("url")
directory = c.String("directory")
stdin = c.Bool("stdin")
if file == "" && URL == "" && stdin == false && directory == "" {
// use default directory if no other options specified
directory = "/etc/distributive.d/"
}
log.WithFields(log.Fields{
"file": file,
"URL": URL,
"directory": directory,
"stdin": stdin,
}).Debug("Command line options")
useCache = !c.Bool("no-cache")
}
app.Run(os.Args) // parse the arguments, execute app.Action
return file, URL, directory, stdin
}