diff --git a/commands/upgrade.go b/commands/upgrade.go index e8efb18..a43a82c 100644 --- a/commands/upgrade.go +++ b/commands/upgrade.go @@ -2,9 +2,14 @@ package commands import ( "fmt" + "os/exec" + "path/filepath" + "strings" "github.com/ci-monk/loli/internal/cli" "github.com/ci-monk/loli/internal/version" + "github.com/cli/safeexec" + "github.com/kardianos/osext" "github.com/spf13/cobra" ) @@ -22,10 +27,18 @@ The next time you upgrade, the hidden file will be overwritten. You can always delete this file. `, RunE: func(cmd *cobra.Command, args []string) error { - c := cli.New(version.GetVersionFormatted()) - err := updateCLI(c) - if err != nil { - return fmt.Errorf("we were not able to upgrade the cli because we encountered an error: %s", err) + loliBinary, _ := osext.Executable() + isHomebrew := isUnderHomebrew(loliBinary) + + if isHomebrew { + fmt.Printf("To upgrade, run: %s\n", "brew upgrade gh") + return nil + } else { + c := cli.New(version.GetVersionFormatted()) + err := updateCLI(c) + if err != nil { + return fmt.Errorf("we were not able to upgrade the cli because we encountered an error: %s", err) + } } return nil }, @@ -45,6 +58,21 @@ func updateCLI(c cli.Updater) error { return c.Upgrade() } +func isUnderHomebrew(binary string) bool { + brewExe, err := safeexec.LookPath("brew") + if err != nil { + return false + } + + brewPrefixBytes, err := exec.Command(brewExe, "--prefix").Output() + if err != nil { + return false + } + + brewBinPrefix := filepath.Join(strings.TrimSpace(string(brewPrefixBytes)), "bin") + string(filepath.Separator) + return strings.HasPrefix(binary, brewBinPrefix) +} + func init() { rootCmd.AddCommand(upgradeCmd) } diff --git a/go.mod b/go.mod index 650258c..213c7df 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,11 @@ require ( github.com/blang/semver v3.5.1+incompatible github.com/briandowns/spinner v1.19.0 github.com/charmbracelet/glamour v0.5.0 + github.com/cli/safeexec v1.0.0 github.com/fatih/color v1.13.0 github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf github.com/jedib0t/go-pretty v4.3.0+incompatible + github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 github.com/kyokomi/emoji/v2 v2.2.10 github.com/logrusorgru/aurora v2.0.3+incompatible github.com/logrusorgru/aurora/v3 v3.0.0 diff --git a/go.sum b/go.sum index 92e15c5..e235883 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU= github.com/charmbracelet/glamour v0.5.0 h1:wu15ykPdB7X6chxugG/NNfDUbyyrCLV9XBalj5wdu3g= github.com/charmbracelet/glamour v0.5.0/go.mod h1:9ZRtG19AUIzcTm7FGLGbq3D5WKQ5UyZBbQsMQN0XIqc= +github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI= +github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -62,6 +64,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo= github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag= +github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= +github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=