Files
docker-cli/cli/command/node/ps.go
Sebastiaan van Stijn 0adaf6be3b verify that DisableFlagsInUseLine is set for all commands
This replaces the visitAll recursive function with a test that verifies that
the option is set for all commands and subcommands, so that it doesn't have
to be modified at runtime.

We currently still have to loop over all functions for the setValidateArgs
call, but that can be looked at separately.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-01 09:39:46 +02:00

113 lines
3.0 KiB
Go

package node
import (
"context"
"strings"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/command/task"
"github.com/docker/cli/opts"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type psOptions struct {
nodeIDs []string
noResolve bool
noTrunc bool
quiet bool
format string
filter opts.FilterOpt
}
func newPsCommand(dockerCLI command.Cli) *cobra.Command {
options := psOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{
Use: "ps [OPTIONS] [NODE...]",
Short: "List tasks running on one or more nodes, defaults to current node",
Args: cli.RequiresMinArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
options.nodeIDs = []string{"self"}
if len(args) != 0 {
options.nodeIDs = args
}
return runPs(cmd.Context(), dockerCLI, options)
},
ValidArgsFunction: completeNodeNames(dockerCLI),
DisableFlagsInUseLine: true,
}
flags := cmd.Flags()
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
flags.BoolVar(&options.noResolve, "no-resolve", false, "Do not map IDs to Names")
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
flags.VisitAll(func(flag *pflag.Flag) {
// Set a default completion function if none was set. We don't look
// up if it does already have one set, because Cobra does this for
// us, and returns an error (which we ignore for this reason).
_ = cmd.RegisterFlagCompletionFunc(flag.Name, cobra.NoFileCompletions)
})
return cmd
}
func runPs(ctx context.Context, dockerCLI command.Cli, options psOptions) error {
apiClient := dockerCLI.Client()
var (
errs []string
tasks []swarm.Task
)
for _, nodeID := range options.nodeIDs {
nodeRef, err := Reference(ctx, apiClient, nodeID)
if err != nil {
errs = append(errs, err.Error())
continue
}
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeRef)
if err != nil {
errs = append(errs, err.Error())
continue
}
filter := options.filter.Value()
filter.Add("node", node.ID)
nodeTasks, err := apiClient.TaskList(ctx, client.TaskListOptions{Filters: filter})
if err != nil {
errs = append(errs, err.Error())
continue
}
tasks = append(tasks, nodeTasks...)
}
format := options.format
if len(format) == 0 {
format = task.DefaultFormat(dockerCLI.ConfigFile(), options.quiet)
}
if len(errs) == 0 || len(tasks) != 0 {
if err := task.Print(ctx, dockerCLI, tasks, idresolver.New(apiClient, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return errors.Errorf("%s", strings.Join(errs, "\n"))
}
return nil
}