|
| 1 | +package update |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "archive/zip" |
| 6 | + "bytes" |
| 7 | + "compress/gzip" |
| 8 | + "crypto/sha256" |
| 9 | + "encoding/hex" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "runtime" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/minio/selfupdate" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + releaseDownloadURL = "https://github.com/gleanwork/glean-cli/releases/download" |
| 24 | + checksumFile = "checksums.txt" |
| 25 | + binaryName = "glean" |
| 26 | +) |
| 27 | + |
| 28 | +// Upgrade checks for a newer release and installs it. |
| 29 | +// If the binary was installed via Homebrew it delegates to `brew upgrade`. |
| 30 | +// Otherwise it downloads the appropriate archive from GitHub Releases, |
| 31 | +// verifies its SHA-256 checksum, and atomically replaces the running binary. |
| 32 | +func Upgrade(currentVersion string) error { |
| 33 | + if currentVersion == devVersion || currentVersion == "" { |
| 34 | + return fmt.Errorf("cannot update a dev build — build from source instead") |
| 35 | + } |
| 36 | + |
| 37 | + // Homebrew-managed install: let brew handle the upgrade. |
| 38 | + if isBrewInstall() { |
| 39 | + return brewUpgrade() |
| 40 | + } |
| 41 | + |
| 42 | + fmt.Fprintln(os.Stderr, "Checking for updates...") |
| 43 | + |
| 44 | + latest, err := fetchLatestTag() |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("could not fetch latest release: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + if !isNewer(latest, currentVersion) { |
| 50 | + fmt.Printf("Already up to date (%s)\n", currentVersion) |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Fprintf(os.Stderr, "Updating %s → %s\n", currentVersion, latest) |
| 55 | + |
| 56 | + assetName := assetFilename() |
| 57 | + assetURL := fmt.Sprintf("%s/%s/%s", releaseDownloadURL, latest, assetName) |
| 58 | + checksumURL := fmt.Sprintf("%s/%s/%s", releaseDownloadURL, latest, checksumFile) |
| 59 | + |
| 60 | + archive, err := download(assetURL) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("download failed: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + if err := verifyChecksum(archive, assetName, checksumURL); err != nil { |
| 66 | + return fmt.Errorf("checksum verification failed: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + binary, err := extractBinary(assetName, archive) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("could not extract binary: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + if err := selfupdate.Apply(bytes.NewReader(binary), selfupdate.Options{}); err != nil { |
| 75 | + return fmt.Errorf("could not apply update: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + fmt.Printf("Updated to %s\n", latest) |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// isBrewInstall reports whether the running binary lives inside a Homebrew Cellar. |
| 83 | +func isBrewInstall() bool { |
| 84 | + exe, err := os.Executable() |
| 85 | + if err != nil { |
| 86 | + return false |
| 87 | + } |
| 88 | + return strings.Contains(exe, "/Cellar/") || strings.Contains(exe, "/homebrew/") |
| 89 | +} |
| 90 | + |
| 91 | +// brewUpgrade runs `brew upgrade gleanwork/tap/glean-cli`. |
| 92 | +func brewUpgrade() error { |
| 93 | + fmt.Fprintln(os.Stderr, "Detected Homebrew install — running brew upgrade...") |
| 94 | + cmd := exec.Command("brew", "upgrade", "gleanwork/tap/glean-cli") |
| 95 | + cmd.Stdout = os.Stdout |
| 96 | + cmd.Stderr = os.Stderr |
| 97 | + return cmd.Run() |
| 98 | +} |
| 99 | + |
| 100 | +// assetFilename returns the expected archive name for the current platform. |
| 101 | +// Matches the GoReleaser name_template in .goreleaser.yaml: |
| 102 | +// glean-cli_{OS}_{arch}.tar.gz (e.g. glean-cli_Darwin_arm64.tar.gz) |
| 103 | +func assetFilename() string { |
| 104 | + goos := runtime.GOOS |
| 105 | + goarch := runtime.GOARCH |
| 106 | + |
| 107 | + // GoReleaser uses the `title` filter: "darwin" → "Darwin" |
| 108 | + osName := strings.ToUpper(goos[:1]) + goos[1:] |
| 109 | + archName := goarch |
| 110 | + if goarch == "amd64" { |
| 111 | + archName = "x86_64" |
| 112 | + } |
| 113 | + |
| 114 | + ext := "tar.gz" |
| 115 | + if goos == "windows" { |
| 116 | + ext = "zip" |
| 117 | + } |
| 118 | + |
| 119 | + return fmt.Sprintf("glean-cli_%s_%s.%s", osName, archName, ext) |
| 120 | +} |
| 121 | + |
| 122 | +// download fetches a URL and returns the body bytes. |
| 123 | +func download(url string) ([]byte, error) { |
| 124 | + client := &http.Client{Timeout: 120 * time.Second} |
| 125 | + resp, err := client.Get(url) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + defer resp.Body.Close() |
| 130 | + if resp.StatusCode != http.StatusOK { |
| 131 | + return nil, fmt.Errorf("HTTP %d from %s", resp.StatusCode, url) |
| 132 | + } |
| 133 | + return io.ReadAll(resp.Body) |
| 134 | +} |
| 135 | + |
| 136 | +// verifyChecksum fetches checksums.txt and confirms the archive matches. |
| 137 | +func verifyChecksum(archive []byte, assetName, checksumURL string) error { |
| 138 | + raw, err := download(checksumURL) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("could not fetch checksums: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + want := "" |
| 144 | + for line := range strings.SplitSeq(string(raw), "\n") { |
| 145 | + fields := strings.Fields(line) |
| 146 | + if len(fields) == 2 && fields[1] == assetName { |
| 147 | + want = fields[0] |
| 148 | + break |
| 149 | + } |
| 150 | + } |
| 151 | + if want == "" { |
| 152 | + return fmt.Errorf("no checksum entry found for %s", assetName) |
| 153 | + } |
| 154 | + |
| 155 | + sum := sha256.Sum256(archive) |
| 156 | + got := hex.EncodeToString(sum[:]) |
| 157 | + if got != want { |
| 158 | + return fmt.Errorf("checksum mismatch: got %s want %s", got, want) |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +// extractBinary pulls the `glean` (or `glean.exe`) binary out of the archive. |
| 164 | +func extractBinary(assetName string, data []byte) ([]byte, error) { |
| 165 | + if strings.HasSuffix(assetName, ".zip") { |
| 166 | + return extractFromZip(data) |
| 167 | + } |
| 168 | + return extractFromTarGz(data) |
| 169 | +} |
| 170 | + |
| 171 | +func extractFromTarGz(data []byte) ([]byte, error) { |
| 172 | + gz, err := gzip.NewReader(bytes.NewReader(data)) |
| 173 | + if err != nil { |
| 174 | + return nil, err |
| 175 | + } |
| 176 | + defer gz.Close() |
| 177 | + |
| 178 | + tr := tar.NewReader(gz) |
| 179 | + for { |
| 180 | + hdr, err := tr.Next() |
| 181 | + if err == io.EOF { |
| 182 | + break |
| 183 | + } |
| 184 | + if err != nil { |
| 185 | + return nil, err |
| 186 | + } |
| 187 | + if hdr.Name == binaryName || strings.HasSuffix(hdr.Name, "/"+binaryName) { |
| 188 | + return io.ReadAll(tr) |
| 189 | + } |
| 190 | + } |
| 191 | + return nil, fmt.Errorf("glean binary not found in archive") |
| 192 | +} |
| 193 | + |
| 194 | +func extractFromZip(data []byte) ([]byte, error) { |
| 195 | + r, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) |
| 196 | + if err != nil { |
| 197 | + return nil, err |
| 198 | + } |
| 199 | + for _, f := range r.File { |
| 200 | + if f.Name == binaryName+".exe" || strings.HasSuffix(f.Name, "/"+binaryName+".exe") || |
| 201 | + f.Name == binaryName || strings.HasSuffix(f.Name, "/"+binaryName) { |
| 202 | + rc, err := f.Open() |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + defer rc.Close() |
| 207 | + return io.ReadAll(rc) |
| 208 | + } |
| 209 | + } |
| 210 | + return nil, fmt.Errorf("glean binary not found in archive") |
| 211 | +} |
0 commit comments