|
8 | 8 | "os" |
9 | 9 | "os/exec" |
10 | 10 | "path" |
| 11 | + "path/filepath" |
11 | 12 | "strings" |
| 13 | + "time" |
12 | 14 |
|
13 | 15 | surveyCore "github.com/AlecAivazis/survey/v2/core" |
14 | 16 | "github.com/cli/cli/api" |
@@ -161,13 +163,16 @@ func main() { |
161 | 163 |
|
162 | 164 | newRelease := <-updateMessageChan |
163 | 165 | if newRelease != nil { |
164 | | - msg := fmt.Sprintf("%s %s → %s\n%s", |
| 166 | + ghExe, _ := os.Executable() |
| 167 | + fmt.Fprintf(stderr, "\n\n%s %s → %s\n", |
165 | 168 | ansi.Color("A new release of gh is available:", "yellow"), |
166 | 169 | ansi.Color(buildVersion, "cyan"), |
167 | | - ansi.Color(newRelease.Version, "cyan"), |
| 170 | + ansi.Color(newRelease.Version, "cyan")) |
| 171 | + if suggestBrewUpgrade(newRelease, ghExe) { |
| 172 | + fmt.Fprintf(stderr, "To upgrade, run: %s\n", "brew update && brew upgrade gh") |
| 173 | + } |
| 174 | + fmt.Fprintf(stderr, "%s\n\n", |
168 | 175 | ansi.Color(newRelease.URL, "yellow")) |
169 | | - |
170 | | - fmt.Fprintf(stderr, "\n\n%s\n\n", msg) |
171 | 176 | } |
172 | 177 | } |
173 | 178 |
|
@@ -259,3 +264,24 @@ func apiVerboseLog() api.ClientOption { |
259 | 264 | colorize := utils.IsTerminal(os.Stderr) |
260 | 265 | return api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize) |
261 | 266 | } |
| 267 | + |
| 268 | +// Suggest to `brew upgrade gh` only if gh was found under homebrew prefix and when the release was |
| 269 | +// published over 24h ago, allowing homebrew-core ample time to merge the formula bump. |
| 270 | +func suggestBrewUpgrade(rel *update.ReleaseInfo, ghBinary string) bool { |
| 271 | + if rel.PublishedAt.IsZero() || time.Since(rel.PublishedAt) < time.Duration(time.Hour*24) { |
| 272 | + return false |
| 273 | + } |
| 274 | + |
| 275 | + brewExe, err := safeexec.LookPath("brew") |
| 276 | + if err != nil { |
| 277 | + return false |
| 278 | + } |
| 279 | + |
| 280 | + brewPrefixBytes, err := exec.Command(brewExe, "--prefix").Output() |
| 281 | + if err != nil { |
| 282 | + return false |
| 283 | + } |
| 284 | + |
| 285 | + brewBinPrefix := filepath.Join(strings.TrimSpace(string(brewPrefixBytes)), "bin") + string(filepath.Separator) |
| 286 | + return strings.HasPrefix(ghBinary, brewBinPrefix) |
| 287 | +} |
0 commit comments