Skip to content

Commit 6847b55

Browse files
committed
Fix: run plugin hooks on command failure, not just success
Signed-off-by: Derek Misler <derek.misler@docker.com>
1 parent e30ce84 commit 6847b55

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

cli-plugins/manager/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func RunCLICommandHooks(ctx context.Context, dockerCLI config.Provider, rootCmd,
4242

4343
// RunPluginHooks is the entrypoint for the hooks execution flow
4444
// after a plugin command was just executed by the CLI.
45-
func RunPluginHooks(ctx context.Context, dockerCLI config.Provider, rootCmd, subCommand *cobra.Command, args []string) {
45+
func RunPluginHooks(ctx context.Context, dockerCLI config.Provider, rootCmd, subCommand *cobra.Command, args []string, cmdErrorMessage string) {
4646
commandName := strings.Join(args, " ")
4747
flags := getNaiveFlags(args)
4848

49-
runHooks(ctx, dockerCLI.ConfigFile(), rootCmd, subCommand, commandName, flags, "")
49+
runHooks(ctx, dockerCLI.ConfigFile(), rootCmd, subCommand, commandName, flags, cmdErrorMessage)
5050
}
5151

5252
func runHooks(ctx context.Context, cfg *configfile.ConfigFile, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {

cmd/docker/docker.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,14 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
480480
subCommand = ccmd
481481
if err != nil || pluginmanager.IsPluginCommand(ccmd) {
482482
err := tryPluginRun(ctx, dockerCli, cmd, args[0], envs)
483-
if err == nil {
484-
if ccmd != nil && dockerCli.Out().IsTerminal() && dockerCli.HooksEnabled() {
485-
pluginmanager.RunPluginHooks(ctx, dockerCli, cmd, ccmd, args)
483+
if ccmd != nil && dockerCli.Out().IsTerminal() && dockerCli.HooksEnabled() {
484+
var errMessage string
485+
if err != nil {
486+
errMessage = err.Error()
486487
}
488+
pluginmanager.RunPluginHooks(ctx, dockerCli, cmd, ccmd, args, errMessage)
489+
}
490+
if err == nil {
487491
return nil
488492
}
489493
if !errdefs.IsNotFound(err) {

0 commit comments

Comments
 (0)