@@ -2,10 +2,8 @@ package system
22
33import (
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
9794func (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-
14699func 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,
0 commit comments