Browse code

fixes #27643

Signed-off-by: Ce Gao <ce.gao@outlook.com>

Ce Gao authored on 2016/10/28 09:02:57
Showing 5 changed files
... ...
@@ -14,6 +14,7 @@ import (
14 14
 
15 15
 type psOptions struct {
16 16
 	serviceID string
17
+	quiet     bool
17 18
 	noResolve bool
18 19
 	noTrunc   bool
19 20
 	filter    opts.FilterOpt
... ...
@@ -32,6 +33,7 @@ func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
32 32
 		},
33 33
 	}
34 34
 	flags := cmd.Flags()
35
+	flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
35 36
 	flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
36 37
 	flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
37 38
 	flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
... ...
@@ -67,5 +69,8 @@ func runPS(dockerCli *command.DockerCli, opts psOptions) error {
67 67
 		return err
68 68
 	}
69 69
 
70
+	if opts.quiet {
71
+		return task.PrintQuiet(dockerCli, tasks)
72
+	}
70 73
 	return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
71 74
 }
... ...
@@ -2,6 +2,7 @@ package task
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"io"
5 6
 	"sort"
6 7
 	"strings"
7 8
 	"text/tabwriter"
... ...
@@ -40,7 +41,9 @@ func (t tasksBySlot) Less(i, j int) bool {
40 40
 	return t[j].Meta.CreatedAt.Before(t[i].CreatedAt)
41 41
 }
42 42
 
43
-// Print task information in a table format
43
+// Print task information in a table format.
44
+// Besides this, command `docker node ps <node>`
45
+// and `docker stack ps` will call this, too.
44 46
 func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
45 47
 	sort.Stable(tasksBySlot(tasks))
46 48
 
... ...
@@ -50,6 +53,27 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
50 50
 	defer writer.Flush()
51 51
 	fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t"))
52 52
 
53
+	if err := print(writer, ctx, tasks, resolver, noTrunc); err != nil {
54
+		return err
55
+	}
56
+
57
+	return nil
58
+}
59
+
60
+// PrintQuiet shows task list in a quiet way.
61
+func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
62
+	sort.Stable(tasksBySlot(tasks))
63
+
64
+	out := dockerCli.Out()
65
+
66
+	for _, task := range tasks {
67
+		fmt.Fprintln(out, task.ID)
68
+	}
69
+
70
+	return nil
71
+}
72
+
73
+func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
53 74
 	prevServiceName := ""
54 75
 	prevSlot := 0
55 76
 	for _, task := range tasks {
... ...
@@ -94,7 +118,7 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
94 94
 		}
95 95
 
96 96
 		fmt.Fprintf(
97
-			writer,
97
+			out,
98 98
 			psTaskItemFmt,
99 99
 			indentedName,
100 100
 			task.Spec.ContainerSpec.Image,
... ...
@@ -105,6 +129,5 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
105 105
 			taskErr,
106 106
 		)
107 107
 	}
108
-
109 108
 	return nil
110 109
 }
... ...
@@ -2462,7 +2462,7 @@ _docker_service_ps() {
2462 2462
 
2463 2463
 	case "$cur" in
2464 2464
 		-*)
2465
-			COMPREPLY=( $( compgen -W "--all -a --filter -f --help --no-resolve --no-trunc" -- "$cur" ) )
2465
+			COMPREPLY=( $( compgen -W "--filter -f --help --no-resolve --no-trunc --quiet -q" -- "$cur" ) )
2466 2466
 			;;
2467 2467
 		*)
2468 2468
 			local counter=$(__docker_pos_first_nonflag '--filter|-f')
... ...
@@ -1167,10 +1167,10 @@ __docker_service_subcommand() {
1167 1167
         (ps)
1168 1168
             _arguments $(__docker_arguments) \
1169 1169
                 $opts_help \
1170
-                "($help -a --all)"{-a,--all}"[Display all tasks]" \
1171 1170
                 "($help)*"{-f=,--filter=}"[Provide filter values]:filter:->filter-options" \
1172 1171
                 "($help)--no-resolve[Do not map IDs to Names]" \
1173 1172
                 "($help)--no-trunc[Do not truncate output]" \
1173
+                "($help -q --quiet)"{-q,--quiet}"[Only display task IDs]" \
1174 1174
                 "($help -)1:service:__docker_complete_services" && ret=0
1175 1175
             case $state in
1176 1176
                 (filter-options)
... ...
@@ -17,22 +17,21 @@ aliases: ["/engine/reference/commandline/service_tasks/"]
17 17
 # service ps
18 18
 
19 19
 ```Markdown
20
-Usage:	docker service ps [OPTIONS] SERVICE
20
+Usage:  docker service ps [OPTIONS] SERVICE
21 21
 
22 22
 List the tasks of a service
23 23
 
24 24
 Options:
25
-  -a, --all            Display all tasks
26
-  -f, --filter value   Filter output based on conditions provided
27
-      --help           Print usage
28
-      --no-resolve     Do not map IDs to Names
29
-      --no-trunc       Do not truncate output
25
+  -f, --filter filter   Filter output based on conditions provided
26
+      --help            Print usage
27
+      --no-resolve      Do not map IDs to Names
28
+      --no-trunc        Do not truncate output
29
+  -q, --quiet           Only display task IDs
30 30
 ```
31 31
 
32 32
 Lists the tasks that are running as part of the specified service. This command
33 33
 has to be run targeting a manager node.
34 34
 
35
-
36 35
 ## Examples
37 36
 
38 37
 ### Listing the tasks that are part of a service