Kaynağa Gözat

fixes #27643

Signed-off-by: Ce Gao <ce.gao@outlook.com>
Ce Gao 8 yıl önce
ebeveyn
işleme
9efa472bd1

+ 5 - 0
cli/command/service/ps.go

@@ -14,6 +14,7 @@ import (
 
 type psOptions struct {
 	serviceID string
+	quiet     bool
 	noResolve bool
 	noTrunc   bool
 	filter    opts.FilterOpt
@@ -32,6 +33,7 @@ func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
 		},
 	}
 	flags := cmd.Flags()
+	flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
 	flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
 	flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
 	flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
@@ -67,5 +69,8 @@ func runPS(dockerCli *command.DockerCli, opts psOptions) error {
 		return err
 	}
 
+	if opts.quiet {
+		return task.PrintQuiet(dockerCli, tasks)
+	}
 	return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
 }

+ 26 - 3
cli/command/task/print.go

@@ -2,6 +2,7 @@ package task
 
 import (
 	"fmt"
+	"io"
 	"sort"
 	"strings"
 	"text/tabwriter"
@@ -40,7 +41,9 @@ func (t tasksBySlot) Less(i, j int) bool {
 	return t[j].Meta.CreatedAt.Before(t[i].CreatedAt)
 }
 
-// Print task information in a table format
+// Print task information in a table format.
+// Besides this, command `docker node ps <node>`
+// and `docker stack ps` will call this, too.
 func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
 	sort.Stable(tasksBySlot(tasks))
 
@@ -50,6 +53,27 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
 	defer writer.Flush()
 	fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t"))
 
+	if err := print(writer, ctx, tasks, resolver, noTrunc); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// PrintQuiet shows task list in a quiet way.
+func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
+	sort.Stable(tasksBySlot(tasks))
+
+	out := dockerCli.Out()
+
+	for _, task := range tasks {
+		fmt.Fprintln(out, task.ID)
+	}
+
+	return nil
+}
+
+func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
 	prevServiceName := ""
 	prevSlot := 0
 	for _, task := range tasks {
@@ -94,7 +118,7 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
 		}
 
 		fmt.Fprintf(
-			writer,
+			out,
 			psTaskItemFmt,
 			indentedName,
 			task.Spec.ContainerSpec.Image,
@@ -105,6 +129,5 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
 			taskErr,
 		)
 	}
-
 	return nil
 }

+ 1 - 1
contrib/completion/bash/docker

@@ -2462,7 +2462,7 @@ _docker_service_ps() {
 
 	case "$cur" in
 		-*)
-			COMPREPLY=( $( compgen -W "--all -a --filter -f --help --no-resolve --no-trunc" -- "$cur" ) )
+			COMPREPLY=( $( compgen -W "--filter -f --help --no-resolve --no-trunc --quiet -q" -- "$cur" ) )
 			;;
 		*)
 			local counter=$(__docker_pos_first_nonflag '--filter|-f')

+ 1 - 1
contrib/completion/zsh/_docker

@@ -1167,10 +1167,10 @@ __docker_service_subcommand() {
         (ps)
             _arguments $(__docker_arguments) \
                 $opts_help \
-                "($help -a --all)"{-a,--all}"[Display all tasks]" \
                 "($help)*"{-f=,--filter=}"[Provide filter values]:filter:->filter-options" \
                 "($help)--no-resolve[Do not map IDs to Names]" \
                 "($help)--no-trunc[Do not truncate output]" \
+                "($help -q --quiet)"{-q,--quiet}"[Only display task IDs]" \
                 "($help -)1:service:__docker_complete_services" && ret=0
             case $state in
                 (filter-options)

+ 6 - 7
docs/reference/commandline/service_ps.md

@@ -17,22 +17,21 @@ aliases: ["/engine/reference/commandline/service_tasks/"]
 # service ps
 
 ```Markdown
-Usage:	docker service ps [OPTIONS] SERVICE
+Usage:  docker service ps [OPTIONS] SERVICE
 
 List the tasks of a service
 
 Options:
-  -a, --all            Display all tasks
-  -f, --filter value   Filter output based on conditions provided
-      --help           Print usage
-      --no-resolve     Do not map IDs to Names
-      --no-trunc       Do not truncate output
+  -f, --filter filter   Filter output based on conditions provided
+      --help            Print usage
+      --no-resolve      Do not map IDs to Names
+      --no-trunc        Do not truncate output
+  -q, --quiet           Only display task IDs
 ```
 
 Lists the tasks that are running as part of the specified service. This command
 has to be run targeting a manager node.
 
-
 ## Examples
 
 ### Listing the tasks that are part of a service