|
| 1 | +package binary |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | +) |
| 8 | + |
| 9 | +// Source identifies how a Linux fullsend binary was obtained. |
| 10 | +type Source int |
| 11 | + |
| 12 | +const ( |
| 13 | + SourceExplicitPath Source = iota |
| 14 | + SourceCheckoutBuild |
| 15 | + SourceReleaseDownload |
| 16 | +) |
| 17 | + |
| 18 | +// AcquireResult holds the path to an acquired binary and metadata for callers. |
| 19 | +type AcquireResult struct { |
| 20 | + TmpDir string // caller must RemoveAll when non-empty |
| 21 | + Path string |
| 22 | + Source Source |
| 23 | +} |
| 24 | + |
| 25 | +// ResolveExplicit validates that path is a Linux ELF for arch. |
| 26 | +func ResolveExplicit(path, arch string) error { |
| 27 | + return ValidateLinuxBinary(path, arch) |
| 28 | +} |
| 29 | + |
| 30 | +// ResolveForRun obtains a Linux binary using the run policy: |
| 31 | +// release download (if released) → cross-compile → latest release. |
| 32 | +func ResolveForRun(version, arch string) (AcquireResult, error) { |
| 33 | + tmpDir, err := os.MkdirTemp("", "fullsend-linux-*") |
| 34 | + if err != nil { |
| 35 | + return AcquireResult{}, fmt.Errorf("creating temp dir: %w", err) |
| 36 | + } |
| 37 | + binaryPath := filepath.Join(tmpDir, "fullsend") |
| 38 | + |
| 39 | + // 1. Released version → download matching release asset. |
| 40 | + if IsReleasedVersion(version) { |
| 41 | + fmt.Fprintf(os.Stderr, "Downloading fullsend %s for linux/%s from GitHub Release...\n", version, arch) |
| 42 | + if dlErr := DownloadRelease(version, arch, binaryPath); dlErr == nil { |
| 43 | + fmt.Fprintf(os.Stderr, "Downloaded fullsend for linux/%s\n", arch) |
| 44 | + return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceReleaseDownload}, nil |
| 45 | + } else { |
| 46 | + fmt.Fprintf(os.Stderr, "WARNING: release download failed: %v\n", dlErr) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // 2. Try cross-compilation (requires Go toolchain + module checkout). |
| 51 | + fmt.Fprintf(os.Stderr, "Cross-compiling fullsend for linux/%s...\n", arch) |
| 52 | + if ccErr := CrossCompile(CrossCompileOpts{ |
| 53 | + Version: version, |
| 54 | + Arch: arch, |
| 55 | + DestPath: binaryPath, |
| 56 | + VersionStamp: "-crosscompiled", |
| 57 | + }); ccErr == nil { |
| 58 | + fmt.Fprintf(os.Stderr, "Cross-compiled fullsend for linux/%s\n", arch) |
| 59 | + return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceCheckoutBuild}, nil |
| 60 | + } else { |
| 61 | + fmt.Fprintf(os.Stderr, "WARNING: cross-compilation failed: %v\n", ccErr) |
| 62 | + } |
| 63 | + |
| 64 | + // 3. Last resort → download latest release. |
| 65 | + fmt.Fprintf(os.Stderr, "Downloading latest fullsend release for linux/%s...\n", arch) |
| 66 | + latestErr := DownloadLatestRelease(arch, binaryPath) |
| 67 | + if latestErr == nil { |
| 68 | + fmt.Fprintf(os.Stderr, "Downloaded latest fullsend for linux/%s\n", arch) |
| 69 | + return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceReleaseDownload}, nil |
| 70 | + } |
| 71 | + fmt.Fprintf(os.Stderr, "WARNING: latest release download failed: %v\n", latestErr) |
| 72 | + |
| 73 | + os.RemoveAll(tmpDir) |
| 74 | + return AcquireResult{}, fmt.Errorf("all strategies failed for linux/%s: provide --fullsend-binary or install Go toolchain", arch) |
| 75 | +} |
| 76 | + |
| 77 | +// ResolveForVendor obtains a Linux binary using the vendoring policy: |
| 78 | +// cross-compile from checkout → matching release (released CLI only) → fail. |
| 79 | +// No latest-release fallback. |
| 80 | +func ResolveForVendor(version, arch string) (AcquireResult, error) { |
| 81 | + tmpDir, err := os.MkdirTemp("", "fullsend-linux-*") |
| 82 | + if err != nil { |
| 83 | + return AcquireResult{}, fmt.Errorf("creating temp dir: %w", err) |
| 84 | + } |
| 85 | + binaryPath := filepath.Join(tmpDir, "fullsend") |
| 86 | + |
| 87 | + // 1. Cross-compile from checkout. |
| 88 | + fmt.Fprintf(os.Stderr, "Cross-compiling fullsend for linux/%s...\n", arch) |
| 89 | + if ccErr := CrossCompile(CrossCompileOpts{ |
| 90 | + Version: version, |
| 91 | + Arch: arch, |
| 92 | + DestPath: binaryPath, |
| 93 | + VersionStamp: "-vendored", |
| 94 | + }); ccErr == nil { |
| 95 | + fmt.Fprintf(os.Stderr, "Cross-compiled fullsend for linux/%s\n", arch) |
| 96 | + return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceCheckoutBuild}, nil |
| 97 | + } else { |
| 98 | + fmt.Fprintf(os.Stderr, "WARNING: cross-compilation failed: %v\n", ccErr) |
| 99 | + } |
| 100 | + |
| 101 | + // 2. Release fetch only for released CLI versions. |
| 102 | + if IsReleasedVersion(version) { |
| 103 | + fmt.Fprintf(os.Stderr, "Downloading fullsend %s for linux/%s from GitHub Release...\n", version, arch) |
| 104 | + if dlErr := DownloadRelease(version, arch, binaryPath); dlErr == nil { |
| 105 | + fmt.Fprintf(os.Stderr, "Downloaded fullsend for linux/%s\n", arch) |
| 106 | + return AcquireResult{TmpDir: tmpDir, Path: binaryPath, Source: SourceReleaseDownload}, nil |
| 107 | + } else { |
| 108 | + os.RemoveAll(tmpDir) |
| 109 | + return AcquireResult{}, fmt.Errorf("cross-compilation unavailable and release download failed for v%s: %w", version, dlErr) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + os.RemoveAll(tmpDir) |
| 114 | + 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) |
| 115 | +} |
0 commit comments