-
Notifications
You must be signed in to change notification settings - Fork 2
feat(binary): onPost hook — runs after install/update #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c4b2e42
feat(binary): onPost hook for binaries — runs after install/update
fentas 0337f17
fix(binary): review round 1 — hook only on actual download + io.Writer
fentas b676747
fix(binary): macOS symlink in TestRunHook_RunsInDir
fentas ed6a126
fix(binary): round 2 — filter parent B_ env, use b.OnPost in addToConfig
fentas e32275e
perf(update): round 3 — skip SHA hash when no hook + rename shadow
fentas fa49f4f
fix(update): round 4 — handle missing binary + tighten env comment
fentas d39d004
fix(update): round 5 — best-effort hook on hash failure
fentas ff44eb3
fix(binary): round 6 — route hook stdout to ErrOut to avoid progress …
fentas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package binary | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| ) | ||
|
|
||
| // RunHook executes a shell command with B_EVENT, B_NAME, B_VERSION, B_FILE | ||
| // env vars set. dir is the working directory (project root). stdout/stderr | ||
| // accept io.Writer so callers can route through the CLI's IO streams | ||
| // (respecting --quiet / output capture). nil writers default to io.Discard. | ||
| // Returns nil on success, the exec error on failure. Callers decide whether | ||
| // to treat the error as fatal or as a warning. | ||
| // | ||
| // The command runs via "sh -c" — hooks are POSIX-only. This is consistent | ||
| // with the existing env onPreSync/onPostSync hooks in pkg/env/env.go. | ||
| func RunHook(command, dir, event, name, version, file string, stdout, stderr io.Writer) error { | ||
| if command == "" { | ||
| return nil | ||
| } | ||
| if stdout == nil { | ||
| stdout = io.Discard | ||
| } | ||
| if stderr == nil { | ||
| stderr = io.Discard | ||
| } | ||
| cmd := exec.Command("sh", "-c", command) | ||
| cmd.Dir = dir | ||
| cmd.Stdout = stdout | ||
| cmd.Stderr = stderr | ||
| // Build env from parent process, filtering out the four hook-specific | ||
| // variables (B_EVENT, B_NAME, B_VERSION, B_FILE) so our values take | ||
| // guaranteed precedence regardless of platform. | ||
| hookVars := map[string]bool{ | ||
| "B_EVENT=": true, "B_NAME=": true, "B_VERSION=": true, "B_FILE=": true, | ||
| } | ||
| env := make([]string, 0, len(os.Environ())+4) | ||
| for _, e := range os.Environ() { | ||
| skip := false | ||
| for prefix := range hookVars { | ||
| if len(e) >= len(prefix) && e[:len(prefix)] == prefix { | ||
| skip = true | ||
| break | ||
| } | ||
| } | ||
| if !skip { | ||
| env = append(env, e) | ||
| } | ||
| } | ||
| env = append(env, | ||
| fmt.Sprintf("B_EVENT=%s", event), | ||
| fmt.Sprintf("B_NAME=%s", name), | ||
| fmt.Sprintf("B_VERSION=%s", version), | ||
| fmt.Sprintf("B_FILE=%s", file), | ||
| ) | ||
|
fentas marked this conversation as resolved.
|
||
| cmd.Env = env | ||
| return cmd.Run() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package binary | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRunHook_SetsEnvVars(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| out := filepath.Join(tmp, "env.txt") | ||
|
|
||
| cmd := `printf "%s\n%s\n%s\n%s" "$B_EVENT" "$B_NAME" "$B_VERSION" "$B_FILE" > ` + out | ||
| if err := RunHook(cmd, tmp, "install", "kubectl", "v1.28.0", "/usr/local/bin/kubectl", os.Stdout, os.Stderr); err != nil { | ||
| t.Fatalf("RunHook: %v", err) | ||
| } | ||
|
|
||
| data, err := os.ReadFile(out) | ||
| if err != nil { | ||
| t.Fatalf("read: %v", err) | ||
| } | ||
| want := "install\nkubectl\nv1.28.0\n/usr/local/bin/kubectl" | ||
| if string(data) != want { | ||
| t.Errorf("env vars:\ngot: %q\nwant: %q", data, want) | ||
| } | ||
| } | ||
|
|
||
| func TestRunHook_EmptyIsNoOp(t *testing.T) { | ||
| if err := RunHook("", t.TempDir(), "install", "x", "v1", "/x", nil, nil); err != nil { | ||
| t.Errorf("empty hook should be no-op, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestRunHook_NonZeroExitReturnsError(t *testing.T) { | ||
| if err := RunHook("exit 1", t.TempDir(), "update", "x", "v1", "/x", nil, nil); err == nil { | ||
| t.Error("expected error on non-zero exit") | ||
| } | ||
| } | ||
|
|
||
| func TestRunHook_RunsInDir(t *testing.T) { | ||
| tmp := t.TempDir() | ||
| // Resolve symlinks so the comparison works on macOS where | ||
| // /var → /private/var and t.TempDir() returns the unresolved path. | ||
| realTmp, err := filepath.EvalSymlinks(tmp) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| out := filepath.Join(tmp, "pwd.txt") | ||
| cmd := "pwd > " + out | ||
| if err := RunHook(cmd, tmp, "install", "x", "v1", "/x", nil, nil); err != nil { | ||
| t.Fatalf("RunHook: %v", err) | ||
| } | ||
| data, err := os.ReadFile(out) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := strings.TrimSpace(string(data)) | ||
| if got != realTmp { | ||
| t.Errorf("hook ran in %q, want %q", got, realTmp) | ||
| } | ||
| } | ||
|
|
||
| func TestRunHook_WritesToProvidedStreams(t *testing.T) { | ||
| var out bytes.Buffer | ||
| if err := RunHook("echo hello", t.TempDir(), "install", "x", "v1", "/x", &out, nil); err != nil { | ||
| t.Fatalf("RunHook: %v", err) | ||
| } | ||
| if out.String() != "hello\n" { | ||
| t.Errorf("stdout = %q, want %q", out.String(), "hello\n") | ||
| } | ||
| } | ||
|
|
||
| func TestRunHook_NilWritersDefaultToDiscard(t *testing.T) { | ||
| // Should not panic with nil writers. | ||
| if err := RunHook("echo ok", t.TempDir(), "install", "x", "v1", "/x", nil, nil); err != nil { | ||
| t.Errorf("unexpected error: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.