Skip to content

Commit

Permalink
feat: add check brewn upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsm-dev committed Sep 26, 2022
1 parent ee2daf4 commit 13eac32
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
36 changes: 32 additions & 4 deletions commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
},
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 13eac32

Please sign in to comment.