feat: wire ctx into plugin hooks

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
This commit is contained in:
Alano Terblanche 2024-04-26 13:03:56 +02:00
parent 7f15dfa4d5
commit 1d666b4105
No known key found for this signature in database
GPG Key ID: 0E8FACD1BA98DE27
3 changed files with 21 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package manager package manager
import ( import (
"context"
"encoding/json" "encoding/json"
"strings" "strings"
@ -27,29 +28,36 @@ type HookPluginData struct {
// a main CLI command was executed. It calls the hook subcommand for all // a main CLI command was executed. It calls the hook subcommand for all
// present CLI plugins that declare support for hooks in their metadata and // present CLI plugins that declare support for hooks in their metadata and
// parses/prints their responses. // parses/prints their responses.
func RunCLICommandHooks(dockerCli command.Cli, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) { func RunCLICommandHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) {
commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ") commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ")
flags := getCommandFlags(subCommand) flags := getCommandFlags(subCommand)
runHooks(dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage) runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage)
} }
// RunPluginHooks is the entrypoint for the hooks execution flow // RunPluginHooks is the entrypoint for the hooks execution flow
// after a plugin command was just executed by the CLI. // after a plugin command was just executed by the CLI.
func RunPluginHooks(dockerCli command.Cli, rootCmd, subCommand *cobra.Command, args []string) { func RunPluginHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, args []string) {
commandName := strings.Join(args, " ") commandName := strings.Join(args, " ")
flags := getNaiveFlags(args) flags := getNaiveFlags(args)
runHooks(dockerCli, rootCmd, subCommand, commandName, flags, "") runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, "")
} }
func runHooks(dockerCli command.Cli, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) { func runHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {
nextSteps := invokeAndCollectHooks(dockerCli, rootCmd, subCommand, invokedCommand, flags, cmdErrorMessage) nextSteps := invokeAndCollectHooks(ctx, dockerCli, rootCmd, subCommand, invokedCommand, flags, cmdErrorMessage)
hooks.PrintNextSteps(dockerCli.Err(), nextSteps) hooks.PrintNextSteps(dockerCli.Err(), nextSteps)
} }
func invokeAndCollectHooks(dockerCli command.Cli, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string { func invokeAndCollectHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string {
// check if the context was cancelled before invoking hooks
select {
case <-ctx.Done():
return nil
default:
}
pluginsCfg := dockerCli.ConfigFile().Plugins pluginsCfg := dockerCli.ConfigFile().Plugins
if pluginsCfg == nil { if pluginsCfg == nil {
return nil return nil
@ -67,7 +75,7 @@ func invokeAndCollectHooks(dockerCli command.Cli, rootCmd, subCmd *cobra.Command
continue continue
} }
hookReturn, err := p.RunHook(HookPluginData{ hookReturn, err := p.RunHook(ctx, HookPluginData{
RootCmd: match, RootCmd: match,
Flags: flags, Flags: flags,
CommandError: cmdErrorMessage, CommandError: cmdErrorMessage,

View File

@ -1,6 +1,7 @@
package manager package manager
import ( import (
"context"
"encoding/json" "encoding/json"
"os" "os"
"os/exec" "os/exec"
@ -105,13 +106,13 @@ func newPlugin(c Candidate, cmds []*cobra.Command) (Plugin, error) {
// RunHook executes the plugin's hooks command // RunHook executes the plugin's hooks command
// and returns its unprocessed output. // and returns its unprocessed output.
func (p *Plugin) RunHook(hookData HookPluginData) ([]byte, error) { func (p *Plugin) RunHook(ctx context.Context, hookData HookPluginData) ([]byte, error) {
hDataBytes, err := json.Marshal(hookData) hDataBytes, err := json.Marshal(hookData)
if err != nil { if err != nil {
return nil, wrapAsPluginError(err, "failed to marshall hook data") return nil, wrapAsPluginError(err, "failed to marshall hook data")
} }
pCmd := exec.Command(p.Path, p.Name, HookSubcommandName, string(hDataBytes)) pCmd := exec.CommandContext(ctx, p.Path, p.Name, HookSubcommandName, string(hDataBytes))
pCmd.Env = os.Environ() pCmd.Env = os.Environ()
pCmd.Env = append(pCmd.Env, ReexecEnvvar+"="+os.Args[0]) pCmd.Env = append(pCmd.Env, ReexecEnvvar+"="+os.Args[0])
hookCmdOutput, err := pCmd.Output() hookCmdOutput, err := pCmd.Output()

View File

@ -337,7 +337,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
err := tryPluginRun(dockerCli, cmd, args[0], envs) err := tryPluginRun(dockerCli, cmd, args[0], envs)
if err == nil { if err == nil {
if dockerCli.HooksEnabled() && dockerCli.Out().IsTerminal() && ccmd != nil { if dockerCli.HooksEnabled() && dockerCli.Out().IsTerminal() && ccmd != nil {
pluginmanager.RunPluginHooks(dockerCli, cmd, ccmd, args) pluginmanager.RunPluginHooks(ctx, dockerCli, cmd, ccmd, args)
} }
return nil return nil
} }
@ -362,7 +362,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
if err != nil { if err != nil {
errMessage = err.Error() errMessage = err.Error()
} }
pluginmanager.RunCLICommandHooks(dockerCli, cmd, subCommand, errMessage) pluginmanager.RunCLICommandHooks(ctx, dockerCli, cmd, subCommand, errMessage)
} }
return err return err