Skip to content

Commit

Permalink
Fix complexity of service/ps.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <dnephin@docker.com>
  • Loading branch information
dnephin committed Jun 1, 2017
1 parent 6279612 commit 0284d81
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions cli/command/service/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -92,50 +121,30 @@ 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++
found = true
}
}
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
}

0 comments on commit 0284d81

Please sign in to comment.