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