Skip to content

Commit

Permalink
Instrument plugin command executions using our utils
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Petito <chrisjpetito@gmail.com>
  • Loading branch information
krissetto committed Apr 30, 2024
1 parent 49dbdab commit 92c5499
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions cli-plugins/manager/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err e
cargs = append(cargs, args...)
cargs = append(cargs, toComplete)
os.Args = cargs
runCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
pluginRunCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
if runErr != nil {
return nil, cobra.ShellCompDirectiveError
}
runErr = runCommand.Run()
runCommand := command.InstrumentPluginCommand(pluginRunCommand, dockerCli)
runErr = runCommand.TimedRun(cmd.Context())
if runErr == nil {
os.Exit(0) // plugin already rendered complete data
}
Expand Down
19 changes: 12 additions & 7 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ func setupHelpCommand(dockerCli command.Cli, rootCmd, helpCmd *cobra.Command) {
helpCmd.Run = nil
helpCmd.RunE = func(c *cobra.Command, args []string) error {
if len(args) > 0 {
helpcmd, err := pluginmanager.PluginRunCommand(dockerCli, args[0], rootCmd)
helpRunCmd, err := pluginmanager.PluginRunCommand(dockerCli, args[0], rootCmd)
helpcmd := command.InstrumentPluginCommand(helpRunCmd, dockerCli)
if err == nil {
return helpcmd.Run()
return helpcmd.TimedRun(c.Context())
}
if !pluginmanager.IsNotFound(err) {
return errors.Errorf("unknown help topic: %v", strings.Join(args, " "))
Expand All @@ -159,11 +160,12 @@ func tryRunPluginHelp(dockerCli command.Cli, ccmd *cobra.Command, cargs []string
if err != nil {
return err
}
helpcmd, err := pluginmanager.PluginRunCommand(dockerCli, cmd.Name(), root)
helpRunCmd, err := pluginmanager.PluginRunCommand(dockerCli, cmd.Name(), root)
helpcmd := command.InstrumentPluginCommand(helpRunCmd, dockerCli)
if err != nil {
return err
}
return helpcmd.Run()
return helpcmd.TimedRun(ccmd.Context())
}

func setHelpFunc(dockerCli command.Cli, cmd *cobra.Command) {
Expand Down Expand Up @@ -225,10 +227,11 @@ func setValidateArgs(dockerCli command.Cli, cmd *cobra.Command) {
}

func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string, envs []string) error {
plugincmd, err := pluginmanager.PluginRunCommand(dockerCli, subcommand, cmd)
pluginRunCmd, err := pluginmanager.PluginRunCommand(dockerCli, subcommand, cmd)
if err != nil {
return err
}
plugincmd := command.InstrumentPluginCommand(pluginRunCmd, dockerCli)

// Establish the plugin socket, adding it to the environment under a
// well-known key if successful.
Expand Down Expand Up @@ -279,7 +282,7 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string,
}
}()

if err := plugincmd.Run(); err != nil {
if err := plugincmd.TimedRun(cmd.Context()); err != nil {
statusCode := 1
exitErr, ok := err.(*exec.ExitError)
if !ok {
Expand Down Expand Up @@ -314,6 +317,8 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {

dockerCli.InstrumentCobraCommands(cmd)

cmd.SetContext(ctx)

var envs []string
args, os.Args, envs, err = processAliases(dockerCli, cmd, args, os.Args)
if err != nil {
Expand Down Expand Up @@ -354,7 +359,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
// We've parsed global args already, so reset args to those
// which remain.
cmd.SetArgs(args)
err = cmd.ExecuteContext(ctx)
err = cmd.Execute()

// If the command is being executed in an interactive terminal
// and hook are enabled, run the plugin hooks.
Expand Down

0 comments on commit 92c5499

Please sign in to comment.