From 50117590567998307eba377295a1ec24e214ccc7 Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Wed, 17 Apr 2024 16:57:44 +0100 Subject: [PATCH] hooks: set expected environment when executing During normal plugin execution (from the CLI), the CLI configures the plugin command it's about to execute in order to pass all environment variables on, as well as to set the ReExec env var that informs the plugin about how it was executed, and which plugins rely on to check whether they are being run standalone or not. This commit adds the same behavior to hook invocations, which is necessary for some plugins to know that they are not running standalone so that they expose their root command at the correct level. Signed-off-by: Laura Brehm --- cli-plugins/manager/plugin.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli-plugins/manager/plugin.go b/cli-plugins/manager/plugin.go index 88600f4e59..6e1ca9bbe5 100644 --- a/cli-plugins/manager/plugin.go +++ b/cli-plugins/manager/plugin.go @@ -2,6 +2,7 @@ package manager import ( "encoding/json" + "os" "os/exec" "path/filepath" "regexp" @@ -113,7 +114,10 @@ func (p *Plugin) RunHook(cmdName string, flags map[string]string) ([]byte, error return nil, wrapAsPluginError(err, "failed to marshall hook data") } - hookCmdOutput, err := exec.Command(p.Path, p.Name, HookSubcommandName, string(hDataBytes)).Output() + pCmd := exec.Command(p.Path, p.Name, HookSubcommandName, string(hDataBytes)) + pCmd.Env = os.Environ() + pCmd.Env = append(pCmd.Env, ReexecEnvvar+"="+os.Args[0]) + hookCmdOutput, err := pCmd.Output() if err != nil { return nil, wrapAsPluginError(err, "failed to execute plugin hook subcommand") }