2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2015-05-12 15:59:34 -04:00
|
|
|
"time"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
|
|
|
"github.com/docker/docker/pkg/timeutils"
|
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdEvents prints a live stream of real time events from the server.
|
|
|
|
//
|
|
|
|
// Usage: docker events [OPTIONS]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdEvents(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("events", nil, Cli.DockerCommands["events"].Description, true)
|
2015-11-09 09:37:24 -05:00
|
|
|
since := cmd.String([]string{"-since"}, "", "Show all events created since timestamp")
|
2015-03-24 23:57:23 -04:00
|
|
|
until := cmd.String([]string{"-until"}, "", "Stream events until this timestamp")
|
|
|
|
flFilter := opts.NewListOpts(nil)
|
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
v = url.Values{}
|
|
|
|
eventFilterArgs = filters.Args{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Consolidate all filter flags, and sanity check them early.
|
|
|
|
// They'll get process in the daemon/server.
|
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
var err error
|
|
|
|
eventFilterArgs, err = filters.ParseFlag(f, eventFilterArgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 15:59:34 -04:00
|
|
|
ref := time.Now()
|
2015-03-24 23:57:23 -04:00
|
|
|
if *since != "" {
|
2015-10-29 13:51:36 -04:00
|
|
|
ts, err := timeutils.GetTimestamp(*since, ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set("since", ts)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
if *until != "" {
|
2015-10-29 13:51:36 -04:00
|
|
|
ts, err := timeutils.GetTimestamp(*until, ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Set("until", ts)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
if len(eventFilterArgs) > 0 {
|
2015-03-25 22:31:29 -04:00
|
|
|
filterJSON, err := filters.ToParam(eventFilterArgs)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-25 22:31:29 -04:00
|
|
|
v.Set("filters", filterJSON)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-05-01 14:23:44 -04:00
|
|
|
sopts := &streamOpts{
|
|
|
|
rawTerminal: true,
|
|
|
|
out: cli.out,
|
|
|
|
}
|
2015-06-04 09:30:14 -04:00
|
|
|
if _, err := cli.stream("GET", "/events?"+v.Encode(), sopts); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|