forked from docker/libcompose
/
events.go
56 lines (48 loc) · 1.21 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
package app
import (
"encoding/json"
"fmt"
"os"
"strings"
"golang.org/x/net/context"
"github.com/Sirupsen/logrus"
"github.com/docker/libcompose/project"
"github.com/docker/libcompose/project/events"
"github.com/urfave/cli"
)
// ProjectEvents listen for real-time events of containers.
func ProjectEvents(p project.APIProject, c *cli.Context) error {
evts, err := p.Events(context.Background(), c.Args()...)
if err != nil {
return err
}
var printfn func(events.ContainerEvent)
if c.Bool("json") {
printfn = printJSON
} else {
printfn = printStd
}
for event := range evts {
printfn(event)
}
return nil
}
func printStd(event events.ContainerEvent) {
output := os.Stdout
fmt.Fprintf(output, "%s ", event.Time.Format("2006-01-02 15:04:05.999999999"))
fmt.Fprintf(output, "%s %s %s", event.Type, event.Event, event.ID)
attrs := []string{}
for attr, value := range event.Attributes {
attrs = append(attrs, fmt.Sprintf("%s=%s", attr, value))
}
fmt.Fprintf(output, " (%s)", strings.Join(attrs, ", "))
fmt.Fprint(output, "\n")
}
func printJSON(event events.ContainerEvent) {
json, err := json.Marshal(event)
if err != nil {
logrus.Warn(err)
}
output := os.Stdout
fmt.Fprintf(output, "%s", json)
}