This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
294 lines (239 loc) · 6.08 KB
/
main.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
// Generate test data
// 1. call Graylog API by API browser
// 2. copy and paste the response to testdata/***.json
// 3. add type and update data
// 4. run this command and output test data
// 5. copy and paste the test data to testdata/***.go
import (
"encoding/json"
"errors"
"log"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/sanity-io/litter"
"github.com/suzuki-shunsuke/go-graylog/v8"
)
type (
dumper interface {
dump(string) error
}
)
func main() {
if err := Main(); err != nil {
log.Fatal(err)
}
}
func Main() error {
options := make([]string, len(data))
i := 0
for k := range data {
options[i] = k
i++
}
file := ""
prompt := &survey.Select{
Message: "file",
Options: options,
}
if err := survey.AskOne(prompt, &file); err != nil {
return err
}
d, ok := data[file]
if !ok {
return errors.New("file is not found")
}
if err := d.dump("testdata/" + file + ".json"); err != nil {
return err
}
return nil
}
func dump(input string, dest interface{}) error {
f, err := os.Open(input)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(dest); err != nil {
return err
}
options := litter.Options{
HideZeroValues: true,
}
options.Dump(dest)
return nil
}
var (
data = map[string]dumper{
"response_create_event_notification": EventNotification{},
"request_create_event_notification": EventNotification{},
"event_definition/gets/response": EventDefinitions{},
"event_definition/get/response": EventDefinition{},
"event_definition/create/request": EventDefinition{},
"event_definition/create/response": EventDefinition{},
"event_definition/update/request": EventDefinition{},
"event_definition/update/response": EventDefinition{},
"event_notifications": EventNotifications{},
"users": Users{},
"user": User{},
"roles": Roles{},
"role": Role{},
"index_sets": IndexSets{},
"index_set": IndexSet{},
"inputs": Inputs{},
"input": Input{},
"streams": Streams{},
"stream": Stream{},
"stream_rules": StreamRules{},
"stream_rule": StreamRule{},
"dashboards": Dashboards{},
"dashboard": Dashboard{},
"stream_alarm_callbacks": StreamAlarmCallbacks{},
"slack_stream_alarm_callback": StreamAlarmCallback{},
"http_stream_alarm_callback": StreamAlarmCallback{},
"email_stream_alarm_callback": StreamAlarmCallback{},
"stream_alert_conditions": StreamAlertConditions{},
"outputs": Outputs{},
"stdout_output": Output{},
}
)
type (
Users struct {
users graylog.UsersBody
}
User struct {
user graylog.User
}
Roles struct {
roles graylog.RolesBody
}
Role struct {
role graylog.Role
}
IndexSets struct {
data graylog.IndexSetsBody
}
IndexSet struct {
data graylog.IndexSet
}
Inputs struct {
data graylog.InputsBody
}
Input struct {
data graylog.Input
}
Streams struct {
data graylog.StreamsBody
}
Stream struct {
data graylog.Stream
}
StreamRules struct {
data graylog.StreamRulesBody
}
StreamRule struct {
data graylog.StreamRule
}
Dashboards struct {
data graylog.DashboardsBody
}
Dashboard struct {
data graylog.Dashboard
}
StreamAlarmCallbacks struct {
data graylog.AlarmCallbacksBody
}
StreamAlarmCallback struct {
data graylog.AlarmCallback
}
StreamAlertConditions struct {
data graylog.AlertConditionsBody
}
Output struct {
data graylog.Output
}
Outputs struct {
data graylog.OutputsBody
}
EventNotification struct {
data graylog.EventNotification
}
EventNotifications struct {
data graylog.EventNotificationsBody
}
EventDefinitions struct {
data graylog.EventDefinitionsBody
}
EventDefinition struct {
data graylog.EventDefinition
}
)
func (users Users) dump(input string) error {
return dump(input, &users.users)
}
func (user User) dump(input string) error {
return dump(input, &user.user)
}
func (roles Roles) dump(input string) error {
return dump(input, &roles.roles)
}
func (role Role) dump(input string) error {
return dump(input, &role.role)
}
func (is IndexSets) dump(input string) error {
return dump(input, &is.data)
}
func (is IndexSet) dump(input string) error {
return dump(input, &is.data)
}
func (ip Inputs) dump(input string) error {
return dump(input, &ip.data)
}
func (ip Input) dump(input string) error {
return dump(input, &ip.data)
}
func (rule StreamRule) dump(input string) error {
return dump(input, &rule.data)
}
func (rules StreamRules) dump(input string) error {
return dump(input, &rules.data)
}
func (streams Streams) dump(input string) error {
return dump(input, &streams.data)
}
func (stream Stream) dump(input string) error {
return dump(input, &stream.data)
}
func (db Dashboard) dump(input string) error {
return dump(input, &db.data)
}
func (db Dashboards) dump(input string) error {
return dump(input, &db.data)
}
func (ac StreamAlarmCallbacks) dump(input string) error {
return dump(input, &ac.data)
}
func (ac StreamAlarmCallback) dump(input string) error {
return dump(input, &ac.data)
}
func (ac StreamAlertConditions) dump(input string) error {
return dump(input, &ac.data)
}
func (output Output) dump(input string) error {
return dump(input, &output.data)
}
func (outputs Outputs) dump(input string) error {
return dump(input, &outputs.data)
}
func (notification EventNotification) dump(input string) error {
return dump(input, ¬ification.data)
}
func (notifications EventNotifications) dump(input string) error {
return dump(input, ¬ifications.data)
}
func (definitions EventDefinitions) dump(input string) error {
return dump(input, &definitions.data)
}
func (definition EventDefinition) dump(input string) error {
return dump(input, &definition.data)
}