github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/dockercompose/event.go (about) 1 package dockercompose 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strconv" 7 ) 8 9 type Event struct { 10 Time string `json:"time"` // todo: time 11 Type Type `json:"type"` 12 Action string `json:"action"` 13 ID string `json:"id"` // todo: type? 14 Service string `json:"service"` 15 Attributes Attributes `json:"attributes"` 16 } 17 18 type Attributes struct { 19 Name string `json:"name"` 20 Image string `json:"image"` 21 } 22 23 func EventFromJsonStr(j string) (Event, error) { 24 var evt Event 25 26 b := []byte(j) 27 err := json.Unmarshal(b, &evt) 28 29 return evt, err 30 } 31 32 // https://docs.docker.com/engine/reference/commandline/events/ 33 type Type int 34 35 const ( 36 // Add 'types' here (and to `stringToType` below) as we support them 37 TypeUnknown Type = iota 38 TypeContainer 39 ) 40 41 var stringToType = map[string]Type{ 42 "container": TypeContainer, 43 } 44 45 func (t Type) String() string { 46 for str, typ := range stringToType { 47 if typ == t { 48 return str 49 } 50 } 51 return "unknown" 52 } 53 54 func (t Type) MarshalJSON() ([]byte, error) { 55 s := t.String() 56 return []byte(fmt.Sprintf("%q", s)), nil 57 } 58 59 func (t *Type) UnmarshalJSON(b []byte) error { 60 s := string(b) 61 if unquoted, err := strconv.Unquote(s); err == nil { 62 s = unquoted 63 } 64 65 typ := stringToType[s] // if type not in map, this returns 0 (i.e. TypeUnknown) 66 *t = typ 67 return nil 68 }