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

     1  package node
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/cli/cli/command/idresolver"
    10  	"github.com/docker/cli/cli/command/task"
    11  	"github.com/docker/cli/opts"
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/api/types/swarm"
    14  	"github.com/pkg/errors"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type psOptions struct {
    19  	nodeIDs   []string
    20  	noResolve bool
    21  	noTrunc   bool
    22  	quiet     bool
    23  	format    string
    24  	filter    opts.FilterOpt
    25  }
    26  
    27  func newPsCommand(dockerCli command.Cli) *cobra.Command {
    28  	options := psOptions{filter: opts.NewFilterOpt()}
    29  
    30  	cmd := &cobra.Command{
    31  		Use:   "ps [OPTIONS] [NODE...]",
    32  		Short: "List tasks running on one or more nodes, defaults to current node",
    33  		Args:  cli.RequiresMinArgs(0),
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			options.nodeIDs = []string{"self"}
    36  
    37  			if len(args) != 0 {
    38  				options.nodeIDs = args
    39  			}
    40  
    41  			return runPs(dockerCli, options)
    42  		},
    43  	}
    44  	flags := cmd.Flags()
    45  	flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
    46  	flags.BoolVar(&options.noResolve, "no-resolve", false, "Do not map IDs to Names")
    47  	flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
    48  	flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
    49  	flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
    50  
    51  	return cmd
    52  }
    53  
    54  func runPs(dockerCli command.Cli, options psOptions) error {
    55  	client := dockerCli.Client()
    56  	ctx := context.Background()
    57  
    58  	var (
    59  		errs  []string
    60  		tasks []swarm.Task
    61  	)
    62  
    63  	for _, nodeID := range options.nodeIDs {
    64  		nodeRef, err := Reference(ctx, client, nodeID)
    65  		if err != nil {
    66  			errs = append(errs, err.Error())
    67  			continue
    68  		}
    69  
    70  		node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
    71  		if err != nil {
    72  			errs = append(errs, err.Error())
    73  			continue
    74  		}
    75  
    76  		filter := options.filter.Value()
    77  		filter.Add("node", node.ID)
    78  
    79  		nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
    80  		if err != nil {
    81  			errs = append(errs, err.Error())
    82  			continue
    83  		}
    84  
    85  		tasks = append(tasks, nodeTasks...)
    86  	}
    87  
    88  	format := options.format
    89  	if len(format) == 0 {
    90  		format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
    91  	}
    92  
    93  	if len(errs) == 0 || len(tasks) != 0 {
    94  		if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
    95  			errs = append(errs, err.Error())
    96  		}
    97  	}
    98  
    99  	if len(errs) > 0 {
   100  		return errors.Errorf("%s", strings.Join(errs, "\n"))
   101  	}
   102  
   103  	return nil
   104  }