|
1 | 1 | package stack |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
4 | 7 | "github.com/docker/cli/cli" |
5 | 8 | "github.com/docker/cli/cli/command" |
6 | | - "github.com/docker/cli/cli/command/stack/options" |
7 | | - "github.com/docker/cli/cli/command/stack/swarm" |
| 9 | + "github.com/docker/cli/cli/command/idresolver" |
| 10 | + "github.com/docker/cli/cli/command/task" |
8 | 11 | flagsHelper "github.com/docker/cli/cli/flags" |
9 | 12 | cliopts "github.com/docker/cli/opts" |
| 13 | + "github.com/moby/moby/client" |
10 | 14 | "github.com/spf13/cobra" |
11 | 15 | ) |
12 | 16 |
|
13 | | -func newPsCommand(dockerCli command.Cli) *cobra.Command { |
14 | | - opts := options.PS{Filter: cliopts.NewFilterOpt()} |
| 17 | +// psOptions holds docker stack ps options |
| 18 | +type psOptions struct { |
| 19 | + filter cliopts.FilterOpt |
| 20 | + noTrunc bool |
| 21 | + namespace string |
| 22 | + noResolve bool |
| 23 | + quiet bool |
| 24 | + format string |
| 25 | +} |
| 26 | + |
| 27 | +func newPsCommand(dockerCLI command.Cli) *cobra.Command { |
| 28 | + opts := psOptions{filter: cliopts.NewFilterOpt()} |
15 | 29 |
|
16 | 30 | cmd := &cobra.Command{ |
17 | 31 | Use: "ps [OPTIONS] STACK", |
18 | 32 | Short: "List the tasks in the stack", |
19 | 33 | Args: cli.ExactArgs(1), |
20 | 34 | RunE: func(cmd *cobra.Command, args []string) error { |
21 | | - opts.Namespace = args[0] |
22 | | - if err := validateStackName(opts.Namespace); err != nil { |
| 35 | + opts.namespace = args[0] |
| 36 | + if err := validateStackName(opts.namespace); err != nil { |
23 | 37 | return err |
24 | 38 | } |
25 | | - return swarm.RunPS(cmd.Context(), dockerCli, opts) |
| 39 | + return runPS(cmd.Context(), dockerCLI, opts) |
26 | 40 | }, |
27 | 41 | ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
28 | | - return completeNames(dockerCli)(cmd, args, toComplete) |
| 42 | + return completeNames(dockerCLI)(cmd, args, toComplete) |
29 | 43 | }, |
30 | 44 | } |
31 | 45 | flags := cmd.Flags() |
32 | | - flags.BoolVar(&opts.NoTrunc, "no-trunc", false, "Do not truncate output") |
33 | | - flags.BoolVar(&opts.NoResolve, "no-resolve", false, "Do not map IDs to Names") |
34 | | - flags.VarP(&opts.Filter, "filter", "f", "Filter output based on conditions provided") |
35 | | - flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display task IDs") |
36 | | - flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp) |
| 46 | + flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output") |
| 47 | + flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names") |
| 48 | + flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided") |
| 49 | + flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs") |
| 50 | + flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp) |
37 | 51 | return cmd |
38 | 52 | } |
| 53 | + |
| 54 | +// runPS is the swarm implementation of docker stack ps |
| 55 | +func runPS(ctx context.Context, dockerCLI command.Cli, opts psOptions) error { |
| 56 | + apiClient := dockerCLI.Client() |
| 57 | + tasks, err := apiClient.TaskList(ctx, client.TaskListOptions{ |
| 58 | + Filters: getStackFilterFromOpt(opts.namespace, opts.filter), |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + if len(tasks) == 0 { |
| 65 | + return fmt.Errorf("nothing found in stack: %s", opts.namespace) |
| 66 | + } |
| 67 | + |
| 68 | + if opts.format == "" { |
| 69 | + opts.format = task.DefaultFormat(dockerCLI.ConfigFile(), opts.quiet) |
| 70 | + } |
| 71 | + |
| 72 | + return task.Print(ctx, dockerCLI, tasks, idresolver.New(apiClient, opts.noResolve), !opts.noTrunc, opts.quiet, opts.format) |
| 73 | +} |
0 commit comments