-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
89 lines (66 loc) · 1.8 KB
/
events.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
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/dustin/go-humanize"
)
func commandEvents(args []string) {
var action string
if len(args) > 0 {
action = args[0]
} else {
action = listAction
}
switch action {
case listAction:
commandEventsList()
case helpAction:
commandEventsHelp(0)
default:
fmt.Println("Error: unkonwn action '" + action + "' for command 'transfers'")
commandEventsHelp(1)
}
}
func commandEventsHelp(exitCode int) {
os.Exit(exitCode)
}
func commandEventsList() {
json, err := putioAPI.Events()
check(err)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprint(w, "Date")
fmt.Fprint(w, "\tEvent")
fmt.Fprint(w, "\tDetails")
fmt.Fprintln(w, "")
events, _ := json.GetObjectArray("events")
for _, event := range events {
eventType, _ := event.GetString("type")
eventDate, _ := event.GetString("created_at")
fmt.Fprintf(w, "%s", eventDate)
switch eventType {
case "transfer_completed":
// keys: created_at, file_id, id, transfer_name, transfer_size, type
transferName, _ := event.GetString("transfer_name")
fmt.Fprint(w, "\tCOMPLETED")
fmt.Fprintf(w, "\t%s", transferName)
case "zip_created":
// Keys: type, zip_id, zip_size, created_at, id
zipID, _ := event.GetInt64("zip_id")
zipSize, _ := event.GetInt64("zip_size")
fmt.Fprint(w, "\tZIPPED ")
fmt.Fprintf(w, "\tID: %d (%s)", zipID, humanize.Bytes(uint64(zipSize)))
default:
fmt.Println("Ahem... It seems you encountered an unsupported event type, sorry !")
fmt.Println("Please open an issue at https://github.com/aanc/go-pio/issues with the following output:")
fmt.Println("Event type: " + eventType)
for key := range event.Map() {
fmt.Println("Key: " + key)
}
fmt.Println("")
}
fmt.Fprintln(w, "")
w.Flush()
}
}