RunHook executes the plugin's hooks command and returns its unprocessed output.
(ctx context.Context, hookData hooks.Request)
| 156 | // RunHook executes the plugin's hooks command |
| 157 | // and returns its unprocessed output. |
| 158 | func (p *Plugin) RunHook(ctx context.Context, hookData hooks.Request) ([]byte, error) { |
| 159 | hDataBytes, err := json.Marshal(hookData) |
| 160 | if err != nil { |
| 161 | return nil, wrapAsPluginError(err, "failed to marshall hook data") |
| 162 | } |
| 163 | |
| 164 | pCmd := exec.CommandContext(ctx, p.Path, p.Name, metadata.HookSubcommandName, string(hDataBytes)) // #nosec G204 -- ignore "Subprocess launched with a potential tainted input or cmd arguments" |
| 165 | pCmd.Env = os.Environ() |
| 166 | pCmd.Env = append(pCmd.Env, metadata.ReexecEnvvar+"="+os.Args[0]) |
| 167 | |
| 168 | out, err := pCmd.Output() |
| 169 | if err != nil { |
| 170 | var exitErr *exec.ExitError |
| 171 | if errors.As(err, &exitErr) { |
| 172 | return nil, wrapAsPluginError(err, "plugin hook subcommand exited unsuccessfully") |
| 173 | } |
| 174 | return nil, wrapAsPluginError(err, "failed to execute plugin hook subcommand: "+pCmd.String()) |
| 175 | } |
| 176 | return out, nil |
| 177 | } |
| 178 | |
| 179 | // pluginNameFormat is used as part of errors for invalid plugin-names. |
| 180 | // We should consider making this less technical ("must start with "a-z", |
no test coverage detected