return nil when no node or service to avoid additional api call

Signed-off-by: allencloud <allen.sun@daocloud.io>
Upstream-commit: 6ef1c7deaf
Component: cli
This commit is contained in:
allencloud
2016-10-09 10:29:58 +08:00
parent 9d0867981a
commit fcc2ad15e7
3 changed files with 25 additions and 16 deletions

View File

@ -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
}