mirror of
https://github.com/containers/podman.git
synced 2025-08-06 19:44:14 +08:00
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>
This commit is contained in:
@ -2,10 +2,8 @@ package system
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
jsonencoding "encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/containers/common/pkg/completion"
|
"github.com/containers/common/pkg/completion"
|
||||||
"github.com/containers/common/pkg/report"
|
"github.com/containers/common/pkg/report"
|
||||||
@ -14,7 +12,6 @@ import (
|
|||||||
"github.com/containers/podman/v5/cmd/podman/validate"
|
"github.com/containers/podman/v5/cmd/podman/validate"
|
||||||
"github.com/containers/podman/v5/libpod/events"
|
"github.com/containers/podman/v5/libpod/events"
|
||||||
"github.com/containers/podman/v5/pkg/domain/entities"
|
"github.com/containers/podman/v5/pkg/domain/entities"
|
||||||
"github.com/containers/storage/pkg/stringid"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -78,7 +75,7 @@ type Event struct {
|
|||||||
events.Details
|
events.Details
|
||||||
}
|
}
|
||||||
|
|
||||||
func newEventFromLibpodEvent(e events.Event) Event {
|
func newEventFromLibpodEvent(e *events.Event) Event {
|
||||||
return Event{
|
return Event{
|
||||||
ContainerExitCode: e.ContainerExitCode,
|
ContainerExitCode: e.ContainerExitCode,
|
||||||
ID: e.ID,
|
ID: e.ID,
|
||||||
@ -95,54 +92,10 @@ func newEventFromLibpodEvent(e events.Event) Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) ToJSONString() (string, error) {
|
func (e *Event) ToJSONString() (string, error) {
|
||||||
b, err := jsonencoding.Marshal(e)
|
b, err := json.Marshal(e)
|
||||||
return string(b), err
|
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() {
|
func init() {
|
||||||
registry.Commands = append(registry.Commands, registry.CliCommand{
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
||||||
Command: systemEventsCommand,
|
Command: systemEventsCommand,
|
||||||
@ -217,20 +170,20 @@ func eventsCmd(cmd *cobra.Command, _ []string) error {
|
|||||||
// channel was closed we can exit
|
// channel was closed we can exit
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
e := newEventFromLibpodEvent(*event)
|
|
||||||
switch {
|
switch {
|
||||||
case doJSON:
|
case doJSON:
|
||||||
|
e := newEventFromLibpodEvent(event)
|
||||||
jsonStr, err := e.ToJSONString()
|
jsonStr, err := e.ToJSONString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(jsonStr)
|
fmt.Println(jsonStr)
|
||||||
case cmd.Flags().Changed("format"):
|
case cmd.Flags().Changed("format"):
|
||||||
if err := rpt.Execute(event); err != nil {
|
if err := rpt.Execute(newEventFromLibpodEvent(event)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Println(e.ToHumanReadable(!noTrunc))
|
fmt.Println(event.ToHumanReadable(!noTrunc))
|
||||||
}
|
}
|
||||||
case err := <-errChannel:
|
case err := <-errChannel:
|
||||||
// only exit in case of an error,
|
// only exit in case of an error,
|
||||||
|
@ -116,9 +116,8 @@ Format the output to JSON Lines or using the given Go template.
|
|||||||
| .Network | Name of network being used (string) |
|
| .Network | Name of network being used (string) |
|
||||||
| .PodID | ID of pod associated with container, if any |
|
| .PodID | ID of pod associated with container, if any |
|
||||||
| .Status | Event status (e.g., create, start, died, ...) |
|
| .Status | Event status (e.g., create, start, died, ...) |
|
||||||
| .Time ... | Event timestamp (string) |
|
| .Time | Event timestamp (string) |
|
||||||
| .TimeNano | Event timestamp with nanosecond precision (int64) |
|
| .TimeNano | Event timestamp with nanosecond precision (int64) |
|
||||||
| .ToHumanReadable *bool* | If true, truncates CID in output |
|
|
||||||
| .Type | Event type (e.g., image, container, pod, ...) |
|
| .Type | Event type (e.g., image, container, pod, ...) |
|
||||||
|
|
||||||
#### **--help**
|
#### **--help**
|
||||||
|
@ -141,7 +141,7 @@ var _ = Describe("Podman events", func() {
|
|||||||
"--stream=false",
|
"--stream=false",
|
||||||
"--since", strconv.FormatInt(start.Unix(), 10),
|
"--since", strconv.FormatInt(start.Unix(), 10),
|
||||||
"--filter", fmt.Sprintf("container=%s", ctrName),
|
"--filter", fmt.Sprintf("container=%s", ctrName),
|
||||||
"--format", "{{json.}}",
|
"--format", "{{json .}}",
|
||||||
})
|
})
|
||||||
|
|
||||||
test.WaitWithDefaultTimeout()
|
test.WaitWithDefaultTimeout()
|
||||||
@ -160,9 +160,6 @@ var _ = Describe("Podman events", func() {
|
|||||||
Expect(event.TimeNano).To(BeNumerically("<=", end.UnixNano()))
|
Expect(event.TimeNano).To(BeNumerically("<=", end.UnixNano()))
|
||||||
Expect(time.Unix(0, event.TimeNano).Unix()).To(BeEquivalentTo(event.Time))
|
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 = podmanTest.Podman([]string{"events", "--stream=false", "--filter=type=container", "--format", "ID: {{.ID}}"})
|
||||||
test.WaitWithDefaultTimeout()
|
test.WaitWithDefaultTimeout()
|
||||||
Expect(test).To(ExitCleanly())
|
Expect(test).To(ExitCleanly())
|
||||||
|
Reference in New Issue
Block a user