github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/stack/swarm/ps.go (about)

     1  package swarm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/idresolver"
     9  	"github.com/docker/cli/cli/command/stack/options"
    10  	"github.com/docker/cli/cli/command/task"
    11  	"github.com/docker/docker/api/types"
    12  )
    13  
    14  // RunPS is the swarm implementation of docker stack ps
    15  func RunPS(dockerCli command.Cli, opts options.PS) error {
    16  	filter := getStackFilterFromOpt(opts.Namespace, opts.Filter)
    17  
    18  	ctx := context.Background()
    19  	client := dockerCli.Client()
    20  	tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	if len(tasks) == 0 {
    26  		return fmt.Errorf("nothing found in stack: %s", opts.Namespace)
    27  	}
    28  
    29  	format := opts.Format
    30  	if len(format) == 0 {
    31  		format = task.DefaultFormat(dockerCli.ConfigFile(), opts.Quiet)
    32  	}
    33  
    34  	return task.Print(ctx, dockerCli, tasks, idresolver.New(client, opts.NoResolve), !opts.NoTrunc, opts.Quiet, format)
    35  }