From 13fc55561e34caadc387d11f8ddf509adf8e07c6 Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Thu, 30 Oct 2025 11:42:07 +0100 Subject: [PATCH] chore: improve how we pipe the json --- internal/hooks/hooks.go | 4 +--- internal/shell/shell.go | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/internal/hooks/hooks.go b/internal/hooks/hooks.go index d9ffff92323869575f3d522723be661742b6640c..2011d3e31a71e190859fdecb0043edc1672d261d 100644 --- a/internal/hooks/hooks.go +++ b/internal/hooks/hooks.go @@ -169,9 +169,7 @@ func (e *Executor) executeHook(ctx context.Context, hook config.Hook, hookCtx Ho "timeout", timeout, ) - fullCommand := fmt.Sprintf("%s <<'CRUSH_HOOK_EOF'\n%s\nCRUSH_HOOK_EOF\n", hook.Command, string(contextJSON)) - - stdout, stderr, err := e.shell.Exec(execCtx, fullCommand) + stdout, stderr, err := e.shell.ExecWithStdin(execCtx, hook.Command, string(contextJSON)) if err != nil { return fmt.Errorf("hook command failed: %w: stdout=%s stderr=%s", err, stdout, stderr) } diff --git a/internal/shell/shell.go b/internal/shell/shell.go index 5a10be9537714162e4d5ed25360b42690395793f..7f505d6b020c719903f9a18144430d6c3e6b1187 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -15,6 +15,7 @@ import ( "context" "errors" "fmt" + "io" "os" "slices" "strings" @@ -100,7 +101,15 @@ func (s *Shell) Exec(ctx context.Context, command string) (string, string, error s.mu.Lock() defer s.mu.Unlock() - return s.execPOSIX(ctx, command) + return s.execPOSIX(ctx, command, nil) +} + +// ExecWithStdin executes a command in the shell with the given stdin +func (s *Shell) ExecWithStdin(ctx context.Context, command string, stdin string) (string, string, error) { + s.mu.Lock() + defer s.mu.Unlock() + + return s.execPOSIX(ctx, command, strings.NewReader(stdin)) } // GetWorkingDir returns the current working directory @@ -229,15 +238,19 @@ func (s *Shell) blockHandler() func(next interp.ExecHandlerFunc) interp.ExecHand } // execPOSIX executes commands using POSIX shell emulation (cross-platform) -func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string, error) { +func (s *Shell) execPOSIX(ctx context.Context, command string, stdin *strings.Reader) (string, string, error) { line, err := syntax.NewParser().Parse(strings.NewReader(command), "") if err != nil { return "", "", fmt.Errorf("could not parse command: %w", err) } var stdout, stderr bytes.Buffer + var stdinReader io.Reader + if stdin != nil { + stdinReader = stdin + } runner, err := interp.New( - interp.StdIO(nil, &stdout, &stderr), + interp.StdIO(stdinReader, &stdout, &stderr), interp.Interactive(false), interp.Env(expand.ListEnviron(s.env...)), interp.Dir(s.cwd),