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

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-10-09 10:29:58 +08:00
parent e307da732a
commit 6ef1c7deaf
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
}

View File

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

View File

@ -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] "