github.com/goern/docker@v1.9.0-rc1/api/client/events.go (about)

     1  package client
     2  
     3  import (
     4  	"net/url"
     5  	"time"
     6  
     7  	Cli "github.com/docker/docker/cli"
     8  	"github.com/docker/docker/opts"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/docker/pkg/parsers/filters"
    11  	"github.com/docker/docker/pkg/timeutils"
    12  )
    13  
    14  // CmdEvents prints a live stream of real time events from the server.
    15  //
    16  // Usage: docker events [OPTIONS]
    17  func (cli *DockerCli) CmdEvents(args ...string) error {
    18  	cmd := Cli.Subcmd("events", nil, Cli.DockerCommands["events"].Description, true)
    19  	since := cmd.String([]string{"#since", "-since"}, "", "Show all events created since timestamp")
    20  	until := cmd.String([]string{"-until"}, "", "Stream events until this timestamp")
    21  	flFilter := opts.NewListOpts(nil)
    22  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
    23  	cmd.Require(flag.Exact, 0)
    24  
    25  	cmd.ParseFlags(args, true)
    26  
    27  	var (
    28  		v               = url.Values{}
    29  		eventFilterArgs = filters.Args{}
    30  	)
    31  
    32  	// Consolidate all filter flags, and sanity check them early.
    33  	// They'll get process in the daemon/server.
    34  	for _, f := range flFilter.GetAll() {
    35  		var err error
    36  		eventFilterArgs, err = filters.ParseFlag(f, eventFilterArgs)
    37  		if err != nil {
    38  			return err
    39  		}
    40  	}
    41  	ref := time.Now()
    42  	if *since != "" {
    43  		v.Set("since", timeutils.GetTimestamp(*since, ref))
    44  	}
    45  	if *until != "" {
    46  		v.Set("until", timeutils.GetTimestamp(*until, ref))
    47  	}
    48  	if len(eventFilterArgs) > 0 {
    49  		filterJSON, err := filters.ToParam(eventFilterArgs)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		v.Set("filters", filterJSON)
    54  	}
    55  	sopts := &streamOpts{
    56  		rawTerminal: true,
    57  		out:         cli.out,
    58  	}
    59  	if _, err := cli.stream("GET", "/events?"+v.Encode(), sopts); err != nil {
    60  		return err
    61  	}
    62  	return nil
    63  }