2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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 {
|
|
|
|
cmd := cli.Subcmd("events", "", "Get real time events from the server", true)
|
|
|
|
since := cmd.String([]string{"#since", "-since"}, "", "Show all events created since timestamp")
|
|
|
|
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{}
|
|
|
|
loc = time.FixedZone(time.Now().Zone())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var setTime = func(key, value string) {
|
|
|
|
format := timeutils.RFC3339NanoFixed
|
|
|
|
if len(value) < len(format) {
|
|
|
|
format = format[:len(value)]
|
|
|
|
}
|
|
|
|
if t, err := time.ParseInLocation(format, value, loc); err == nil {
|
|
|
|
v.Set(key, strconv.FormatInt(t.Unix(), 10))
|
|
|
|
} else {
|
|
|
|
v.Set(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *since != "" {
|
|
|
|
setTime("since", *since)
|
|
|
|
}
|
|
|
|
if *until != "" {
|
|
|
|
setTime("until", *until)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|