diff options
author | Jhon Honce <jhonce@redhat.com> | 2020-10-09 09:13:22 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2020-10-13 17:28:45 -0700 |
commit | eb4a746efcb9e76e29942461b97da797fd67109f (patch) | |
tree | 980663ba01f79bd107980fd0c3205fe833503a41 /cmd/podman/system/events.go | |
parent | 7ad631b819d991559ba20e4728a6803a2546158f (diff) | |
download | podman-eb4a746efcb9e76e29942461b97da797fd67109f.tar.gz podman-eb4a746efcb9e76e29942461b97da797fd67109f.tar.bz2 podman-eb4a746efcb9e76e29942461b97da797fd67109f.zip |
Restore --format table support
* system df
* events
* fix error handling from go routine
* update tests to use gomega matchers for better error messages
* system info
* version
* volume inspect
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd/podman/system/events.go')
-rw-r--r-- | cmd/podman/system/events.go | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/cmd/podman/system/events.go b/cmd/podman/system/events.go index 04e948f30..aaf572873 100644 --- a/cmd/podman/system/events.go +++ b/cmd/podman/system/events.go @@ -1,13 +1,12 @@ package system import ( - "bufio" "context" + "fmt" "os" - "strings" "text/template" - "github.com/containers/buildah/pkg/formats" + "github.com/containers/podman/v2/cmd/podman/parse" "github.com/containers/podman/v2/cmd/podman/registry" "github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/libpod/events" @@ -28,6 +27,7 @@ var ( RunE: eventsCmd, Example: `podman events podman events --filter event=create + podman events --format {{.Image}} podman events --since 1h30s`, } ) @@ -51,60 +51,54 @@ func init() { _ = flags.MarkHidden("stream") } -func eventsCmd(cmd *cobra.Command, args []string) error { - var ( - err error - eventsError error - tmpl *template.Template - ) - if strings.Join(strings.Fields(eventFormat), "") == "{{json.}}" { - eventFormat = formats.JSONString - } - if eventFormat != formats.JSONString { - tmpl, err = template.New("events").Parse(eventFormat) - if err != nil { - return err - } - } +func eventsCmd(cmd *cobra.Command, _ []string) error { if len(eventOptions.Since) > 0 || len(eventOptions.Until) > 0 { eventOptions.FromStart = true } - eventChannel := make(chan *events.Event) + eventChannel := make(chan *events.Event, 1) eventOptions.EventChan = eventChannel + errChannel := make(chan error) + + var ( + tmpl *template.Template + doJSON bool + ) + + if cmd.Flags().Changed("format") { + doJSON = parse.MatchesJSONFormat(eventFormat) + if !doJSON { + var err error + tmpl, err = template.New("events").Parse(eventFormat) + if err != nil { + return err + } + } + } go func() { - eventsError = registry.ContainerEngine().Events(context.Background(), eventOptions) + err := registry.ContainerEngine().Events(context.Background(), eventOptions) + errChannel <- err }() - if eventsError != nil { - return eventsError - } - w := bufio.NewWriter(os.Stdout) for event := range eventChannel { switch { - case eventFormat == formats.JSONString: + case event == nil: + // no-op + case doJSON: 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 { + fmt.Println(jsonStr) + case cmd.Flags().Changed("format"): + if err := tmpl.Execute(os.Stdout, event); err != nil { return err } + fmt.Println("") 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 + fmt.Println(event.ToHumanReadable()) } } - return nil + + return <-errChannel } |