Show brew upgrade when installed with Homebrew (#2519)

Show `brew upgrade pulumi` as the upgrade message when the currently
running pulumi executable is on macOS and running from the brew install
directory.
This commit is contained in:
Justin Van Patten 2019-03-06 10:56:44 -08:00 committed by GitHub
parent 62b6f1c3b2
commit 7784c94c88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View file

@ -2,6 +2,9 @@
### Improvements
- Show `brew upgrade pulumi` as the upgrade message when the currently running `pulumi` executable
is running on macOS from the brew install directory.
## 0.17.1 (Released March 6, 2019)
### Improvements

View file

@ -16,6 +16,7 @@ package cmd
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
@ -324,6 +325,14 @@ func getUpgradeCommand() string {
return ""
}
isBrew, err := isBrewInstall(exe)
if err != nil {
glog.V(3).Infof("error determining if the running executable was installed with brew: %s", err)
}
if isBrew {
return "$ brew upgrade pulumi"
}
if filepath.Dir(exe) != filepath.Join(curUser.HomeDir, ".pulumi", "bin") {
return ""
}
@ -342,6 +351,47 @@ func getUpgradeCommand() string {
`((New-Object System.Net.WebClient).DownloadString('https://get.pulumi.com/install.ps1'))"`
}
// isBrewInstall returns true if the current running executable is running on macOS and was installed with brew.
func isBrewInstall(exe string) (bool, error) {
if runtime.GOOS != "darwin" {
return false, nil
}
exePath, err := filepath.EvalSymlinks(exe)
if err != nil {
return false, err
}
brewBin, err := exec.LookPath("brew")
if err != nil {
return false, err
}
brewPrefixCmd := exec.Command(brewBin, "--prefix", "pulumi")
var stdout bytes.Buffer
var stderr bytes.Buffer
brewPrefixCmd.Stdout = &stdout
if err = brewPrefixCmd.Run(); err != nil {
if ee, ok := err.(*exec.ExitError); ok {
ee.Stderr = stderr.Bytes()
}
return false, errors.Wrapf(err, "'brew --prefix pulumi' failed")
}
brewPrefixCmdOutput := strings.TrimSpace(stdout.String())
if brewPrefixCmdOutput == "" {
return false, errors.New("trimmed output from 'brew --prefix pulumi' is empty")
}
brewPrefixPath, err := filepath.EvalSymlinks(brewPrefixCmdOutput)
if err != nil {
return false, err
}
brewPrefixExePath := filepath.Join(brewPrefixPath, "bin", "pulumi")
return exePath == brewPrefixExePath, nil
}
func isDevVersion(s semver.Version) bool {
if len(s.Pre) == 0 {
return false