|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "regexp" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/basecamp/fizzy-cli/internal/config" |
| 18 | + version "github.com/hashicorp/go-version" |
| 19 | + "github.com/mattn/go-isatty" |
| 20 | + "gopkg.in/yaml.v3" |
| 21 | +) |
| 22 | + |
| 23 | +const fizzyUpdateRepo = "basecamp/fizzy-cli" |
| 24 | + |
| 25 | +var gitDescribeSuffixRE = regexp.MustCompile(`\d+-\d+-g[a-f0-9]{8}$`) |
| 26 | + |
| 27 | +var ( |
| 28 | + updateHTTPClient = &http.Client{Timeout: 5 * time.Second} |
| 29 | + updateCancel context.CancelFunc |
| 30 | + updateMessage chan *releaseInfo |
| 31 | +) |
| 32 | + |
| 33 | +type releaseInfo struct { |
| 34 | + Version string `json:"tag_name" yaml:"version"` |
| 35 | + URL string `json:"html_url" yaml:"url"` |
| 36 | + PublishedAt time.Time `json:"published_at" yaml:"published_at"` |
| 37 | +} |
| 38 | + |
| 39 | +type updateStateEntry struct { |
| 40 | + CheckedForUpdateAt time.Time `yaml:"checked_for_update_at"` |
| 41 | + LatestRelease releaseInfo `yaml:"latest_release"` |
| 42 | +} |
| 43 | + |
| 44 | +func startUpdateCheck() { |
| 45 | + if updateCancel != nil { |
| 46 | + updateCancel() |
| 47 | + updateCancel = nil |
| 48 | + updateMessage = nil |
| 49 | + } |
| 50 | + |
| 51 | + current := currentVersion() |
| 52 | + if !isUpdateableVersion(current) || !shouldCheckForUpdate() { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + stateDir, err := config.StateDir() |
| 57 | + if err != nil { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + ctx, cancel := context.WithCancel(context.Background()) |
| 62 | + updateCancel = cancel |
| 63 | + updateMessage = make(chan *releaseInfo, 1) |
| 64 | + go func() { |
| 65 | + rel, err := checkForUpdate(ctx, updateHTTPClient, filepath.Join(stateDir, "state.yml"), fizzyUpdateRepo, current) |
| 66 | + if err != nil && cfgVerbose { |
| 67 | + fmt.Fprintf(os.Stderr, "warning: checking for update failed: %v\n", err) |
| 68 | + } |
| 69 | + updateMessage <- rel |
| 70 | + }() |
| 71 | +} |
| 72 | + |
| 73 | +func finishUpdateCheck() { |
| 74 | + if updateCancel == nil || updateMessage == nil { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + updateCancel() |
| 79 | + rel := <-updateMessage |
| 80 | + updateCancel = nil |
| 81 | + updateMessage = nil |
| 82 | + if rel == nil { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + exe, _ := os.Executable() |
| 87 | + isHomebrew := isUnderHomebrew(exe) |
| 88 | + if isHomebrew && isRecentRelease(rel.PublishedAt) { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Fprintf(os.Stderr, "\n\nA new release of fizzy is available: %s → %s\n", |
| 93 | + strings.TrimPrefix(currentVersion(), "v"), |
| 94 | + strings.TrimPrefix(rel.Version, "v")) |
| 95 | + if isHomebrew { |
| 96 | + fmt.Fprintln(os.Stderr, "To upgrade, run: brew upgrade basecamp/tap/fizzy") |
| 97 | + } else { |
| 98 | + fmt.Fprintln(os.Stderr, "Upgrade with your package manager, or download it from:") |
| 99 | + } |
| 100 | + fmt.Fprintf(os.Stderr, "%s\n\n", rel.URL) |
| 101 | +} |
| 102 | + |
| 103 | +func shouldCheckForUpdate() bool { |
| 104 | + if os.Getenv("FIZZY_NO_UPDATE_NOTIFIER") != "" { |
| 105 | + return false |
| 106 | + } |
| 107 | + if os.Getenv("CODESPACES") != "" { |
| 108 | + return false |
| 109 | + } |
| 110 | + if isCI() { |
| 111 | + return false |
| 112 | + } |
| 113 | + if cfgAgent || cfgJSON || cfgQuiet || cfgIDsOnly || cfgCount || cfgJQ != "" { |
| 114 | + return false |
| 115 | + } |
| 116 | + return isTerminal(os.Stdout) && isTerminal(os.Stderr) |
| 117 | +} |
| 118 | + |
| 119 | +func checkForUpdate(ctx context.Context, client *http.Client, stateFilePath, repo, currentVersion string) (*releaseInfo, error) { |
| 120 | + stateEntry, _ := getUpdateStateEntry(stateFilePath) |
| 121 | + if stateEntry != nil && time.Since(stateEntry.CheckedForUpdateAt).Hours() < 24 { |
| 122 | + return nil, nil |
| 123 | + } |
| 124 | + |
| 125 | + rel, err := getLatestReleaseInfo(ctx, client, repo) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + if err := setUpdateStateEntry(stateFilePath, time.Now(), *rel); err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + if versionGreaterThan(rel.Version, currentVersion) { |
| 135 | + return rel, nil |
| 136 | + } |
| 137 | + return nil, nil |
| 138 | +} |
| 139 | + |
| 140 | +func getLatestReleaseInfo(ctx context.Context, client *http.Client, repo string) (*releaseInfo, error) { |
| 141 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo), nil) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + req.Header.Set("Accept", "application/vnd.github+json") |
| 146 | + |
| 147 | + res, err := client.Do(req) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + defer func() { |
| 152 | + _, _ = io.Copy(io.Discard, res.Body) |
| 153 | + res.Body.Close() |
| 154 | + }() |
| 155 | + if res.StatusCode != http.StatusOK { |
| 156 | + return nil, fmt.Errorf("unexpected HTTP %d", res.StatusCode) |
| 157 | + } |
| 158 | + |
| 159 | + var rel releaseInfo |
| 160 | + if err := json.NewDecoder(io.LimitReader(res.Body, 1<<20)).Decode(&rel); err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + return &rel, nil |
| 164 | +} |
| 165 | + |
| 166 | +func getUpdateStateEntry(stateFilePath string) (*updateStateEntry, error) { |
| 167 | + content, err := os.ReadFile(stateFilePath) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + |
| 172 | + var stateEntry updateStateEntry |
| 173 | + if err := yaml.Unmarshal(content, &stateEntry); err != nil { |
| 174 | + return nil, err |
| 175 | + } |
| 176 | + return &stateEntry, nil |
| 177 | +} |
| 178 | + |
| 179 | +func setUpdateStateEntry(stateFilePath string, t time.Time, rel releaseInfo) error { |
| 180 | + content, err := yaml.Marshal(updateStateEntry{CheckedForUpdateAt: t, LatestRelease: rel}) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + if err := os.MkdirAll(filepath.Dir(stateFilePath), 0o755); err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + return os.WriteFile(stateFilePath, content, 0o600) |
| 188 | +} |
| 189 | + |
| 190 | +func versionGreaterThan(v, w string) bool { |
| 191 | + w = gitDescribeSuffixRE.ReplaceAllStringFunc(w, func(m string) string { |
| 192 | + idx := strings.IndexRune(m, '-') |
| 193 | + n, _ := strconv.Atoi(m[:idx]) |
| 194 | + return fmt.Sprintf("%d-pre.0", n+1) |
| 195 | + }) |
| 196 | + |
| 197 | + vv, ve := version.NewVersion(v) |
| 198 | + vw, we := version.NewVersion(w) |
| 199 | + return ve == nil && we == nil && vv.GreaterThan(vw) |
| 200 | +} |
| 201 | + |
| 202 | +func isUpdateableVersion(v string) bool { |
| 203 | + v = strings.TrimSpace(v) |
| 204 | + if v == "" || v == "dev" || strings.Contains(v, "dirty") || strings.Contains(v, "-g") { |
| 205 | + return false |
| 206 | + } |
| 207 | + _, err := version.NewVersion(v) |
| 208 | + return err == nil |
| 209 | +} |
| 210 | + |
| 211 | +func isRecentRelease(publishedAt time.Time) bool { |
| 212 | + return !publishedAt.IsZero() && time.Since(publishedAt) < 24*time.Hour |
| 213 | +} |
| 214 | + |
| 215 | +func isUnderHomebrew(exePath string) bool { |
| 216 | + if exePath == "" { |
| 217 | + return false |
| 218 | + } |
| 219 | + brewExe, err := exec.LookPath("brew") |
| 220 | + if err != nil { |
| 221 | + return false |
| 222 | + } |
| 223 | + prefix, err := exec.Command(brewExe, "--prefix").Output() |
| 224 | + if err != nil { |
| 225 | + return false |
| 226 | + } |
| 227 | + brewBinPrefix := filepath.Join(strings.TrimSpace(string(prefix)), "bin") + string(filepath.Separator) |
| 228 | + return strings.HasPrefix(exePath, brewBinPrefix) |
| 229 | +} |
| 230 | + |
| 231 | +func isCI() bool { |
| 232 | + for _, name := range []string{"CI", "GITHUB_ACTIONS", "BUILDKITE", "CIRCLECI", "GITLAB_CI", "JENKINS_URL", "TEAMCITY_VERSION", "TF_BUILD"} { |
| 233 | + if os.Getenv(name) != "" { |
| 234 | + return true |
| 235 | + } |
| 236 | + } |
| 237 | + return false |
| 238 | +} |
| 239 | + |
| 240 | +func isTerminal(f *os.File) bool { |
| 241 | + return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) |
| 242 | +} |
0 commit comments