github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/cli/command/node/ps.go (about)

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