Skip to content

Commit

Permalink
Merge pull request #16 from marcus-crane/fix-update-versioning
Browse files Browse the repository at this point in the history
Fix autoupdater + add an explicit flag to disable updates
  • Loading branch information
marcus-crane authored Feb 7, 2022
2 parents 8edcf1e + 28e4430 commit 5ad15b7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
12 changes: 11 additions & 1 deletion cmd/khinsider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ func Execute(buildInfo BuildInfo) {
},
Usage: "easily fetch videogame soundtracks from downloads.khinsider.com",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
},
&cli.BoolFlag{
Name: "no-updates",
Aliases: []string{"n"},
Value: false,
Usage: "Disable checks for updates to khinsider (env: KHINSIDER_NO_UPDATE)",
EnvVars: []string{"CI", "KHINSIDER_NO_UPDATE"},
},
},
Before: func(c *cli.Context) error {
if c.Bool("debug") {
Expand Down
18 changes: 13 additions & 5 deletions cmd/khinsider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"github.com/marcus-crane/khinsider/v2/internal/updater"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"os"
"strings"

"github.com/marcus-crane/khinsider/v2/pkg/update"
)

func isUpdaterDisabled() bool {
return os.Getenv("CI") == "true" || os.Getenv("KHINSIDER_NO_UPDATE") == "true"
func isUpdaterDisabled(c *cli.Context) bool {
return c.Bool("no-updates")
}

func CheckForUpdates(c *cli.Context, currentVersion string, prerelease bool) (bool, string) {
if isUpdaterDisabled() && c.Command.Name != "update" {
if isUpdaterDisabled(c) && c.Command.Name != "update" || strings.Contains(currentVersion, "-DEV") {
pterm.Debug.Println("Updater is disabled. Skipping update check.")
return false, ""
}
Expand All @@ -41,8 +41,16 @@ func CheckForUpdates(c *cli.Context, currentVersion string, prerelease bool) (bo
func UpdateAction(c *cli.Context, currentVersion string, prerelease bool) error {
releaseAvailable, remoteVersion := CheckForUpdates(c, currentVersion, prerelease)
pterm.Debug.Printfln("Release is available: %t. Remote version is %s", releaseAvailable, remoteVersion)
if !releaseAvailable && !isUpdaterDisabled() {
if strings.Contains(currentVersion, "-DEV") {
pterm.Error.Println("You can't run updates when running a dev build")
return nil
}
if !releaseAvailable && !isUpdaterDisabled(c) {
pterm.Info.Printfln("Sorry, no updates are available. The latest version is %s and you're on %s", remoteVersion, currentVersion)
return nil
}
if isUpdaterDisabled(c) {
return nil
}
return updater.UpgradeInPlace(c.App.Writer, c.App.ErrWriter, remoteVersion)
}
21 changes: 4 additions & 17 deletions internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,27 @@ package updater

import (
"fmt"
"github.com/cli/safeexec"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/cli/safeexec"
)

func isUnderHomebrew() bool {
flyBinary, err := os.Executable()
if err != nil {
return false
}

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(flyBinary, brewBinPrefix)
_, err = exec.Command(brewExe, "list", "khinsider").Output()
return err == nil
}

func updateCommand(version string) string {
switch {
case isUnderHomebrew():
return "brew upgrade khinsider"
return "brew update && brew install marcus-crane/tap/khinsider"
case runtime.GOOS == "windows":
cmd := "iwr https://utf9k.net/khinsider/install.ps1 -useb | iex"
if version != "" {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

var (
version = "dev"
version = "v2.0.4"
commit = "n/a"
date = "n/a"
builtBy = "dev"
builtBy = "source"
)

func main() {
Expand Down

0 comments on commit 5ad15b7

Please sign in to comment.