-
Notifications
You must be signed in to change notification settings - Fork 290
/
event.go
68 lines (55 loc) · 1.29 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
package dockercompose
import (
"encoding/json"
"fmt"
"strconv"
)
type Event struct {
Time string `json:"time"` // todo: time
Type Type `json:"type"`
Action string `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
}