From 6ef1c7deaf031579a692d30d7601559a9c6e6109 Mon Sep 17 00:00:00 2001 From: allencloud Date: Sun, 9 Oct 2016 10:29:58 +0800 Subject: [PATCH] return nil when no node or service to avoid additional api call Signed-off-by: allencloud --- command/node/list.go | 22 +++++++++++++--------- command/service/list.go | 13 +++++++++---- command/utils.go | 6 +++--- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/command/node/list.go b/command/node/list.go index bed4bc4965..d028d19147 100644 --- a/command/node/list.go +++ b/command/node/list.go @@ -45,6 +45,7 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command { func runList(dockerCli *command.DockerCli, opts listOptions) error { client := dockerCli.Client() + out := dockerCli.Out() ctx := context.Background() nodes, err := client.NodeList( @@ -54,17 +55,20 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error { return err } - info, err := client.Info(ctx) - if err != nil { - return err + if len(nodes) > 0 && !opts.quiet { + // only non-empty nodes and not quiet, should we call /info api + info, err := client.Info(ctx) + if err != nil { + return err + } + printTable(out, nodes, info) + } else if !opts.quiet { + // no nodes and not quiet, print only one line with columns ID, HOSTNAME, ... + printTable(out, nodes, types.Info{}) + } else { + printQuiet(out, nodes) } - out := dockerCli.Out() - if opts.quiet { - printQuiet(out, nodes) - } else { - printTable(out, nodes, info) - } return nil } diff --git a/command/service/list.go b/command/service/list.go index 681acd3f25..2278643fbc 100644 --- a/command/service/list.go +++ b/command/service/list.go @@ -49,16 +49,15 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command { func runList(dockerCli *command.DockerCli, opts listOptions) error { ctx := context.Background() client := dockerCli.Client() + out := dockerCli.Out() services, err := client.ServiceList(ctx, types.ServiceListOptions{Filter: opts.filter.Value()}) if err != nil { return err } - out := dockerCli.Out() - if opts.quiet { - PrintQuiet(out, services) - } else { + if len(services) > 0 && !opts.quiet { + // only non-empty services and not quiet, should we call TaskList and NodeList api taskFilter := filters.NewArgs() for _, service := range services { taskFilter.Add("service", service.ID) @@ -75,7 +74,13 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error { } PrintNotQuiet(out, services, nodes, tasks) + } else if !opts.quiet { + // no services and not quiet, print only one line with columns ID, NAME, REPLICAS... + PrintNotQuiet(out, services, []swarm.Node{}, []swarm.Task{}) + } else { + PrintQuiet(out, services) } + return nil } diff --git a/command/utils.go b/command/utils.go index e768cf770d..9f9a1ee80d 100644 --- a/command/utils.go +++ b/command/utils.go @@ -58,14 +58,14 @@ func PrettyPrint(i interface{}) string { } } -// PromptForConfirmation request and check confirmation from user. +// PromptForConfirmation requests and checks confirmation from user. // This will display the provided message followed by ' [y/N] '. If // the user input 'y' or 'Y' it returns true other false. If no -// message is provided "Are you sure you want to proceeed? [y/N] " +// message is provided "Are you sure you want to proceed? [y/N] " // will be used instead. func PromptForConfirmation(ins *InStream, outs *OutStream, message string) bool { if message == "" { - message = "Are you sure you want to proceeed?" + message = "Are you sure you want to proceed?" } message += " [y/N] "