-
Notifications
You must be signed in to change notification settings - Fork 303
/
event.go
151 lines (130 loc) · 3.01 KB
/
event.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
package dockercompose
import (
"encoding/json"
"fmt"
"strconv"
)
type Event struct {
Time string `json:"time"` // todo: time
Type Type `json:"type"`
Action Action `json:"action"`
ID string `json:"id"` // todo: type?
Service string `json:"service"`
Attributes Attributes `json:"attributes"`
}
type Attributes struct {
Name string `json:"name"`
Image string `json:"image"`
}
func EventFromJsonStr(j string) (Event, error) {
var evt Event
b := []byte(j)
err := json.Unmarshal(b, &evt)
return evt, err
}
// https://docs.docker.com/engine/reference/commandline/events/
type Type int
const (
// Add 'types' here (and to `stringToType` below) as we support them
TypeUnknown Type = iota
TypeContainer
)
var stringToType = map[string]Type{
"container": TypeContainer,
}
func (t Type) String() string {
for str, typ := range stringToType {
if typ == t {
return str
}
}
return "unknown"
}
func (t Type) MarshalJSON() ([]byte, error) {
s := t.String()
return []byte(fmt.Sprintf("%q", s)), nil
}
func (t *Type) UnmarshalJSON(b []byte) error {
s := string(b)
if unquoted, err := strconv.Unquote(s); err == nil {
s = unquoted
}
typ := stringToType[s] // if type not in map, this returns 0 (i.e. TypeUnknown)
*t = typ
return nil
}
type Action int
const (
// Add 'actions' here (and to `stringToAction` below`) as we support them
// CONTAINER actions
ActionUnknown Action = iota
ActionAttach
ActionCommit
ActionCopy
ActionCreate
ActionDestroy
ActionDie
ActionExecCreate
ActionExecDetach
ActionExecDie
ActionExecStart
ActionExport
ActionHealthStatus
ActionKill
ActionOom
ActionPause
ActionRename
ActionResize
ActionRestart
ActionStart
ActionStop
ActionTop
ActionUnpause
ActionUpdate
)
var stringToAction = map[string]Action{
"attach": ActionAttach,
"commit": ActionCommit,
"copy": ActionCopy,
"create": ActionCreate,
"destroy": ActionDestroy,
"die": ActionDie,
"exec_create": ActionExecCreate,
"exec_detach": ActionExecDetach,
"exec_die": ActionExecDie,
"exec_start": ActionExecStart,
"export": ActionExport,
"health_status": ActionHealthStatus,
"kill": ActionKill,
"oom": ActionOom,
"pause": ActionPause,
"rename": ActionRename,
"resize": ActionResize,
"restart": ActionRestart,
"start": ActionStart,
"stop": ActionStop,
"top": ActionTop,
"unpause": ActionUnpause,
"update": ActionUpdate,
}
func (a Action) String() string {
for str, act := range stringToAction {
if act == a {
return str
}
}
return "unknown"
}
func (a Action) MarshalJSON() ([]byte, error) {
s := a.String()
return []byte(fmt.Sprintf("%q", s)), nil
}
func (a *Action) UnmarshalJSON(b []byte) error {
s := string(b)
if unquoted, err := strconv.Unquote(s); err == nil {
s = unquoted
}
action := stringToAction[s] // if action not in map, this returns 0 (i.e. ActionUnknown)
*a = action
return nil
}