Merge pull request #27654 from gaocegege/add-quiet-mode-to-service-ps

Add -q option to `docker service ps`
This commit is contained in:
Vincent Demeester 2016-10-28 05:21:03 +02:00 committed by GitHub
commit 4b65bdd802
2 changed files with 31 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import (
type psOptions struct { type psOptions struct {
serviceID string serviceID string
quiet bool
noResolve bool noResolve bool
noTrunc bool noTrunc bool
filter opts.FilterOpt filter opts.FilterOpt
@ -32,6 +33,7 @@ func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
}, },
} }
flags := cmd.Flags() 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.noTrunc, "no-trunc", false, "Do not truncate output")
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names") 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") 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 return err
} }
if opts.quiet {
return task.PrintQuiet(dockerCli, tasks)
}
return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc) return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
} }

View File

@ -2,6 +2,7 @@ package task
import ( import (
"fmt" "fmt"
"io"
"sort" "sort"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
@ -40,7 +41,9 @@ func (t tasksBySlot) Less(i, j int) bool {
return t[j].Meta.CreatedAt.Before(t[i].CreatedAt) 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 { func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
sort.Stable(tasksBySlot(tasks)) sort.Stable(tasksBySlot(tasks))
@ -50,6 +53,27 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
defer writer.Flush() defer writer.Flush()
fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t")) 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 := "" prevServiceName := ""
prevSlot := 0 prevSlot := 0
for _, task := range tasks { for _, task := range tasks {
@ -94,7 +118,7 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
} }
fmt.Fprintf( fmt.Fprintf(
writer, out,
psTaskItemFmt, psTaskItemFmt,
indentedName, indentedName,
task.Spec.ContainerSpec.Image, task.Spec.ContainerSpec.Image,
@ -105,6 +129,5 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
taskErr, taskErr,
) )
} }
return nil return nil
} }