From d8412061d283a32291be4d7dc10d53677128c79f Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Thu, 25 Feb 2016 10:10:22 -0800 Subject: [PATCH] Add watch command to ctr This command dump events matching a given container id or all events if no id is provided. Signed-off-by: Kenfe-Mickael Laventure --- ctr/container.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/ctr/container.go b/ctr/container.go index b6900af..d489e3b 100644 --- a/ctr/container.go +++ b/ctr/container.go @@ -52,6 +52,7 @@ var containersCommand = cli.Command{ resumeCommand, startCommand, statsCommand, + watchCommand, }, Action: listContainers, } @@ -271,6 +272,44 @@ func attachStdio(s stdio) error { return nil } +var watchCommand = cli.Command{ + Name: "watch", + Usage: "print container events", + Action: func(context *cli.Context) { + c := getClient(context) + id := context.Args().First() + if id != "" { + resp, err := c.State(netcontext.Background(), &types.StateRequest{Id: id}) + if err != nil { + fatal(err.Error(), 1) + } + for _, c := range resp.Containers { + if c.Id == id { + break + } + } + if id == "" { + fatal("Invalid container id", 1) + } + } + events, reqErr := c.Events(netcontext.Background(), &types.EventsRequest{}) + if reqErr != nil { + fatal(reqErr.Error(), 1) + } + + for { + e, err := events.Recv() + if err != nil { + fatal(err.Error(), 1) + } + + if id == "" || e.Id == id { + fmt.Printf("%#v\n", e) + } + } + }, +} + var pauseCommand = cli.Command{ Name: "pause", Usage: "pause a container",