ps.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package stack
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/docker/docker/cli/command/formatter"
  9. "github.com/docker/docker/cli/command/idresolver"
  10. "github.com/docker/docker/cli/command/task"
  11. "github.com/docker/docker/opts"
  12. "github.com/spf13/cobra"
  13. )
  14. type psOptions struct {
  15. filter opts.FilterOpt
  16. noTrunc bool
  17. namespace string
  18. noResolve bool
  19. quiet bool
  20. format string
  21. }
  22. func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
  23. opts := psOptions{filter: opts.NewFilterOpt()}
  24. cmd := &cobra.Command{
  25. Use: "ps [OPTIONS] STACK",
  26. Short: "List the tasks in the stack",
  27. Args: cli.ExactArgs(1),
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. opts.namespace = args[0]
  30. return runPS(dockerCli, opts)
  31. },
  32. }
  33. flags := cmd.Flags()
  34. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
  35. flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
  36. flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
  37. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
  38. flags.StringVar(&opts.format, "format", "", "Pretty-print tasks using a Go template")
  39. return cmd
  40. }
  41. func runPS(dockerCli *command.DockerCli, opts psOptions) error {
  42. namespace := opts.namespace
  43. client := dockerCli.Client()
  44. ctx := context.Background()
  45. filter := getStackFilterFromOpt(opts.namespace, opts.filter)
  46. tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
  47. if err != nil {
  48. return err
  49. }
  50. if len(tasks) == 0 {
  51. fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
  52. return nil
  53. }
  54. format := opts.format
  55. if len(format) == 0 {
  56. if len(dockerCli.ConfigFile().TasksFormat) > 0 && !opts.quiet {
  57. format = dockerCli.ConfigFile().TasksFormat
  58. } else {
  59. format = formatter.TableFormatKey
  60. }
  61. }
  62. return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), !opts.noTrunc, opts.quiet, format)
  63. }