github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/stack/swarm/ps.go (about)

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