mirror of https://github.com/docker/cli.git
add `docker events --format`
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
parent
d9cb421d69
commit
0ae2a02ce6
|
@ -3,8 +3,10 @@ package system
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
@ -15,6 +17,7 @@ import (
|
||||||
"github.com/docker/docker/cli/command"
|
"github.com/docker/docker/cli/command"
|
||||||
"github.com/docker/docker/opts"
|
"github.com/docker/docker/opts"
|
||||||
"github.com/docker/docker/pkg/jsonlog"
|
"github.com/docker/docker/pkg/jsonlog"
|
||||||
|
"github.com/docker/docker/utils/templates"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,6 +25,7 @@ type eventsOptions struct {
|
||||||
since string
|
since string
|
||||||
until string
|
until string
|
||||||
filter opts.FilterOpt
|
filter opts.FilterOpt
|
||||||
|
format string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEventsCommand creates a new cobra.Command for `docker events`
|
// NewEventsCommand creates a new cobra.Command for `docker events`
|
||||||
|
@ -41,11 +45,18 @@ func NewEventsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
flags.StringVar(&opts.since, "since", "", "Show all events created since timestamp")
|
flags.StringVar(&opts.since, "since", "", "Show all events created since timestamp")
|
||||||
flags.StringVar(&opts.until, "until", "", "Stream events until this timestamp")
|
flags.StringVar(&opts.until, "until", "", "Stream events until this timestamp")
|
||||||
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
||||||
|
flags.StringVar(&opts.format, "format", "", "Format the output using the given go template")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runEvents(dockerCli *command.DockerCli, opts *eventsOptions) error {
|
func runEvents(dockerCli *command.DockerCli, opts *eventsOptions) error {
|
||||||
|
tmpl, err := makeTemplate(opts.format)
|
||||||
|
if err != nil {
|
||||||
|
return cli.StatusError{
|
||||||
|
StatusCode: 64,
|
||||||
|
Status: "Error parsing format: " + err.Error()}
|
||||||
|
}
|
||||||
options := types.EventsOptions{
|
options := types.EventsOptions{
|
||||||
Since: opts.since,
|
Since: opts.since,
|
||||||
Until: opts.until,
|
Until: opts.until,
|
||||||
|
@ -58,33 +69,48 @@ func runEvents(dockerCli *command.DockerCli, opts *eventsOptions) error {
|
||||||
}
|
}
|
||||||
defer responseBody.Close()
|
defer responseBody.Close()
|
||||||
|
|
||||||
return streamEvents(responseBody, dockerCli.Out())
|
return streamEvents(dockerCli.Out(), responseBody, 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(ioutil.Discard, &eventtypes.Message{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// streamEvents decodes prints the incoming events in the provided output.
|
// streamEvents decodes prints the incoming events in the provided output.
|
||||||
func streamEvents(input io.Reader, output io.Writer) error {
|
func streamEvents(out io.Writer, input io.Reader, tmpl *template.Template) error {
|
||||||
return DecodeEvents(input, func(event eventtypes.Message, err error) error {
|
return DecodeEvents(input, func(event eventtypes.Message, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
printOutput(event, output)
|
if tmpl == nil {
|
||||||
return nil
|
return prettyPrintEvent(out, event)
|
||||||
|
}
|
||||||
|
return formatEvent(out, event, tmpl)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type eventProcessor func(event eventtypes.Message, err error) error
|
type eventProcessor func(event eventtypes.Message, err error) error
|
||||||
|
|
||||||
// printOutput prints all types of event information.
|
// prettyPrintEvent prints all types of event information.
|
||||||
// Each output includes the event type, actor id, name and action.
|
// Each output includes the event type, actor id, name and action.
|
||||||
// Actor attributes are printed at the end if the actor has any.
|
// Actor attributes are printed at the end if the actor has any.
|
||||||
func printOutput(event eventtypes.Message, output io.Writer) {
|
func prettyPrintEvent(out io.Writer, event eventtypes.Message) error {
|
||||||
if event.TimeNano != 0 {
|
if event.TimeNano != 0 {
|
||||||
fmt.Fprintf(output, "%s ", time.Unix(0, event.TimeNano).Format(jsonlog.RFC3339NanoFixed))
|
fmt.Fprintf(out, "%s ", time.Unix(0, event.TimeNano).Format(jsonlog.RFC3339NanoFixed))
|
||||||
} else if event.Time != 0 {
|
} else if event.Time != 0 {
|
||||||
fmt.Fprintf(output, "%s ", time.Unix(event.Time, 0).Format(jsonlog.RFC3339NanoFixed))
|
fmt.Fprintf(out, "%s ", time.Unix(event.Time, 0).Format(jsonlog.RFC3339NanoFixed))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(output, "%s %s %s", event.Type, event.Action, event.Actor.ID)
|
fmt.Fprintf(out, "%s %s %s", event.Type, event.Action, event.Actor.ID)
|
||||||
|
|
||||||
if len(event.Actor.Attributes) > 0 {
|
if len(event.Actor.Attributes) > 0 {
|
||||||
var attrs []string
|
var attrs []string
|
||||||
|
@ -97,7 +123,13 @@ func printOutput(event eventtypes.Message, output io.Writer) {
|
||||||
v := event.Actor.Attributes[k]
|
v := event.Actor.Attributes[k]
|
||||||
attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
|
attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
|
||||||
}
|
}
|
||||||
fmt.Fprintf(output, " (%s)", strings.Join(attrs, ", "))
|
fmt.Fprintf(out, " (%s)", strings.Join(attrs, ", "))
|
||||||
}
|
}
|
||||||
fmt.Fprint(output, "\n")
|
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue