ps.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/idresolver"
  9. "github.com/docker/docker/cli/command/task"
  10. "github.com/docker/docker/opts"
  11. "github.com/spf13/cobra"
  12. )
  13. type psOptions struct {
  14. filter opts.FilterOpt
  15. noTrunc bool
  16. namespace string
  17. noResolve bool
  18. }
  19. func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
  20. opts := psOptions{filter: opts.NewFilterOpt()}
  21. cmd := &cobra.Command{
  22. Use: "ps [OPTIONS] STACK",
  23. Short: "List the tasks in the stack",
  24. Args: cli.ExactArgs(1),
  25. RunE: func(cmd *cobra.Command, args []string) error {
  26. opts.namespace = args[0]
  27. return runPS(dockerCli, opts)
  28. },
  29. }
  30. flags := cmd.Flags()
  31. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
  32. flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
  33. flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
  34. return cmd
  35. }
  36. func runPS(dockerCli *command.DockerCli, opts psOptions) error {
  37. namespace := opts.namespace
  38. client := dockerCli.Client()
  39. ctx := context.Background()
  40. filter := getStackFilterFromOpt(opts.namespace, opts.filter)
  41. tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
  42. if err != nil {
  43. return err
  44. }
  45. if len(tasks) == 0 {
  46. fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
  47. return nil
  48. }
  49. return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
  50. }