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>
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package container
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/docker/cli/cli/command/completion"
|
|
"github.com/docker/cli/cli/command/formatter/tabwriter"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type topOptions struct {
|
|
container string
|
|
|
|
args []string
|
|
}
|
|
|
|
// newTopCommand creates a new cobra.Command for "docker container top",
|
|
func newTopCommand(dockerCLI command.Cli) *cobra.Command {
|
|
var opts topOptions
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "top CONTAINER [ps OPTIONS]",
|
|
Short: "Display the running processes of a container",
|
|
Args: cli.RequiresMinArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
opts.container = args[0]
|
|
opts.args = args[1:]
|
|
return runTop(cmd.Context(), dockerCLI, &opts)
|
|
},
|
|
Annotations: map[string]string{
|
|
"aliases": "docker container top, docker top",
|
|
},
|
|
ValidArgsFunction: completion.ContainerNames(dockerCLI, false),
|
|
DisableFlagsInUseLine: true,
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
flags.SetInterspersed(false)
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runTop(ctx context.Context, dockerCli command.Cli, opts *topOptions) error {
|
|
procList, err := dockerCli.Client().ContainerTop(ctx, opts.container, opts.args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
|
|
fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))
|
|
|
|
for _, proc := range procList.Processes {
|
|
fmt.Fprintln(w, strings.Join(proc, "\t"))
|
|
}
|
|
w.Flush()
|
|
return nil
|
|
}
|