Skip to content

Commit 8f1cebf

Browse files
committed
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 <pholzing@redhat.com>
1 parent a3a1b44 commit 8f1cebf

3 files changed

Lines changed: 7 additions & 58 deletions

File tree

cmd/podman/system/events.go

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package system
22

33
import (
44
"context"
5-
jsonencoding "encoding/json"
65
"fmt"
76
"os"
8-
"time"
97

108
"github.com/containers/common/pkg/completion"
119
"github.com/containers/common/pkg/report"
@@ -14,7 +12,6 @@ import (
1412
"github.com/containers/podman/v5/cmd/podman/validate"
1513
"github.com/containers/podman/v5/libpod/events"
1614
"github.com/containers/podman/v5/pkg/domain/entities"
17-
"github.com/containers/storage/pkg/stringid"
1815
"github.com/spf13/cobra"
1916
)
2017

@@ -78,7 +75,7 @@ type Event struct {
7875
events.Details
7976
}
8077

81-
func newEventFromLibpodEvent(e events.Event) Event {
78+
func newEventFromLibpodEvent(e *events.Event) Event {
8279
return Event{
8380
ContainerExitCode: e.ContainerExitCode,
8481
ID: e.ID,
@@ -95,54 +92,10 @@ func newEventFromLibpodEvent(e events.Event) Event {
9592
}
9693

9794
func (e *Event) ToJSONString() (string, error) {
98-
b, err := jsonencoding.Marshal(e)
95+
b, err := json.Marshal(e)
9996
return string(b), err
10097
}
10198

102-
func (e *Event) ToHumanReadable(truncate bool) string {
103-
if e == nil {
104-
return ""
105-
}
106-
var humanFormat string
107-
id := e.ID
108-
if truncate {
109-
id = stringid.TruncateID(id)
110-
}
111-
112-
timeUnix := time.Unix(0, e.TimeNano)
113-
114-
switch e.Type {
115-
case events.Container, events.Pod:
116-
humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s", timeUnix, e.Type, e.Status, id, e.Image, e.Name)
117-
if e.PodID != "" {
118-
humanFormat += fmt.Sprintf(", pod_id=%s", e.PodID)
119-
}
120-
if e.HealthStatus != "" {
121-
humanFormat += fmt.Sprintf(", health_status=%s", e.HealthStatus)
122-
}
123-
// check if the container has labels and add it to the output
124-
if len(e.Attributes) > 0 {
125-
for k, v := range e.Attributes {
126-
humanFormat += fmt.Sprintf(", %s=%s", k, v)
127-
}
128-
}
129-
humanFormat += ")"
130-
case events.Network:
131-
humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", timeUnix, e.Type, e.Status, id, id, e.Network)
132-
case events.Image:
133-
humanFormat = fmt.Sprintf("%s %s %s %s %s", timeUnix, e.Type, e.Status, id, e.Name)
134-
case events.System:
135-
if e.Name != "" {
136-
humanFormat = fmt.Sprintf("%s %s %s %s", timeUnix, e.Type, e.Status, e.Name)
137-
} else {
138-
humanFormat = fmt.Sprintf("%s %s %s", timeUnix, e.Type, e.Status)
139-
}
140-
case events.Volume, events.Machine:
141-
humanFormat = fmt.Sprintf("%s %s %s %s", timeUnix, e.Type, e.Status, e.Name)
142-
}
143-
return humanFormat
144-
}
145-
14699
func init() {
147100
registry.Commands = append(registry.Commands, registry.CliCommand{
148101
Command: systemEventsCommand,
@@ -217,20 +170,20 @@ func eventsCmd(cmd *cobra.Command, _ []string) error {
217170
// channel was closed we can exit
218171
return nil
219172
}
220-
e := newEventFromLibpodEvent(*event)
221173
switch {
222174
case doJSON:
175+
e := newEventFromLibpodEvent(event)
223176
jsonStr, err := e.ToJSONString()
224177
if err != nil {
225178
return err
226179
}
227180
fmt.Println(jsonStr)
228181
case cmd.Flags().Changed("format"):
229-
if err := rpt.Execute(event); err != nil {
182+
if err := rpt.Execute(newEventFromLibpodEvent(event)); err != nil {
230183
return err
231184
}
232185
default:
233-
fmt.Println(e.ToHumanReadable(!noTrunc))
186+
fmt.Println(event.ToHumanReadable(!noTrunc))
234187
}
235188
case err := <-errChannel:
236189
// only exit in case of an error,

docs/source/markdown/podman-events.1.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,8 @@ Format the output to JSON Lines or using the given Go template.
116116
| .Network | Name of network being used (string) |
117117
| .PodID | ID of pod associated with container, if any |
118118
| .Status | Event status (e.g., create, start, died, ...) |
119-
| .Time ... | Event timestamp (string) |
119+
| .Time | Event timestamp (string) |
120120
| .TimeNano | Event timestamp with nanosecond precision (int64) |
121-
| .ToHumanReadable *bool* | If true, truncates CID in output |
122121
| .Type | Event type (e.g., image, container, pod, ...) |
123122

124123
#### **--help**

test/e2e/events_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ var _ = Describe("Podman events", func() {
141141
"--stream=false",
142142
"--since", strconv.FormatInt(start.Unix(), 10),
143143
"--filter", fmt.Sprintf("container=%s", ctrName),
144-
"--format", "{{json.}}",
144+
"--format", "{{json .}}",
145145
})
146146

147147
test.WaitWithDefaultTimeout()
@@ -160,9 +160,6 @@ var _ = Describe("Podman events", func() {
160160
Expect(event.TimeNano).To(BeNumerically("<=", end.UnixNano()))
161161
Expect(time.Unix(0, event.TimeNano).Unix()).To(BeEquivalentTo(event.Time))
162162

163-
date := time.Unix(0, event.TimeNano).Format("2006-01-02")
164-
Expect(event.ToHumanReadable(false)).To(HavePrefix(date))
165-
166163
test = podmanTest.Podman([]string{"events", "--stream=false", "--filter=type=container", "--format", "ID: {{.ID}}"})
167164
test.WaitWithDefaultTimeout()
168165
Expect(test).To(ExitCleanly())

0 commit comments

Comments
 (0)