From d296f7a391b54cc5a0b0a80a9d735e7e47a96c86 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Fri, 4 Nov 2016 19:23:07 -0700 Subject: [PATCH] service ps: Truncate Task IDs - Refactored to move resolution code into the idresolver - Made `ps` output more bearable by shortening service IDs in task names Signed-off-by: Andrea Luzzardi --- cli/command/idresolver/idresolver.go | 22 +++++++++++++++++++++- cli/command/task/print.go | 24 +++++------------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/cli/command/idresolver/idresolver.go b/cli/command/idresolver/idresolver.go index ad0d96735d..511b1a8f54 100644 --- a/cli/command/idresolver/idresolver.go +++ b/cli/command/idresolver/idresolver.go @@ -7,6 +7,7 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" + "github.com/docker/docker/pkg/stringid" ) // IDResolver provides ID to Name resolution. @@ -26,7 +27,7 @@ func New(client client.APIClient, noResolve bool) *IDResolver { } func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) { - switch t.(type) { + switch t := t.(type) { case swarm.Node: node, _, err := r.client.NodeInspectWithRaw(ctx, id) if err != nil { @@ -45,6 +46,25 @@ func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, return id, nil } return service.Spec.Annotations.Name, nil + case swarm.Task: + // If the caller passes the full task there's no need to do a lookup. + if t.ID == "" { + var err error + + t, _, err = r.client.TaskInspectWithRaw(ctx, id) + if err != nil { + return id, nil + } + } + taskID := stringid.TruncateID(t.ID) + if t.ServiceID == "" { + return taskID, nil + } + service, err := r.Resolve(ctx, swarm.Service{}, t.ServiceID) + if err != nil { + return "", err + } + return fmt.Sprintf("%s.%d.%s", service, t.Slot, taskID), nil default: return "", fmt.Errorf("unsupported type") } diff --git a/cli/command/task/print.go b/cli/command/task/print.go index b3cdcbe533..45af178a42 100644 --- a/cli/command/task/print.go +++ b/cli/command/task/print.go @@ -74,38 +74,24 @@ 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 { - prevServiceName := "" + prevService := "" prevSlot := 0 for _, task := range tasks { - serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID) - if err != nil { - return err - } + name, err := resolver.Resolve(ctx, task, task.ID) + nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID) if err != nil { return err } - name := task.Annotations.Name - // TODO: This is the fallback .. in case task name is not present in - // Annotations (upgraded from 1.12). - // We may be able to remove the following in the future. - if name == "" { - if task.Slot != 0 { - name = fmt.Sprintf("%v.%v.%v", serviceName, task.Slot, task.ID) - } else { - name = fmt.Sprintf("%v.%v.%v", serviceName, task.NodeID, task.ID) - } - } - // Indent the name if necessary indentedName := name // Since the new format of the task name is .., we should only compare // and here. - if prevServiceName == serviceName && prevSlot == task.Slot { + if prevService == task.ServiceID && prevSlot == task.Slot { indentedName = fmt.Sprintf(" \\_ %s", indentedName) } - prevServiceName = serviceName + prevService = task.ServiceID prevSlot = task.Slot // Trim and quote the error message.