2016-09-08 13:11:39 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
|
|
|
"github.com/docker/cli/cli/command/task"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/spf13/cobra"
|
2017-05-15 08:45:19 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type psOptions struct {
|
|
|
|
filter opts.FilterOpt
|
|
|
|
noTrunc bool
|
|
|
|
namespace string
|
|
|
|
noResolve bool
|
2016-11-07 00:54:40 -05:00
|
|
|
quiet bool
|
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func newPsCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := psOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ps [OPTIONS] STACK",
|
|
|
|
Short: "List the tasks in the stack",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.namespace = args[0]
|
|
|
|
return runPS(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
|
|
|
|
flags.BoolVar(&options.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runPS(dockerCli command.Cli, options psOptions) error {
|
|
|
|
namespace := options.namespace
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
filter := getStackFilterFromOpt(options.namespace, options.filter)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-11-01 10:01:16 -04:00
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tasks) == 0 {
|
2017-08-30 06:47:53 -04:00
|
|
|
return fmt.Errorf("nothing found in stack: %s", namespace)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-11-07 00:54:40 -05:00
|
|
|
if len(format) == 0 {
|
2017-07-05 13:40:08 -04:00
|
|
|
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
|
2016-11-07 00:54:40 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
return task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|