-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.go
242 lines (207 loc) · 5.86 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/gookit/color"
"github.com/gookit/goutil/strutil"
"github.com/qiangyt/jog/config"
"github.com/tj/go-naturaldate"
)
// OptionsT ...
type OptionsT struct {
LogFilePath string
ConfigFilePath string
Debug bool
ConfigItemPath string
ConfigItemValue string
FollowMode bool
NumberOfLines int
levelFilterTexts []string
levelFilters []config.Enum
beforeFilterText string
BeforeFilter *time.Time
afterFilterText string
AfterFilter *time.Time
OutputRawJSON bool
GrokPatternsUsed []string
GrokPatterns []string
}
// Options ...
type Options = *OptionsT
// InitGroks ...
func (i Options) InitGroks(cfg config.Configuration) {
if len(i.GrokPatternsUsed) == 0 {
// try to uses default patterns
i.GrokPatternsUsed = cfg.Grok.Uses
}
i.GrokPatterns = make([]string, len(i.GrokPatternsUsed))
for index, patternName := range i.GrokPatternsUsed {
i.GrokPatterns[index] = "%{" + patternName + "}"
}
}
func (i Options) isGrokEnabled() bool {
return len(i.GrokPatterns) > 0
}
// GetLevelFilters ...
func (i Options) GetLevelFilters() []config.Enum {
return i.levelFilters
}
// InitLevelFilters ...
func (i Options) InitLevelFilters(levelFieldEnums config.EnumMap) {
if len(i.levelFilterTexts) == 0 {
i.levelFilters = make([]config.Enum, 0)
return
}
for _, levelFilterText := range i.levelFilterTexts {
levelFilter := levelFieldEnums.GetEnum(levelFilterText)
i.levelFilters = append(i.levelFilters, levelFilter)
}
}
// InitTimestampFilters ...
func (i Options) InitTimestampFilters(timestampField config.Field) {
now := time.Now()
if len(i.beforeFilterText) > 0 {
f, err := naturaldate.Parse(i.beforeFilterText, now, naturaldate.WithDirection(naturaldate.Past))
if err != nil {
log.Printf("failed to parse before-time filter %s as natural timestamp, so try absolute parse\n", i.beforeFilterText)
f = ParseTimestamp(timestampField, i.beforeFilterText)
}
log.Printf("before-time filter: %v", f)
i.BeforeFilter = &f
}
if len(i.afterFilterText) > 0 {
f, err := naturaldate.Parse(i.afterFilterText, now, naturaldate.WithDirection(naturaldate.Past))
if err != nil {
log.Printf("failed to parse after-time filter %s as natural timestamp, so try absolute parse\n", i.afterFilterText)
f = ParseTimestamp(timestampField, i.afterFilterText)
}
log.Printf("after-time filter: %v", f)
i.AfterFilter = &f
if i.BeforeFilter != nil {
if i.BeforeFilter.Before(*i.AfterFilter) {
panic(fmt.Errorf("before-time filter (%s) shouldn't be before after-time filter (%s)", i.beforeFilterText, i.afterFilterText))
}
}
}
}
// HasTimestampFilter ...
func (i Options) HasTimestampFilter() bool {
return i.BeforeFilter != nil || i.AfterFilter != nil
}
func printErrorHint(format string, a ...interface{}) {
PrintHelp()
color.Red.Printf(format+". Please check above example\n", a...)
}
// OptionsWithCommandLine ...
func OptionsWithCommandLine() (bool, Options) {
r := &OptionsT{
Debug: false,
FollowMode: false,
NumberOfLines: -1,
levelFilterTexts: make([]string, 0),
GrokPatternsUsed: make([]string, 0),
}
var err error
var hasNumberOfLines = false
for i := 0; i < len(os.Args); i++ {
if i == 0 {
continue
}
arg := os.Args[i]
if arg[0:1] == "-" {
if arg == "-c" || arg == "--config" {
if i+1 >= len(os.Args) {
printErrorHint("Missing config file path")
return false, nil
}
r.ConfigFilePath = os.Args[i+1]
i++
} else if arg == "-cs" || arg == "--config-set" {
if i+1 >= len(os.Args) {
printErrorHint("Missing config item expression")
return false, nil
}
r.ConfigItemPath, r.ConfigItemValue, err = ParseConfigExpression(os.Args[i+1])
if err != nil {
printErrorHint("%v", err)
return false, nil
}
i++
} else if arg == "-cg" || arg == "--config-get" {
if i+1 >= len(os.Args) {
printErrorHint("Missing config item path")
return false, nil
}
r.ConfigItemPath = os.Args[i+1]
i++
} else if arg == "-f" || arg == "--follow" {
r.FollowMode = true
} else if arg == "-n" || arg == "--lines" {
if i+1 >= len(os.Args) {
printErrorHint("Missing lines argument")
return false, nil
}
r.NumberOfLines = strutil.MustInt(os.Args[i+1])
hasNumberOfLines = true
i++
} else if arg == "-t" || arg == "--template" {
PrintConfigTemplate()
return false, nil
} else if arg == "-h" || arg == "--help" {
PrintHelp()
return false, nil
} else if arg == "-V" || arg == "--version" {
PrintVersion()
return false, nil
} else if arg == "-d" || arg == "--debug" {
r.Debug = true
} else if arg == "-j" || arg == "--json" {
r.OutputRawJSON = true
} else if arg == "-l" || arg == "--level" {
if i+1 >= len(os.Args) {
printErrorHint("Missing level argument")
return false, nil
}
r.levelFilterTexts = append(r.levelFilterTexts, os.Args[i+1])
i++
} else if arg == "-g" || arg == "--grok" {
if i+1 >= len(os.Args) {
printErrorHint("Missing grok argument")
return false, nil
}
r.GrokPatternsUsed = append(r.GrokPatternsUsed, os.Args[i+1])
i++
} else if arg == "--reset-grok-library-dir" {
config.ResetDefaultGrokLibraryDir()
return false, nil
} else if arg == "-a" || arg == "--after" {
if i+1 >= len(os.Args) {
printErrorHint("Missing after argument")
return false, nil
}
r.afterFilterText = os.Args[i+1]
i++
} else if arg == "-b" || arg == "--before" {
if i+1 >= len(os.Args) {
printErrorHint("Missing before argument")
return false, nil
}
r.beforeFilterText = os.Args[i+1]
i++
} else {
printErrorHint("Unknown option: '%s'", arg)
return false, nil
}
} else {
r.LogFilePath = arg
}
}
if !hasNumberOfLines {
if r.FollowMode {
r.NumberOfLines = 10
}
}
return true, r
}