forked from knes1/elktail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
251 lines (227 loc) · 7.37 KB
/
configuration.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
243
244
245
246
247
248
249
250
/* Copyright (C) 2016 Krešimir Nesek
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package main
import (
"runtime"
"os"
"encoding/json"
"io/ioutil"
"github.com/codegangsta/cli"
)
type SearchTarget struct {
Url string
TunnelUrl string `json:"-"`
IndexPattern string
}
type QueryDefinition struct {
Terms []string
Format string
TimestampField string
AfterDateTime string `json:"-"`
BeforeDateTime string `json:"-"`
}
type Configuration struct {
SearchTarget SearchTarget
QueryDefinition QueryDefinition
InitialEntries int
ListOnly bool `json:"-"`
User string
Password string `json:"-"`
Verbose bool `json:"-"`
MoreVerbose bool `json:"-"`
TraceRequests bool `json:"-"`
SSHTunnelParams string
SaveQuery bool `json:"-"`
}
var confDir = ".elktail"
var defaultConfFile = "default.json"
//When changing this array, make sure to also make appropriate changes in CopyConfigRelevantSettingsTo
var configRelevantFlags = []string{"url", "f", "i", "t", "u", "ssh"}
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
func (c *Configuration) Copy() *Configuration {
result := new(Configuration)
c.CopyConfigRelevantSettingsTo(result)
c.CopyNonConfigRelevantSettingsTo(result)
return result
}
//When making change here make sure configRelevantFlags global var is also changed
func (c *Configuration) CopyConfigRelevantSettingsTo(dest *Configuration) {
//copy config relevant configuration settings
dest.SearchTarget.TunnelUrl = c.SearchTarget.TunnelUrl
dest.SearchTarget.Url = c.SearchTarget.Url
dest.SearchTarget.IndexPattern = c.SearchTarget.IndexPattern
dest.QueryDefinition.Format = c.QueryDefinition.Format
dest.QueryDefinition.Terms = make([]string, len(c.QueryDefinition.Terms))
copy(dest.QueryDefinition.Terms, c.QueryDefinition.Terms)
dest.User = c.User
dest.SSHTunnelParams = c.SSHTunnelParams
}
func (c *Configuration) CopyNonConfigRelevantSettingsTo(dest *Configuration) {
//copy non-config relevant settings
dest.QueryDefinition.TimestampField = c.QueryDefinition.TimestampField
dest.QueryDefinition.AfterDateTime = c.QueryDefinition.AfterDateTime
dest.QueryDefinition.BeforeDateTime = c.QueryDefinition.BeforeDateTime
dest.ListOnly = c.ListOnly
dest.InitialEntries = c.InitialEntries
dest.Password = c.Password
dest.Verbose = c.Verbose
dest.MoreVerbose = c.MoreVerbose
dest.TraceRequests = c.TraceRequests
}
func (c *Configuration) SaveDefault() {
confDirPath := userHomeDir() + string(os.PathSeparator) + confDir;
if _, err := os.Stat(confDirPath); os.IsNotExist(err) {
//conf directory doesn't exist, let's create it
err := os.Mkdir(confDirPath, 0700)
if (err != nil) {
Error.Printf("Failed to create configuration directory %s, %s\n", confDirPath, err)
return
}
}
confJson, err := json.MarshalIndent(c, "", " ")
if (err != nil) {
Error.Printf("Failed to marshall configuration to json: %s.\n", err)
return
}
confFile := confDirPath + string(os.PathSeparator) + defaultConfFile;
err = ioutil.WriteFile(confFile, confJson, 0700)
if (err != nil) {
Error.Printf("Failed to save configuration to file %s, %s\n", confFile, err)
return
}
}
func LoadDefault() (conf *Configuration, err error) {
confDirPath := userHomeDir() + string(os.PathSeparator) + confDir;
if _, err := os.Stat(confDirPath); os.IsNotExist(err) {
//conf directory doesn't exist, let's create it
err := os.Mkdir(confDirPath, 0700)
if (err != nil) {
return nil, err
}
}
confFile := confDirPath + string(os.PathSeparator) + defaultConfFile;
var config *Configuration
confBytes, err := ioutil.ReadFile(confFile)
if (err != nil) {
return nil, err
}
err = json.Unmarshal(confBytes, &config)
if (err != nil) {
return nil, err
}
return config, nil
}
func (config *Configuration) Flags() []cli.Flag {
cli.VersionFlag.Usage = "Print the version"
cli.HelpFlag.Usage = "Show help"
return []cli.Flag {
cli.StringFlag{
Name: "url",
Value: "http://127.0.0.1:9200",
Usage: "(*) ElasticSearch URL",
Destination: &config.SearchTarget.Url,
},
cli.StringFlag{
Name: "f,format",
Value: "%message",
Usage: "(*) Message format for the entries - field names are referenced using % sign, for example '%@timestamp %message'",
Destination: &config.QueryDefinition.Format,
},
cli.StringFlag{
Name: "i,index-pattern",
Value: "logstash-[0-9].*",
Usage: "(*) Index pattern - elktail will attempt to tail only the latest of logstash's indexes matched by the pattern",
Destination: &config.SearchTarget.IndexPattern,
},
cli.StringFlag{
Name: "t,timestamp-field",
Value: "@timestamp",
Usage: "(*) Timestamp field name used for tailing entries",
Destination: &config.QueryDefinition.TimestampField,
},
cli.BoolFlag{
Name: "l,list-only",
Usage: "Just list the results once, do not follow",
Destination: &config.ListOnly,
},
cli.IntFlag{
Name: "n",
Value: 50,
Usage: "Number of entries fetched initially",
Destination: &config.InitialEntries,
},
cli.StringFlag{
Name: "a,after",
Value: "",
Usage: "List results after specified date (example: -a \"2016-06-17T15:00\")",
Destination: &config.QueryDefinition.AfterDateTime,
},
cli.StringFlag{
Name: "b,before",
Value: "",
Usage: "List results before specified date (example: -b \"2016-06-17T15:00\")",
Destination: &config.QueryDefinition.BeforeDateTime,
},
cli.BoolFlag{
Name: "s",
Usage: "Save query terms - next invocation of elktail (without parameters) will use saved query terms. Any additional terms specified will be applied with AND operator to saved terms",
Destination: &config.SaveQuery,
},
cli.StringFlag{
Name: "u",
Value: "",
Usage: "(*) Username for http basic auth, password is supplied over password prompt",
Destination: &config.User,
},
cli.StringFlag{
Name: "ssh,ssh-tunnel",
Value: "",
Usage: "(*) Use ssh tunnel to connect. Format for the argument is [localport:][user@]sshhost.tld[:sshport]",
Destination: &config.SSHTunnelParams,
},
cli.BoolFlag{
Name: "v1",
Usage: "Enable verbose output (for debugging)",
Destination: &config.Verbose,
},
cli.BoolFlag{
Name: "v2",
Usage: "Enable even more verbose output (for debugging)",
Destination: &config.MoreVerbose,
},
cli.BoolFlag{
Name: "v3",
Usage: "Same as v2 but also trace requests and responses (for debugging)",
Destination: &config.TraceRequests,
},
cli.VersionFlag,
cli.HelpFlag,
}
}
//Elktail will work in list-only (no follow) mode if appropriate flag is set or if query has date-time filtering enabled
func (c *Configuration) IsListOnly() bool {
return c.ListOnly || c.QueryDefinition.IsDateTimeFiltered()
}
func (q *QueryDefinition) IsDateTimeFiltered() bool {
return q.AfterDateTime != "" || q.BeforeDateTime != ""
}
func IsConfigRelevantFlagSet(c *cli.Context) bool {
for _, flag := range configRelevantFlags {
if c.IsSet(flag) {
return true
}
}
return false
}