summaryrefslogtreecommitdiff
path: root/cmd/podman/system/events.go
blob: 6aae62dc09174dc69e5a2928f525332b9a225be3 (plain)
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
package system

import (
	"bufio"
	"context"
	"html/template"
	"os"

	"github.com/containers/buildah/pkg/formats"
	"github.com/containers/libpod/cmd/podman/registry"
	"github.com/containers/libpod/cmd/podman/validate"
	"github.com/containers/libpod/libpod/events"
	"github.com/containers/libpod/pkg/domain/entities"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	eventsDescription = "Monitor podman events"
	eventsCommand     = &cobra.Command{
		Use:   "events",
		Args:  validate.NoArgs,
		Short: "Show podman events",
		Long:  eventsDescription,
		RunE:  eventsCmd,
		Example: `podman events
  podman events --filter event=create
  podman events --since 1h30s`,
	}
)

var (
	eventOptions entities.EventsOptions
	eventFormat  string
)

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
		Command: eventsCommand,
	})
	flags := eventsCommand.Flags()
	flags.StringArrayVar(&eventOptions.Filter, "filter", []string{}, "filter output")
	flags.StringVar(&eventFormat, "format", "", "format the output using a Go template")
	flags.BoolVar(&eventOptions.Stream, "stream", true, "stream new events; for testing only")
	flags.StringVar(&eventOptions.Since, "since", "", "show all events created since timestamp")
	flags.StringVar(&eventOptions.Until, "until", "", "show all events until timestamp")
	_ = flags.MarkHidden("stream")
}

func eventsCmd(cmd *cobra.Command, args []string) error {
	var (
		err         error
		eventsError error
		tmpl        *template.Template
	)
	if eventFormat != formats.JSONString {
		tmpl, err = template.New("events").Parse(eventFormat)
		if err != nil {
			return err
		}
	}
	if len(eventOptions.Since) > 0 || len(eventOptions.Until) > 0 {
		eventOptions.FromStart = true
	}
	eventChannel := make(chan *events.Event)
	eventOptions.EventChan = eventChannel

	go func() {
		eventsError = registry.ContainerEngine().Events(context.Background(), eventOptions)
	}()
	if eventsError != nil {
		return eventsError
	}

	w := bufio.NewWriter(os.Stdout)
	for event := range eventChannel {
		switch {
		case eventFormat == formats.JSONString:
			jsonStr, err := event.ToJSONString()
			if err != nil {
				return errors.Wrapf(err, "unable to format json")
			}
			if _, err := w.Write([]byte(jsonStr)); err != nil {
				return err
			}
		case len(eventFormat) > 0:
			if err := tmpl.Execute(w, event); err != nil {
				return err
			}
		default:
			if _, err := w.Write([]byte(event.ToHumanReadable())); err != nil {
				return err
			}
		}
		if _, err := w.Write([]byte("\n")); err != nil {
			return err
		}
		if err := w.Flush(); err != nil {
			return err
		}
	}
	return nil
}