-
Notifications
You must be signed in to change notification settings - Fork 92
refactor(cli): share Linux binary acquisition for run and vendoring #2015
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
4 commits
Select commit
Hold shift + click to select a range
a95eb33
refactor(cli): share Linux binary acquisition for run and vendoring
ifireball 2f32bdf
fix(cli): restore OIDC HTTP timeout and invalid-hex test
ifireball ce2c94d
fix(cli): address PR review findings for binary vendoring
ifireball db20367
fix(cli): drop duplicate vendor steps and reuse ValidArch
ifireball 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package binary | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // Source identifies how a Linux fullsend binary was obtained. | ||
| type Source int | ||
|
|
||
| const ( | ||
| SourceExplicitPath Source = iota | ||
| SourceCheckoutBuild | ||
| SourceReleaseDownload | ||
| ) | ||
|
|
||
| // AcquireResult holds the path to an acquired binary and metadata for callers. | ||
| type AcquireResult struct { | ||
| TmpDir string // caller must RemoveAll when non-empty | ||
| Path string | ||
| Source Source | ||
| } | ||
|
|
||
| // ResolveExplicit validates that path is a Linux ELF for arch. | ||
| func ResolveExplicit(path, arch string) error { | ||
| return ValidateLinuxBinary(path, arch) | ||
| } | ||
|
|
||
| // ResolveForRun obtains a Linux binary using the run policy: | ||
| // release download (if released) → cross-compile → latest release. | ||
| func ResolveForRun(version, arch string) (AcquireResult, error) { | ||
| tmpDir, err := os.MkdirTemp("", "fullsend-linux-*") | ||
| if err != nil { | ||
| return AcquireResult{}, fmt.Errorf("creating temp dir: %w", err) | ||
| } | ||
| binaryPath := filepath.Join(tmpDir, "fullsend") | ||
|
|
||
| // 1. Released version → download matching release asset. | ||
| if IsReleasedVersion(version) { | ||
| fmt.Fprintf(os.Stderr, "Downloading fullsend %s for linux/%s from GitHub Release...\n", version, arch) | ||
| if dlErr := DownloadRelease(version, arch, binaryPath); dlErr == nil { | ||
| fmt.Fprintf(os.Stderr, "Downloaded fullsend for linux/%s\n", arch) | ||
| return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceReleaseDownload}, nil | ||
| } else { | ||
| fmt.Fprintf(os.Stderr, "WARNING: release download failed: %v\n", dlErr) | ||
| } | ||
| } | ||
|
|
||
| // 2. Try cross-compilation (requires Go toolchain + module checkout). | ||
| fmt.Fprintf(os.Stderr, "Cross-compiling fullsend for linux/%s...\n", arch) | ||
| if ccErr := CrossCompile(CrossCompileOpts{ | ||
| Version: version, | ||
| Arch: arch, | ||
| DestPath: binaryPath, | ||
| VersionStamp: "-crosscompiled", | ||
| }); ccErr == nil { | ||
| fmt.Fprintf(os.Stderr, "Cross-compiled fullsend for linux/%s\n", arch) | ||
| return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceCheckoutBuild}, nil | ||
| } else { | ||
| fmt.Fprintf(os.Stderr, "WARNING: cross-compilation failed: %v\n", ccErr) | ||
| } | ||
|
ifireball marked this conversation as resolved.
|
||
|
|
||
| // 3. Last resort → download latest release. | ||
| fmt.Fprintf(os.Stderr, "Downloading latest fullsend release for linux/%s...\n", arch) | ||
| latestErr := DownloadLatestRelease(arch, binaryPath) | ||
| if latestErr == nil { | ||
| fmt.Fprintf(os.Stderr, "Downloaded latest fullsend for linux/%s\n", arch) | ||
| return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceReleaseDownload}, nil | ||
| } | ||
| fmt.Fprintf(os.Stderr, "WARNING: latest release download failed: %v\n", latestErr) | ||
|
|
||
| os.RemoveAll(tmpDir) | ||
| return AcquireResult{}, fmt.Errorf("all strategies failed for linux/%s: provide --fullsend-binary or install Go toolchain", arch) | ||
| } | ||
|
|
||
| // ResolveForVendor obtains a Linux binary using the vendoring policy: | ||
| // cross-compile from checkout → matching release (released CLI only) → fail. | ||
| // No latest-release fallback. | ||
| func ResolveForVendor(version, arch string) (AcquireResult, error) { | ||
| tmpDir, err := os.MkdirTemp("", "fullsend-linux-*") | ||
| if err != nil { | ||
| return AcquireResult{}, fmt.Errorf("creating temp dir: %w", err) | ||
| } | ||
| binaryPath := filepath.Join(tmpDir, "fullsend") | ||
|
|
||
| // 1. Cross-compile from checkout. | ||
| fmt.Fprintf(os.Stderr, "Cross-compiling fullsend for linux/%s...\n", arch) | ||
| if ccErr := CrossCompile(CrossCompileOpts{ | ||
| Version: version, | ||
| Arch: arch, | ||
| DestPath: binaryPath, | ||
| VersionStamp: "-vendored", | ||
| }); ccErr == nil { | ||
| fmt.Fprintf(os.Stderr, "Cross-compiled fullsend for linux/%s\n", arch) | ||
| return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceCheckoutBuild}, nil | ||
| } else { | ||
| fmt.Fprintf(os.Stderr, "WARNING: cross-compilation failed: %v\n", ccErr) | ||
| } | ||
|
|
||
| // 2. Release fetch only for released CLI versions. | ||
| if IsReleasedVersion(version) { | ||
| fmt.Fprintf(os.Stderr, "Downloading fullsend %s for linux/%s from GitHub Release...\n", version, arch) | ||
| if dlErr := DownloadRelease(version, arch, binaryPath); dlErr == nil { | ||
| fmt.Fprintf(os.Stderr, "Downloaded fullsend for linux/%s\n", arch) | ||
| return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceReleaseDownload}, nil | ||
| } else { | ||
| os.RemoveAll(tmpDir) | ||
| return AcquireResult{}, fmt.Errorf("cross-compilation unavailable and release download failed for v%s: %w", version, dlErr) | ||
| } | ||
| } | ||
|
|
||
| os.RemoveAll(tmpDir) | ||
| return AcquireResult{}, fmt.Errorf("cannot vendor binary: not in fullsend source tree and CLI version %s is a dev build — use --fullsend-binary, run from a checkout, or use a released CLI", version) | ||
| } | ||
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,64 @@ | ||
| package binary | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // CrossCompileOpts configures a cross-compilation build. | ||
| type CrossCompileOpts struct { | ||
| Version string // CLI version to embed (before stamp suffix) | ||
| Arch string | ||
| DestPath string | ||
| VersionStamp string // e.g. "-vendored", "-crosscompiled", or "" | ||
| } | ||
|
|
||
| // ModuleRoot returns the fullsend module root directory, or an error if not | ||
| // inside a Go module checkout. | ||
| func ModuleRoot() (string, error) { | ||
| goPath, lookErr := exec.LookPath("go") | ||
| if lookErr != nil { | ||
| return "", fmt.Errorf("Go toolchain not found: %w", lookErr) | ||
| } | ||
| modRootCmd := exec.Command(goPath, "env", "GOMOD") | ||
| modOutput, err := modRootCmd.Output() | ||
| if err != nil { | ||
| return "", fmt.Errorf("finding module root: %w", err) | ||
| } | ||
| modPath := strings.TrimSpace(string(modOutput)) | ||
| if modPath == "" || modPath == os.DevNull { | ||
| return "", fmt.Errorf("not in a Go module") | ||
| } | ||
| return filepath.Dir(modPath), nil | ||
| } | ||
|
|
||
| // CrossCompile builds a Linux fullsend binary and writes it to DestPath. | ||
| // Requires the Go toolchain and a fullsend module checkout (go env GOMOD). | ||
| func CrossCompile(opts CrossCompileOpts) error { | ||
| goPath, lookErr := exec.LookPath("go") | ||
| if lookErr != nil { | ||
| return fmt.Errorf("Go toolchain not found — install Go or use a released version of fullsend: %w", lookErr) | ||
| } | ||
|
|
||
| modRoot, err := ModuleRoot() | ||
| if err != nil { | ||
| return fmt.Errorf("not in a Go module — run from the fullsend source tree or use a released version: %w", err) | ||
| } | ||
|
|
||
| versionLD := opts.Version + opts.VersionStamp | ||
| buildCmd := exec.Command(goPath, "build", | ||
| "-ldflags", fmt.Sprintf("-X github.com/fullsend-ai/fullsend/internal/cli.version=%s", versionLD), | ||
| "-o", opts.DestPath, | ||
| "./cmd/fullsend/", | ||
| ) | ||
| buildCmd.Dir = modRoot | ||
| buildCmd.Env = append(os.Environ(), "GOTOOLCHAIN=auto", "GOOS=linux", "GOARCH="+opts.Arch, "CGO_ENABLED=0") | ||
| buildCmd.Stderr = os.Stderr | ||
| if err := buildCmd.Run(); err != nil { | ||
| return fmt.Errorf("cross-compiling for linux/%s: %w", opts.Arch, err) | ||
| } | ||
| return nil | ||
| } |
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.