-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
224 lines (199 loc) · 7.52 KB
/
args.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"./config"
"fmt"
"github.com/akamensky/argparse"
"github.com/araddon/dateparse"
"golang.org/x/sys/unix"
"os"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
// DefaultLimit is the value used when no limit is provided by the user
const DefaultLimit = 300
// DefaultRange is the value used when no range is provided by the user
const DefaultRange = "2h"
// DefaultConfigPath is the default location of the configuration path.
const DefaultConfigPath = "~/.graylog"
// options structure stores the command-line options and values.
type options struct {
listStreams bool
application string
query string
fields string
limit int
streamIds []string
tail bool
configPath string
timeRange int
startDate *time.Time
endDate *time.Time
json bool
serverConfig *config.IniFile
noColor bool
}
// parseArgs parses the command-line arguments.
// returns: *options which contains both the parsed command-line arguments.
func parseArgs() *options {
parser := argparse.NewParser("graylog", "Search and tail logs from Graylog.")
var defaultConfigPath = expandPath(DefaultConfigPath)
listStreams := parser.Flag("", "list-streams", &argparse.Options{Required: false, Help: "List Graylog streams and exit."})
application := parser.String("a", "application", &argparse.Options{Required: false, Help: "Special case to search the 'application' message field, e.g., -a send-email is equivalent to -q 'application:send-email'. Merged with the -q query using 'AND' if the -q query is present."})
query := parser.String("q", "query", &argparse.Options{Required: false, Help: "Query terms to search on (Elasticsearch syntax). Defaults to '*'."})
fields := parser.String("e", "export", &argparse.Options{Required: false, Help: "Export specified fields as CSV into a file named 'export.csv'. Format is 'field1,field2,field3...'. Requires --start (and, optionally, --end) option."})
limit := parser.Int("l", "limit", &argparse.Options{Required: false, Help: "The maximum number of messages to request from Graylog. Must be greater then 0", Default: DefaultLimit})
streamNames := parser.String("s", "stream", &argparse.Options{Required: false, Help: "The name of the stream(s) to display messages from. Default: all streams."})
tail := parser.Flag("t", "tail", &argparse.Options{Required: false, Help: "Whether to tail the output. Requires a relative search."})
configPath := parser.String("c", "config", &argparse.Options{Required: false, Help: "Path to the config file", Default: defaultConfigPath})
timeRange := parser.String("r", "range", &argparse.Options{Required: false, Help: "Time range to search backwards from the current moment. Examples: 30m, 2h, 4d", Default: DefaultRange})
start := parser.String("", "start", &argparse.Options{Required: false, Help: "Starting time to search from. Allows variable formats, including '1:32pm' or '1/4/2019 12:30:00'."})
end := parser.String("", "end", &argparse.Options{Required: false, Help: "Ending time to search from. Allows variable formats, including '6:45am' or '2019-01-04 12:30:00'. Defaults to now if --start is provided but no --end."})
json := parser.Flag("j", "json", &argparse.Options{Required: false, Help: "Output messages in json format. Shows the modified log message, not the untouched message from Graylog. Useful in understanding the fields available when creating Format templates or for further processing."})
noColor := parser.Flag("", "no-colors", &argparse.Options{Required: false, Help: "Don't use colors in output."})
if err := parser.Parse(os.Args); err != nil {
invalidArgs(parser, err, "")
}
if len(*fields) > 0 && len(*start) == 0 {
invalidArgs(parser, nil, "The --export option requires the --start option")
}
startDate := strToDate(parser, *start, "The --start date can't be parsed", false)
endDate := strToDate(parser, *end, "The --end date can't be parsed", true)
if *limit <= 0 {
var newLimit = DefaultLimit
limit = &newLimit
}
if startDate != nil {
var newTail = false
tail = &newTail
}
var newQuery string
if len(*application) > 0 {
newQuery = "application:" + *application
if len(*query) > 0 {
newQuery += " AND " + *query
}
query = &newQuery
}
opts := options{
listStreams: *listStreams,
application: *application,
query: *query,
fields: *fields,
limit: *limit,
tail: *tail,
configPath: *configPath,
timeRange: timeRangeToSeconds(parser, *timeRange),
startDate: startDate,
endDate: endDate,
json: *json,
noColor: *noColor || isTty(),
}
// Read the configuration file
cfg, err := config.New(opts.configPath)
if err != nil {
invalidArgs(parser, err, "")
}
opts.serverConfig = cfg
// Convert the stream names into Graylog stream ids
if len(*streamNames) > 0 {
opts.streamIds = findStreamIds(&opts, *streamNames)
if opts.streamIds == nil {
invalidArgs(parser, nil, "Invalid stream name(s)")
}
}
return &opts
}
// Convert a variable human-friendly date into a time.Time.
func strToDate(parser *argparse.Parser, dateStr string, errorStr string, defaultToNow bool) *time.Time {
var dateTime time.Time
var err error
if len(dateStr) > 0 {
// Check to see if the date is a time only
matched, _ := regexp.MatchString("^[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?([ ]*(am|pm|AM|PM)?)?$", dateStr)
if matched {
dateStr = time.Now().Format("2006-01-02") + " " + dateStr
}
dateTime, err = dateparse.ParseLocal(dateStr)
if err != nil {
invalidArgs(parser, err, errorStr)
} else {
if dateTime.Year() == 0 {
dateTime = dateTime.AddDate(time.Now().Year(), 0, 0)
}
}
if err != nil {
return nil
}
return &dateTime
}
if defaultToNow {
dateTime = time.Now()
return &dateTime
}
return nil
}
// Converts a simple human-friendly time range into seconds, e.g., 2h for 2 hours, 3d2h30m for 3 days, 2 hours and
// 30 minutes.
func timeRangeToSeconds(parser *argparse.Parser, timeRange string) int {
re := regexp.MustCompile("([0-9]*)([a-zA-Z]*)")
parts := re.FindAllString(timeRange, -1)
var accumulator int
for _, part := range parts {
if len(part) > 1 {
unit := part[len(part)-1:]
numberStr := part[:len(part)-1]
num, err := strconv.Atoi(numberStr)
if err != nil {
invalidArgs(parser, err, "Time range can't be parsed")
}
switch strings.ToLower(unit) {
case "s":
accumulator += num
case "m":
accumulator += num * 60
case "h":
accumulator += num * 3600
case "d":
accumulator += num * 86400
default:
invalidArgs(parser, err, "Time range can't be parsed")
}
}
}
return accumulator
}
// Display the help message when a command-line argument is invalid.
func invalidArgs(parser *argparse.Parser, err error, msg string) {
if len(msg) > 0 {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n\n", msg, err.Error())
} else {
fmt.Fprintf(os.Stderr, "%s\n\n", msg)
}
} else if err != nil {
fmt.Fprintf(os.Stderr, "%s\n\n", err.Error())
}
fmt.Fprintf(os.Stderr, parser.Usage(nil))
os.Exit(1)
}
// Expand a leading tilde (~) in a file path into the user's home directory.
func expandPath(configPath string) string {
var path = configPath
if strings.HasPrefix(configPath, "~/") {
usr, _ := user.Current()
dir := usr.HomeDir
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
path = filepath.Join(dir, path[2:])
}
return path
}
// Check to see whether we're outputting to a terminal or if we've been redirected to a file
func isTty() bool {
_, err := unix.IoctlGetTermios(int(os.Stdout.Fd()), unix.TIOCGETA)
return err == nil
}