github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/stack/ps.go (about) 1 // +build experimental 2 3 package stack 4 5 import ( 6 "fmt" 7 8 "golang.org/x/net/context" 9 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/swarm" 12 "github.com/docker/docker/cli" 13 "github.com/docker/docker/cli/command" 14 "github.com/docker/docker/cli/command/idresolver" 15 "github.com/docker/docker/cli/command/task" 16 "github.com/docker/docker/opts" 17 "github.com/spf13/cobra" 18 ) 19 20 type psOptions struct { 21 all bool 22 filter opts.FilterOpt 23 noTrunc bool 24 namespace string 25 noResolve bool 26 } 27 28 func newPsCommand(dockerCli *command.DockerCli) *cobra.Command { 29 opts := psOptions{filter: opts.NewFilterOpt()} 30 31 cmd := &cobra.Command{ 32 Use: "ps [OPTIONS] STACK", 33 Short: "List the tasks in the stack", 34 Args: cli.ExactArgs(1), 35 RunE: func(cmd *cobra.Command, args []string) error { 36 opts.namespace = args[0] 37 return runPS(dockerCli, opts) 38 }, 39 } 40 flags := cmd.Flags() 41 flags.BoolVarP(&opts.all, "all", "a", false, "Display all tasks") 42 flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output") 43 flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names") 44 flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided") 45 46 return cmd 47 } 48 49 func runPS(dockerCli *command.DockerCli, opts psOptions) error { 50 namespace := opts.namespace 51 client := dockerCli.Client() 52 ctx := context.Background() 53 54 filter := opts.filter.Value() 55 filter.Add("label", labelNamespace+"="+opts.namespace) 56 if !opts.all && !filter.Include("desired-state") { 57 filter.Add("desired-state", string(swarm.TaskStateRunning)) 58 filter.Add("desired-state", string(swarm.TaskStateAccepted)) 59 } 60 61 tasks, err := client.TaskList(ctx, types.TaskListOptions{Filter: filter}) 62 if err != nil { 63 return err 64 } 65 66 if len(tasks) == 0 { 67 fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace) 68 return nil 69 } 70 71 return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc) 72 }