hooks: pass command execution error to plugins

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
Laura Brehm 2024-04-22 17:12:53 +01:00
parent d8fc76ea56
commit 43cb06e1ae
No known key found for this signature in database
GPG Key ID: CFBF847B4A313468
3 changed files with 21 additions and 15 deletions

View File

@ -18,19 +18,20 @@ type HookPluginData struct {
// which is currently being invoked. If a hook for `docker context` is // which is currently being invoked. If a hook for `docker context` is
// configured and the user executes `docker context ls`, the plugin will // configured and the user executes `docker context ls`, the plugin will
// be invoked with `context`. // be invoked with `context`.
RootCmd string RootCmd string
Flags map[string]string Flags map[string]string
CommandError string
} }
// RunCLICommandHooks is the entrypoint into the hooks execution flow after // RunCLICommandHooks is the entrypoint into the hooks execution flow after
// 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) { func RunCLICommandHooks(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) runHooks(dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage)
} }
// RunPluginHooks is the entrypoint for the hooks execution flow // RunPluginHooks is the entrypoint for the hooks execution flow
@ -39,16 +40,16 @@ func RunPluginHooks(dockerCli command.Cli, rootCmd, subCommand *cobra.Command, a
commandName := strings.Join(args, " ") commandName := strings.Join(args, " ")
flags := getNaiveFlags(args) flags := getNaiveFlags(args)
runHooks(dockerCli, rootCmd, subCommand, commandName, flags) runHooks(dockerCli, rootCmd, subCommand, commandName, flags, "")
} }
func runHooks(dockerCli command.Cli, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string) { func runHooks(dockerCli command.Cli, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {
nextSteps := invokeAndCollectHooks(dockerCli, rootCmd, subCommand, invokedCommand, flags) nextSteps := invokeAndCollectHooks(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) []string { func invokeAndCollectHooks(dockerCli command.Cli, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string {
pluginsCfg := dockerCli.ConfigFile().Plugins pluginsCfg := dockerCli.ConfigFile().Plugins
if pluginsCfg == nil { if pluginsCfg == nil {
return nil return nil
@ -66,7 +67,11 @@ func invokeAndCollectHooks(dockerCli command.Cli, rootCmd, subCmd *cobra.Command
continue continue
} }
hookReturn, err := p.RunHook(match, flags) hookReturn, err := p.RunHook(HookPluginData{
RootCmd: match,
Flags: flags,
CommandError: cmdErrorMessage,
})
if err != nil { if err != nil {
// skip misbehaving plugins, but don't halt execution // skip misbehaving plugins, but don't halt execution
continue continue

View File

@ -105,11 +105,8 @@ 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(cmdName string, flags map[string]string) ([]byte, error) { func (p *Plugin) RunHook(hookData HookPluginData) ([]byte, error) {
hDataBytes, err := json.Marshal(HookPluginData{ hDataBytes, err := json.Marshal(hookData)
RootCmd: cmdName,
Flags: flags,
})
if err != nil { if err != nil {
return nil, wrapAsPluginError(err, "failed to marshall hook data") return nil, wrapAsPluginError(err, "failed to marshall hook data")
} }

View File

@ -357,7 +357,11 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
// If the command is being executed in an interactive terminal // If the command is being executed in an interactive terminal
// and hook are enabled, run the plugin hooks. // and hook are enabled, run the plugin hooks.
if dockerCli.HooksEnabled() && dockerCli.Out().IsTerminal() && subCommand != nil { if dockerCli.HooksEnabled() && dockerCli.Out().IsTerminal() && subCommand != nil {
pluginmanager.RunCLICommandHooks(dockerCli, cmd, subCommand) var errMessage string
if err != nil {
errMessage = err.Error()
}
pluginmanager.RunCLICommandHooks(dockerCli, cmd, subCommand, errMessage)
} }
return err return err