From 8f1cebf96f14b80c672544d7d05f1d64722785fb Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Thu, 22 Feb 2024 11:42:27 +0100 Subject: [PATCH] cmd/podman: remove duplicated event ToHumanReadable() ToHumanReadable() exists twice now, there is no reason for this just call the function on the backend event type is fine as this still has to be used there. It also fixes a bug where the wrong event type was passed to the template which did not match the docs and json output. Signed-off-by: Paul Holzinger --- cmd/podman/system/events.go | 57 +++---------------------- docs/source/markdown/podman-events.1.md | 3 +- test/e2e/events_test.go | 5 +-- 3 files changed, 7 insertions(+), 58 deletions(-) diff --git a/cmd/podman/system/events.go b/cmd/podman/system/events.go index 7d7feaebec..ceb056a0d7 100644 --- a/cmd/podman/system/events.go +++ b/cmd/podman/system/events.go @@ -2,10 +2,8 @@ package system import ( "context" - jsonencoding "encoding/json" "fmt" "os" - "time" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" @@ -14,7 +12,6 @@ import ( "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/libpod/events" "github.com/containers/podman/v5/pkg/domain/entities" - "github.com/containers/storage/pkg/stringid" "github.com/spf13/cobra" ) @@ -78,7 +75,7 @@ type Event struct { events.Details } -func newEventFromLibpodEvent(e events.Event) Event { +func newEventFromLibpodEvent(e *events.Event) Event { return Event{ ContainerExitCode: e.ContainerExitCode, ID: e.ID, @@ -95,54 +92,10 @@ func newEventFromLibpodEvent(e events.Event) Event { } func (e *Event) ToJSONString() (string, error) { - b, err := jsonencoding.Marshal(e) + b, err := json.Marshal(e) return string(b), err } -func (e *Event) ToHumanReadable(truncate bool) string { - if e == nil { - return "" - } - var humanFormat string - id := e.ID - if truncate { - id = stringid.TruncateID(id) - } - - timeUnix := time.Unix(0, e.TimeNano) - - switch e.Type { - case events.Container, events.Pod: - humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s", timeUnix, e.Type, e.Status, id, e.Image, e.Name) - if e.PodID != "" { - humanFormat += fmt.Sprintf(", pod_id=%s", e.PodID) - } - if e.HealthStatus != "" { - humanFormat += fmt.Sprintf(", health_status=%s", e.HealthStatus) - } - // check if the container has labels and add it to the output - if len(e.Attributes) > 0 { - for k, v := range e.Attributes { - humanFormat += fmt.Sprintf(", %s=%s", k, v) - } - } - humanFormat += ")" - case events.Network: - humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", timeUnix, e.Type, e.Status, id, id, e.Network) - case events.Image: - humanFormat = fmt.Sprintf("%s %s %s %s %s", timeUnix, e.Type, e.Status, id, e.Name) - case events.System: - if e.Name != "" { - humanFormat = fmt.Sprintf("%s %s %s %s", timeUnix, e.Type, e.Status, e.Name) - } else { - humanFormat = fmt.Sprintf("%s %s %s", timeUnix, e.Type, e.Status) - } - case events.Volume, events.Machine: - humanFormat = fmt.Sprintf("%s %s %s %s", timeUnix, e.Type, e.Status, e.Name) - } - return humanFormat -} - func init() { registry.Commands = append(registry.Commands, registry.CliCommand{ Command: systemEventsCommand, @@ -217,20 +170,20 @@ func eventsCmd(cmd *cobra.Command, _ []string) error { // channel was closed we can exit return nil } - e := newEventFromLibpodEvent(*event) switch { case doJSON: + e := newEventFromLibpodEvent(event) jsonStr, err := e.ToJSONString() if err != nil { return err } fmt.Println(jsonStr) case cmd.Flags().Changed("format"): - if err := rpt.Execute(event); err != nil { + if err := rpt.Execute(newEventFromLibpodEvent(event)); err != nil { return err } default: - fmt.Println(e.ToHumanReadable(!noTrunc)) + fmt.Println(event.ToHumanReadable(!noTrunc)) } case err := <-errChannel: // only exit in case of an error, diff --git a/docs/source/markdown/podman-events.1.md b/docs/source/markdown/podman-events.1.md index 9e521c1546..0267456848 100644 --- a/docs/source/markdown/podman-events.1.md +++ b/docs/source/markdown/podman-events.1.md @@ -116,9 +116,8 @@ Format the output to JSON Lines or using the given Go template. | .Network | Name of network being used (string) | | .PodID | ID of pod associated with container, if any | | .Status | Event status (e.g., create, start, died, ...) | -| .Time ... | Event timestamp (string) | +| .Time | Event timestamp (string) | | .TimeNano | Event timestamp with nanosecond precision (int64) | -| .ToHumanReadable *bool* | If true, truncates CID in output | | .Type | Event type (e.g., image, container, pod, ...) | #### **--help** diff --git a/test/e2e/events_test.go b/test/e2e/events_test.go index a947be35dd..63ca8f239c 100644 --- a/test/e2e/events_test.go +++ b/test/e2e/events_test.go @@ -141,7 +141,7 @@ var _ = Describe("Podman events", func() { "--stream=false", "--since", strconv.FormatInt(start.Unix(), 10), "--filter", fmt.Sprintf("container=%s", ctrName), - "--format", "{{json.}}", + "--format", "{{json .}}", }) test.WaitWithDefaultTimeout() @@ -160,9 +160,6 @@ var _ = Describe("Podman events", func() { Expect(event.TimeNano).To(BeNumerically("<=", end.UnixNano())) Expect(time.Unix(0, event.TimeNano).Unix()).To(BeEquivalentTo(event.Time)) - date := time.Unix(0, event.TimeNano).Format("2006-01-02") - Expect(event.ToHumanReadable(false)).To(HavePrefix(date)) - test = podmanTest.Podman([]string{"events", "--stream=false", "--filter=type=container", "--format", "ID: {{.ID}}"}) test.WaitWithDefaultTimeout() Expect(test).To(ExitCleanly())