From b1881e6c58e3e75560d369f754134513c3f42063 Mon Sep 17 00:00:00 2001 From: skanehira Date: Mon, 26 Aug 2019 08:19:03 +0900 Subject: [PATCH] Improve container top Check container state. If container is not running then not call top api. --- pkg/commands/container.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/commands/container.go b/pkg/commands/container.go index 96a3a663..9f48442d 100644 --- a/pkg/commands/container.go +++ b/pkg/commands/container.go @@ -367,6 +367,16 @@ func (c *Container) Attach() (*exec.Cmd, error) { // Top returns process information func (c *Container) Top() (container.ContainerTopOKBody, error) { + detail, err := c.Inspect() + if err != nil { + return container.ContainerTopOKBody{}, err + } + + // check container status + if !detail.State.Running { + return container.ContainerTopOKBody{}, errors.New("container is not running") + } + return c.Client.ContainerTop(context.Background(), c.ID, []string{}) } @@ -411,7 +421,7 @@ func (c *Container) Inspect() (types.ContainerJSON, error) { // RenderTop returns details about the container func (c *Container) RenderTop() (string, error) { - result, err := c.Client.ContainerTop(context.Background(), c.ID, []string{}) + result, err := c.Top() if err != nil { return "", err }