Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/command/builder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewBuilderCommand(dockerCli command.Cli) *cobra.Command {
Annotations: map[string]string{"version": "1.31"},
}
cmd.AddCommand(
newEventsCommand(dockerCli),
NewPruneCommand(dockerCli),
image.NewBuildCommand(dockerCli),
)
Expand Down
13 changes: 13 additions & 0 deletions cli/command/builder/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package builder

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker builder events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.BuilderEventType)
}
1 change: 1 addition & 0 deletions cli/command/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewConfigCommand(dockerCli command.Cli) *cobra.Command {
cmd.AddCommand(
newConfigListCommand(dockerCli),
newConfigCreateCommand(dockerCli),
newConfigEventsCommand(dockerCli),
newConfigInspectCommand(dockerCli),
newConfigRemoveCommand(dockerCli),
)
Expand Down
13 changes: 13 additions & 0 deletions cli/command/config/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newConfigEventsCommand creates a new cobra.Command for `docker secret events`
func newConfigEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.ConfigEventType)
}
1 change: 1 addition & 0 deletions cli/command/container/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewContainerCommand(dockerCli command.Cli) *cobra.Command {
NewCopyCommand(dockerCli),
NewCreateCommand(dockerCli),
NewDiffCommand(dockerCli),
newEventsCommand(dockerCli),
NewExecCommand(dockerCli),
NewExportCommand(dockerCli),
NewKillCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/container/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package container

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker container events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.ContainerEventType)
}
1 change: 1 addition & 0 deletions cli/command/image/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewImageCommand(dockerCli command.Cli) *cobra.Command {
}
cmd.AddCommand(
NewBuildCommand(dockerCli),
newEventsCommand(dockerCli),
NewHistoryCommand(dockerCli),
NewImportCommand(dockerCli),
NewLoadCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/image/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package image

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker image events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.ImageEventType)
}
1 change: 1 addition & 0 deletions cli/command/network/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewNetworkCommand(dockerCli command.Cli) *cobra.Command {
newConnectCommand(dockerCli),
newCreateCommand(dockerCli),
newDisconnectCommand(dockerCli),
newEventsCommand(dockerCli),
newInspectCommand(dockerCli),
newListCommand(dockerCli),
newRemoveCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/network/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package network

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker network events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.NetworkEventType)
}
1 change: 1 addition & 0 deletions cli/command/node/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewNodeCommand(dockerCli command.Cli) *cobra.Command {
}
cmd.AddCommand(
newDemoteCommand(dockerCli),
newEventsCommand(dockerCli),
newInspectCommand(dockerCli),
newListCommand(dockerCli),
newPromoteCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/node/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package node

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker node events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.NodeEventType)
}
1 change: 1 addition & 0 deletions cli/command/plugin/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewPluginCommand(dockerCli command.Cli) *cobra.Command {
cmd.AddCommand(
newDisableCommand(dockerCli),
newEnableCommand(dockerCli),
newEventsCommand(dockerCli),
newInspectCommand(dockerCli),
newInstallCommand(dockerCli),
newListCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/plugin/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package plugin

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker plugin events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.PluginEventType)
}
1 change: 1 addition & 0 deletions cli/command/secret/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewSecretCommand(dockerCli command.Cli) *cobra.Command {
cmd.AddCommand(
newSecretListCommand(dockerCli),
newSecretCreateCommand(dockerCli),
newSecretEventsCommand(dockerCli),
newSecretInspectCommand(dockerCli),
newSecretRemoveCommand(dockerCli),
)
Expand Down
13 changes: 13 additions & 0 deletions cli/command/secret/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package secret

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newSecretEventsCommand creates a new cobra.Command for `docker secret events`
func newSecretEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.SecretEventType)
}
1 change: 1 addition & 0 deletions cli/command/service/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command {
}
cmd.AddCommand(
newCreateCommand(dockerCli),
newEventsCommand(dockerCli),
newInspectCommand(dockerCli),
newPsCommand(dockerCli),
newListCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/service/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package service

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker service events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.ServiceEventType)
}
122 changes: 4 additions & 118 deletions cli/command/system/events.go
Original file line number Diff line number Diff line change
@@ -1,143 +1,29 @@
package system

