Browse code

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

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2016/10/09 11:29:58
Showing 3 changed files
... ...
@@ -45,6 +45,7 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
45 45
 
46 46
 func runList(dockerCli *command.DockerCli, opts listOptions) error {
47 47
 	client := dockerCli.Client()
48
+	out := dockerCli.Out()
48 49
 	ctx := context.Background()
49 50
 
50 51
 	nodes, err := client.NodeList(
... ...
@@ -54,17 +55,20 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
54 54
 		return err
55 55
 	}
56 56
 
57
-	info, err := client.Info(ctx)
58
-	if err != nil {
59
-		return err
60
-	}
61
-
62
-	out := dockerCli.Out()
63
-	if opts.quiet {
64
-		printQuiet(out, nodes)
65
-	} else {
57
+	if len(nodes) > 0 && !opts.quiet {
58
+		// only non-empty nodes and not quiet, should we call /info api
59
+		info, err := client.Info(ctx)
60
+		if err != nil {
61
+			return err
62
+		}
66 63
 		printTable(out, nodes, info)
64
+	} else if !opts.quiet {
65
+		// no nodes and not quiet, print only one line with columns ID, HOSTNAME, ...
66
+		printTable(out, nodes, types.Info{})
67
+	} else {
68
+		printQuiet(out, nodes)
67 69
 	}
70
+
68 71
 	return nil
69 72
 }
70 73
 
... ...
@@ -49,16 +49,15 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
49 49
 func runList(dockerCli *command.DockerCli, opts listOptions) error {
50 50
 	ctx := context.Background()
51 51
 	client := dockerCli.Client()
52
+	out := dockerCli.Out()
52 53
 
53 54
 	services, err := client.ServiceList(ctx, types.ServiceListOptions{Filter: opts.filter.Value()})
54 55
 	if err != nil {
55 56
 		return err
56 57
 	}
57 58
 
58
-	out := dockerCli.Out()
59
-	if opts.quiet {
60
-		PrintQuiet(out, services)
61
-	} else {
59
+	if len(services) > 0 && !opts.quiet {
60
+		// only non-empty services and not quiet, should we call TaskList and NodeList api
62 61
 		taskFilter := filters.NewArgs()
63 62
 		for _, service := range services {
64 63
 			taskFilter.Add("service", service.ID)
... ...
@@ -75,7 +74,13 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
75 75
 		}
76 76
 
77 77
 		PrintNotQuiet(out, services, nodes, tasks)
78
+	} else if !opts.quiet {
79
+		// no services and not quiet, print only one line with columns ID, NAME, REPLICAS...
80
+		PrintNotQuiet(out, services, []swarm.Node{}, []swarm.Task{})
81
+	} else {
82
+		PrintQuiet(out, services)
78 83
 	}
84
+
79 85
 	return nil
80 86
 }
81 87
 
... ...
@@ -58,14 +58,14 @@ func PrettyPrint(i interface{}) string {
58 58
 	}
59 59
 }
60 60
 
61
-// PromptForConfirmation request and check confirmation from user.
61
+// PromptForConfirmation requests and checks confirmation from user.
62 62
 // This will display the provided message followed by ' [y/N] '. If
63 63
 // the user input 'y' or 'Y' it returns true other false.  If no
64
-// message is provided "Are you sure you want to proceeed? [y/N] "
64
+// message is provided "Are you sure you want to proceed? [y/N] "
65 65
 // will be used instead.
66 66
 func PromptForConfirmation(ins *InStream, outs *OutStream, message string) bool {
67 67
 	if message == "" {
68
-		message = "Are you sure you want to proceeed?"
68
+		message = "Are you sure you want to proceed?"
69 69
 	}
70 70
 	message += " [y/N] "
71 71