Skip to content

Commit

Permalink
Merge pull request #327 from ningmingxiao/dev3
Browse files Browse the repository at this point in the history
nerdctl events --format
  • Loading branch information
AkihiroSuda committed Sep 4, 2021
2 parents 3e44edd + ee8f083 commit 90d4a88
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,10 @@ Get real time events from the server.
:warning: The output format is not compatible with Docker.

Usage: `nerdctl events [OPTIONS]`
Flags:
- :whale: `--format`: Format the output using the given Go template, e.g, `{{json .}}`

Unimplemented `docker events` flags: `--filter`, `--format`, `--since`, `--until`
Unimplemented `docker events` flags: `--filter`, `--since`, `--until`

### :whale: nerdctl info
Display system-wide information
Expand Down
57 changes: 49 additions & 8 deletions cmd/nerdctl/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"text/template"
"time"

"github.com/containerd/containerd/events"
"github.com/containerd/containerd/log"
"github.com/containerd/typeurl"
"github.com/docker/cli/templates"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"

Expand All @@ -35,6 +39,19 @@ var eventsCommand = &cli.Command{
Usage: "Get real time events from the server",
Description: "NOTE: The output format is not compatible with Docker.",
Action: eventsAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Usage: "Format the output using the given Go template, e.g, '{{json .}}'",
},
},
}

type Out struct {
Timestamp time.Time
Namespace string
Topic string
Event string
}

// eventsActions is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go
Expand All @@ -50,6 +67,19 @@ func eventsAction(clicontext *cli.Context) error {
defer cancel()
eventsClient := client.EventService()
eventsCh, errCh := eventsClient.Subscribe(ctx)

var tmpl *template.Template
switch format := clicontext.String("format"); format {
case "":
tmpl = nil
case "raw", "table":
return errors.New("unsupported format: \"raw\" and \"table\"")
default:
tmpl, err = templates.Parse(format)
if err != nil {
return err
}
}
for {
var e *events.Envelope
select {
Expand All @@ -71,14 +101,25 @@ func eventsAction(clicontext *cli.Context) error {
continue
}
}
if _, err := fmt.Fprintln(
clicontext.App.Writer,
e.Timestamp,
e.Namespace,
e.Topic,
string(out),
); err != nil {
return err
if tmpl != nil {
out := Out{e.Timestamp, e.Namespace, e.Topic, string(out)}
var b bytes.Buffer
if err := tmpl.Execute(&b, out); err != nil {
return err
}
if _, err := fmt.Fprintln(clicontext.App.Writer, b.String()+"\n"); err != nil {
return err
}
} else {
if _, err := fmt.Fprintln(
clicontext.App.Writer,
e.Timestamp,
e.Namespace,
e.Topic,
string(out),
); err != nil {
return err
}
}
}
}
Expand Down

0 comments on commit 90d4a88

Please sign in to comment.