فهرست منبع

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

Signed-off-by: allencloud <allen.sun@daocloud.io>
allencloud 8 سال پیش
والد
کامیت
3af743bb32
3فایلهای تغییر یافته به همراه25 افزوده شده و 16 حذف شده
  1. 13 9
      cli/command/node/list.go
  2. 9 4
      cli/command/service/list.go
  3. 3 3
      cli/command/utils.go

+ 13 - 9
cli/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
-	}
-
-	out := dockerCli.Out()
-	if opts.quiet {
-		printQuiet(out, nodes)
-	} else {
+	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)
 	}
+
 	return nil
 }
 

+ 9 - 4
cli/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
 }
 

+ 3 - 3
cli/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] "