-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
367 lines (327 loc) · 9.7 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"log"
"os"
"strings"
"github.com/dhrapson/sched-load/controller"
"github.com/dhrapson/sched-load/iaas"
"github.com/urfave/cli"
)
var (
region string
clientId string
filePath string
force bool
)
func main() {
app := cli.NewApp()
app.Name = "sched-load"
app.Usage = "uploads files to public IaaS & publishes a schedule for regular file uploads"
flags := []cli.Flag{
cli.StringFlag{
Name: "region, r",
Usage: "public IaaS region for storing the files",
Destination: ®ion,
},
cli.StringFlag{
Name: "client, c",
Usage: "identifier for the client",
Destination: &clientId,
},
}
app.Commands = []cli.Command{
{
Name: "status",
Aliases: []string{"st"},
Usage: "show status of connection and schedule",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
ctrler := controller.Controller{Client: iaasClient}
details, err := ctrler.Status()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
log.Println("connected to IaaS")
log.Println("Credential Type: " + details["CredentialType"])
if details["ClientId"] == "" {
log.Println("Client ID: none set")
} else {
log.Println("Client ID: " + details["ClientId"])
}
log.Println("Integrator ID: " + details["IntegratorId"])
log.Println("Account ID: " + details["AccountId"])
return nil
},
},
{
Name: "client",
Aliases: []string{"c"},
Usage: "manage client accounts",
Subcommands: []cli.Command{
{
Name: "delete",
Aliases: []string{"d"},
Usage: "remove a client account, optionally also remove all the uploaded data files",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "force deletion of the user accounts uploaded files",
Destination: &force,
},
},
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
wasPreExisting, err := controller.DeleteClientUser(force)
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if wasPreExisting {
if force {
log.Printf("deleted account %s, left data files in place\n", clientId)
} else {
log.Printf("deleted account %s & all data files\n", clientId)
}
} else {
log.Printf("%s account did not exist\n", clientId)
if force {
log.Printf("removed any data files for account %s\n", clientId)
}
}
return nil
},
},
{
Name: "create",
Aliases: []string{"add"},
Usage: "create a client account",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
creds, err := controller.CreateClientUser()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
log.Printf("created account %s\n", clientId)
log.Printf("Credentials are %s\n", creds.String())
return nil
},
},
},
},
{
Name: "data-file",
Aliases: []string{"df"},
Usage: "manage data files",
Subcommands: []cli.Command{
{
Name: "delete",
Aliases: []string{"d"},
Usage: "remove a remote data file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "remote, r",
Usage: "remote file path",
Destination: &filePath,
},
},
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
wasPreExisting, err := controller.DeleteDataFile(filePath)
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if wasPreExisting {
log.Printf("deleted %s\n", filePath)
} else {
log.Printf("%s did not exist\n", filePath)
}
return nil
},
},
{
Name: "list-uploaded",
Aliases: []string{"lu"},
Usage: "list remote unprocessed data files",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
files, err := controller.ListDataFiles()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
var filesList string
if len(files) == 0 {
filesList = "\tnone found"
}
for _, filePath := range files {
filesList += "\t" + filePath + "\n"
}
log.Printf("listing files:\n%s", filesList)
return nil
},
},
{
Name: "upload",
Aliases: []string{"u"},
Usage: "upload a data file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "path to the local file",
Destination: &filePath,
},
},
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
if fileName, err := controller.UploadDataFile(filePath); err != nil {
log.Fatalf("Error: %s\n", err.Error())
} else {
log.Printf("uploaded %s\n", fileName)
}
return nil
},
},
},
},
{
Name: "immediate-collection",
Aliases: []string{"ic"},
Usage: "enable/disable immediate collection of uploaded data files",
Subcommands: []cli.Command{
{
Name: "status",
Usage: "show the immediate data file collection status",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
status, err := controller.ImmediateDataFileCollectionStatus()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if status {
log.Println("Immediate collection status is enabled")
} else {
log.Println("Immediate collection status is disabled")
}
return nil
},
},
{
Name: "enable",
Usage: "enable immediate data file collection",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
wasNewlySet, err := controller.EnableImmediateDataFileCollection()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if wasNewlySet {
log.Println("Enabled immediate collection")
} else {
log.Println("Immediate collection was already enabled")
}
return nil
},
},
{
Name: "disable",
Usage: "disable immediate data file collection",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
wasPreExisting, err := controller.DisableImmediateDataFileCollection()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if wasPreExisting {
log.Println("Disabled immediate collection")
} else {
log.Println("Immediate collection was already disabled")
}
return nil
},
},
},
},
{
Name: "schedule",
Aliases: []string{"sc"},
Usage: "set a schedule for collection of uploaded data files",
Subcommands: []cli.Command{
{
Name: "status",
Usage: "show the schedule status",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
if schedule, err := controller.GetSchedule(); err != nil {
log.Fatalf("Error: %s\n", err.Error())
} else {
log.Println("existing schedule: " + schedule)
}
return nil
},
},
{
Name: "daily",
Usage: "set a daily schedule",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
wasPreExisting, err := controller.SetSchedule("DAILY")
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if wasPreExisting {
log.Println("Set daily schedule")
} else {
log.Println("Daily schedule was already set")
}
return nil
},
},
{
Name: "none",
Usage: "remove schedule",
Action: func(c *cli.Context) error {
clientId = strings.ToLower(clientId)
iaasClient := iaas.AwsClient{Region: region, ClientId: clientId}
controller := controller.Controller{Client: iaasClient}
wasPreExisting, err := controller.RemoveSchedule()
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}
if wasPreExisting {
log.Println("Removed schedule")
} else {
log.Println("No schedule existed to remove")
}
return nil
},
},
},
},
}
app.Flags = flags
app.CommandNotFound = func(c *cli.Context, command string) {
log.Printf("Invalid command '%s'\n\n", command)
cli.ShowAppHelp(c)
os.Exit(1)
}
app.Run(os.Args)
}