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