From 0284d81a112d0834d46526ebd683fa64fd12109d Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 1 Jun 2017 11:47:43 -0400 Subject: [PATCH] Fix complexity of service/ps. Signed-off-by: Daniel Nephin --- cli/command/service/ps.go | 65 ++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/cli/command/service/ps.go b/cli/command/service/ps.go index 7c744478ec8c..6266bfce68a1 100644 --- a/cli/command/service/ps.go +++ b/cli/command/service/ps.go @@ -12,6 +12,7 @@ import ( "github.com/docker/cli/opts" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/client" "github.com/pkg/errors" "github.com/spf13/cobra" "golang.org/x/net/context" @@ -52,6 +53,34 @@ func runPS(dockerCli command.Cli, options psOptions) error { client := dockerCli.Client() ctx := context.Background() + filter, notfound, err := createFilter(ctx, client, options) + if err != nil { + return err + } + + tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter}) + if err != nil { + return err + } + + format := options.format + if len(format) == 0 { + if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet { + format = dockerCli.ConfigFile().TasksFormat + } else { + format = formatter.TableFormatKey + } + } + if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil { + return err + } + if len(notfound) != 0 { + return errors.New(strings.Join(notfound, "\n")) + } + return nil +} + +func createFilter(ctx context.Context, client client.APIClient, options psOptions) (filters.Args, []string, error) { filter := options.filter.Value() serviceIDFilter := filters.NewArgs() @@ -62,14 +91,14 @@ func runPS(dockerCli command.Cli, options psOptions) error { } serviceByIDList, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceIDFilter}) if err != nil { - return err + return filter, nil, err } serviceByNameList, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceNameFilter}) if err != nil { - return err + return filter, nil, err } - var errs []string + var notfound []string serviceCount := 0 loop: // Match services by 1. Full ID, 2. Full name, 3. ID prefix. An error is returned if the ID-prefix match is ambiguous @@ -92,7 +121,7 @@ loop: for _, s := range serviceByIDList { if strings.HasPrefix(s.ID, service) { if found { - return errors.New("multiple services found with provided prefix: " + service) + return filter, nil, errors.New("multiple services found with provided prefix: " + service) } filter.Add("service", s.ID) serviceCount++ @@ -100,42 +129,22 @@ loop: } } if !found { - errs = append(errs, "no such service: "+service) + notfound = append(notfound, "no such service: "+service) } } if serviceCount == 0 { - return errors.New(strings.Join(errs, "\n")) + return filter, nil, errors.New(strings.Join(notfound, "\n")) } if filter.Include("node") { nodeFilters := filter.Get("node") for _, nodeFilter := range nodeFilters { nodeReference, err := node.Reference(ctx, client, nodeFilter) if err != nil { - return err + return filter, nil, err } filter.Del("node", nodeFilter) filter.Add("node", nodeReference) } } - - tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter}) - if err != nil { - return err - } - - format := options.format - if len(format) == 0 { - if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet { - format = dockerCli.ConfigFile().TasksFormat - } else { - format = formatter.TableFormatKey - } - } - if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil { - return err - } - if len(errs) != 0 { - return errors.New(strings.Join(errs, "\n")) - } - return nil + return filter, nil, err }