From bbd2018ee19eff5594ae3986bf56fbcd0044699d Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Tue, 6 Dec 2016 18:52:47 -0800 Subject: [PATCH] service ps: Revert output to 1.12 behavior. - Display the ID column - Do not append the task ID in the name column - (NEW): Truncate task IDs, unless --no-trunc is specified Signed-off-by: Andrea Luzzardi --- cli/command/task/print.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cli/command/task/print.go b/cli/command/task/print.go index 2995e9afb3..0f1c2cf724 100644 --- a/cli/command/task/print.go +++ b/cli/command/task/print.go @@ -14,11 +14,12 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command/idresolver" + "github.com/docker/docker/pkg/stringid" "github.com/docker/go-units" ) const ( - psTaskItemFmt = "%s\t%s\t%s\t%s\t%s %s ago\t%s\t%s\n" + psTaskItemFmt = "%s\t%s\t%s\t%s\t%s\t%s %s ago\t%s\t%s\n" maxErrLength = 30 ) @@ -67,7 +68,7 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task // Ignore flushing errors defer writer.Flush() - fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR", "PORTS"}, "\t")) + fmt.Fprintln(writer, strings.Join([]string{"ID", "NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR", "PORTS"}, "\t")) if err := print(writer, ctx, tasks, resolver, noTrunc); err != nil { return err @@ -90,25 +91,36 @@ func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error { } func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error { - prevService := "" - prevSlot := 0 + prevName := "" for _, task := range tasks { - name, err := resolver.Resolve(ctx, task, task.ID) + id := task.ID + if !noTrunc { + id = stringid.TruncateID(id) + } + + serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID) + if err != nil { + return err + } nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID) if err != nil { return err } + name := "" + if task.Slot != 0 { + name = fmt.Sprintf("%v.%v", serviceName, task.Slot) + } else { + name = fmt.Sprintf("%v.%v", serviceName, task.NodeID) + } + // Indent the name if necessary indentedName := name - // Since the new format of the task name is .., we should only compare - // and here. - if prevService == task.ServiceID && prevSlot == task.Slot { + if name == prevName { indentedName = fmt.Sprintf(" \\_ %s", indentedName) } - prevService = task.ServiceID - prevSlot = task.Slot + prevName = name // Trim and quote the error message. taskErr := task.Status.Err @@ -134,6 +146,7 @@ func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idr fmt.Fprintf( out, psTaskItemFmt, + id, indentedName, image, nodeValue,