From 953f2e9f3b79e33bd3f114be2e805d4b325ac41c Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Tue, 27 Sep 2016 15:17:56 -0700 Subject: [PATCH] Add client impl for listing containers Signed-off-by: Mrunal Patel --- cmd/client/container.go | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cmd/client/container.go b/cmd/client/container.go index 067f8178..de40641e 100644 --- a/cmd/client/container.go +++ b/cmd/client/container.go @@ -18,6 +18,7 @@ var containerCommand = cli.Command{ stopContainerCommand, removeContainerCommand, containerStatusCommand, + listContainersCommand, }, } @@ -164,6 +165,26 @@ var containerStatusCommand = cli.Command{ }, } +var listContainersCommand = cli.Command{ + Name: "list", + Usage: "list containers", + Action: func(context *cli.Context) error { + // Set up a connection to the server. + conn, err := getClientConnection(context) + if err != nil { + return fmt.Errorf("failed to connect: %v", err) + } + defer conn.Close() + client := pb.NewRuntimeServiceClient(conn) + + err = ListContainers(client) + if err != nil { + return fmt.Errorf("listing containers failed: %v", err) + } + return nil + }, +} + // CreateContainer sends a CreateContainerRequest to the server, and parses // the returned CreateContainerResponse. func CreateContainer(client pb.RuntimeServiceClient, sandbox string, path string) error { @@ -264,3 +285,24 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID string) error { return nil } + +// ListContainers sends a ListContainerRequest to the server, and parses +// the returned ListContainerResponse. +func ListContainers(client pb.RuntimeServiceClient) error { + r, err := client.ListContainers(context.Background(), &pb.ListContainersRequest{}) + if err != nil { + return err + } + for _, c := range r.GetContainers() { + fmt.Printf("ID: %s\n", *c.Id) + fmt.Printf("Pod: %s\n", *c.PodSandboxId) + if c.State != nil { + fmt.Printf("Status: %s\n", *c.State) + } + if c.CreatedAt != nil { + ctm := time.Unix(*c.CreatedAt, 0) + fmt.Printf("Created: %v\n", ctm) + } + } + return nil +}