import (
"context"
"fmt"
"io"
"sort"
"strings"
"text/template"
"time"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/events"
"github.com/docker/cli/opts"
"github.com/docker/cli/templates"
"github.com/docker/docker/api/types"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

type eventsOptions struct {
since string
until string
filter opts.FilterOpt
format string
}

// NewEventsCommand creates a new cobra.Command for `docker events`
func NewEventsCommand(dockerCli command.Cli) *cobra.Command {
options := eventsOptions{filter: opts.NewFilterOpt()}
options := events.Options{Filter: opts.NewFilterOpt()}

cmd := &cobra.Command{
Use: "events [OPTIONS]",
Short: "Get real time events from the server",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runEvents(dockerCli, &options)
return events.Run(dockerCli, &options)
},
ValidArgsFunction: completion.NoComplete,
}

flags := cmd.Flags()
flags.StringVar(&options.since, "since", "", "Show all events created since timestamp")
flags.StringVar(&options.until, "until", "", "Stream events until this timestamp")
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
flags.StringVar(&options.format, "format", "", "Format the output using the given Go template")

events.InstallFlags(flags, &options)
return cmd
}

func runEvents(dockerCli command.Cli, options *eventsOptions) error {
tmpl, err := makeTemplate(options.format)
if err != nil {
return cli.StatusError{
StatusCode: 64,
Status: "Error parsing format: " + err.Error()}
}
eventOptions := types.EventsOptions{
Since: options.since,
Until: options.until,
Filters: options.filter.Value(),
}

ctx, cancel := context.WithCancel(context.Background())
events, errs := dockerCli.Client().Events(ctx, eventOptions)
defer cancel()

out := dockerCli.Out()

for {
select {
case event := <-events:
if err := handleEvent(out, event, tmpl); err != nil {
return err
}
case err := <-errs:
if err == io.EOF {
return nil
}
return err
}
}
}

func handleEvent(out io.Writer, event eventtypes.Message, tmpl *template.Template) error {
if tmpl == nil {
return prettyPrintEvent(out, event)
}

return formatEvent(out, event, tmpl)
}

func makeTemplate(format string) (*template.Template, error) {
if format == "" {
return nil, nil
}
tmpl, err := templates.Parse(format)
if err != nil {
return tmpl, err
}
// we execute the template for an empty message, so as to validate
// a bad template like "{{.badFieldString}}"
return tmpl, tmpl.Execute(io.Discard, &eventtypes.Message{})
}

// rfc3339NanoFixed is similar to time.RFC3339Nano, except it pads nanoseconds
// zeros to maintain a fixed number of characters
const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"

// prettyPrintEvent prints all types of event information.
// Each output includes the event type, actor id, name and action.
// Actor attributes are printed at the end if the actor has any.
func prettyPrintEvent(out io.Writer, event eventtypes.Message) error {
if event.TimeNano != 0 {
fmt.Fprintf(out, "%s ", time.Unix(0, event.TimeNano).Format(rfc3339NanoFixed))
} else if event.Time != 0 {
fmt.Fprintf(out, "%s ", time.Unix(event.Time, 0).Format(rfc3339NanoFixed))
}

fmt.Fprintf(out, "%s %s %s", event.Type, event.Action, event.Actor.ID)

if len(event.Actor.Attributes) > 0 {
var attrs []string
var keys []string
for k := range event.Actor.Attributes {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := event.Actor.Attributes[k]
attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
}
fmt.Fprintf(out, " (%s)", strings.Join(attrs, ", "))
}
fmt.Fprint(out, "\n")
return nil
}

func formatEvent(out io.Writer, event eventtypes.Message, tmpl *template.Template) error {
defer out.Write([]byte{'\n'})
return tmpl.Execute(out, event)
}
1 change: 1 addition & 0 deletions cli/command/volume/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewVolumeCommand(dockerCli command.Cli) *cobra.Command {
}
cmd.AddCommand(
newCreateCommand(dockerCli),
newEventsCommand(dockerCli),
newInspectCommand(dockerCli),
newListCommand(dockerCli),
newRemoveCommand(dockerCli),
Expand Down
13 changes: 13 additions & 0 deletions cli/command/volume/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package volume

import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/events"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/spf13/cobra"
)

// newEventsCommand creates a new cobra.Command for `docker volume events`
func newEventsCommand(dockerCli command.Cli) *cobra.Command {
return events.NewObjectEventsCommand(dockerCli, eventtypes.VolumeEventType)
}
Loading