From 0f0172f41ba8ae6ba7b99b2d54b8f8d942d17670 Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 15:29:44 +1300 Subject: [PATCH 01/11] Embed version for go install users --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index d6c8807..a3bc074 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( ) var ( - version = "dev" + version = "v2.0.4-DEV" commit = "n/a" date = "n/a" builtBy = "dev" From 5cedeadda2ba6a9f4b9c98f307a5eefcac95162c Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 15:29:56 +1300 Subject: [PATCH 02/11] Tidy up old fly references and update brew update command --- internal/updater/updater.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/updater/updater.go b/internal/updater/updater.go index eb7b22f..6c899ea 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -16,7 +16,7 @@ import ( ) func isUnderHomebrew() bool { - flyBinary, err := os.Executable() + khinsiderBinary, err := os.Executable() if err != nil { return false } @@ -32,13 +32,13 @@ func isUnderHomebrew() bool { } brewBinPrefix := filepath.Join(strings.TrimSpace(string(brewPrefixBytes)), "bin") + string(filepath.Separator) - return strings.HasPrefix(flyBinary, brewBinPrefix) + return strings.HasPrefix(khinsiderBinary, brewBinPrefix) } 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 != "" { From 36cb34d6e8dc83dd2b8029fb0dd0dc3e56328b5e Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 15:39:24 +1300 Subject: [PATCH 03/11] Stop updater from continuing if an update is not available --- cmd/khinsider/update.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/khinsider/update.go b/cmd/khinsider/update.go index 95ca616..c63fffd 100644 --- a/cmd/khinsider/update.go +++ b/cmd/khinsider/update.go @@ -43,6 +43,7 @@ func UpdateAction(c *cli.Context, currentVersion string, prerelease bool) error pterm.Debug.Printfln("Release is available: %t. Remote version is %s", releaseAvailable, remoteVersion) if !releaseAvailable && !isUpdaterDisabled() { pterm.Info.Printfln("Sorry, no updates are available. The latest version is %s and you're on %s", remoteVersion, currentVersion) + return nil } return updater.UpgradeInPlace(c.App.Writer, c.App.ErrWriter, remoteVersion) } From 4b2c61ef9966c09f441b049825c3da89994dfd87 Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 15:41:20 +1300 Subject: [PATCH 04/11] Disable updater when using a dev build --- cmd/khinsider/update.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/khinsider/update.go b/cmd/khinsider/update.go index c63fffd..0f926aa 100644 --- a/cmd/khinsider/update.go +++ b/cmd/khinsider/update.go @@ -5,6 +5,7 @@ import ( "github.com/pterm/pterm" "github.com/urfave/cli/v2" "os" + "strings" "github.com/marcus-crane/khinsider/v2/pkg/update" ) @@ -14,7 +15,7 @@ func isUpdaterDisabled() bool { } func CheckForUpdates(c *cli.Context, currentVersion string, prerelease bool) (bool, string) { - if isUpdaterDisabled() && c.Command.Name != "update" { + if isUpdaterDisabled() && c.Command.Name != "update" || strings.Contains(currentVersion, "-DEV") { pterm.Debug.Println("Updater is disabled. Skipping update check.") return false, "" } From efa6782af102f7ae14bc8317da5f0ad939addc1c Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 15:52:16 +1300 Subject: [PATCH 05/11] Add flag for disabling updates --- cmd/khinsider/main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/khinsider/main.go b/cmd/khinsider/main.go index 6bae3fe..ea7f396 100644 --- a/cmd/khinsider/main.go +++ b/cmd/khinsider/main.go @@ -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") { From 6c7c784d12dddf4331769be6a78c5c443519ca12 Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 15:53:37 +1300 Subject: [PATCH 06/11] Power update disable off flag directly --- cmd/khinsider/update.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/khinsider/update.go b/cmd/khinsider/update.go index 0f926aa..aab9088 100644 --- a/cmd/khinsider/update.go +++ b/cmd/khinsider/update.go @@ -4,18 +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" || strings.Contains(currentVersion, "-DEV") { + if isUpdaterDisabled(c) && c.Command.Name != "update" || strings.Contains(currentVersion, "-DEV") { pterm.Debug.Println("Updater is disabled. Skipping update check.") return false, "" } @@ -42,7 +41,7 @@ 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 !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 } From f74a9875e65ab5fc2d401a8d4da568f21abb8408 Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 16:03:23 +1300 Subject: [PATCH 07/11] Skip updates when in dev mode or disabled updates just in case --- cmd/khinsider/update.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/khinsider/update.go b/cmd/khinsider/update.go index aab9088..f1b20ae 100644 --- a/cmd/khinsider/update.go +++ b/cmd/khinsider/update.go @@ -41,9 +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 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) } From c265892e34c384d22ee848bb521cbe8e08da13ac Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 16:25:56 +1300 Subject: [PATCH 08/11] Fix updater to work for both Homebrew and regular users --- internal/updater/updater.go | 16 +++------------- main.go | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 6c899ea..600982c 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -5,34 +5,24 @@ package updater import ( "fmt" + "github.com/cli/safeexec" "io" "os" "os/exec" - "path/filepath" "runtime" - "strings" - - "github.com/cli/safeexec" ) func isUnderHomebrew() bool { - khinsiderBinary, 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() + _, err = exec.Command(brewExe, "list", "khinsider").Output() if err != nil { return false } - - brewBinPrefix := filepath.Join(strings.TrimSpace(string(brewPrefixBytes)), "bin") + string(filepath.Separator) - return strings.HasPrefix(khinsiderBinary, brewBinPrefix) + return true } func updateCommand(version string) string { diff --git a/main.go b/main.go index a3bc074..a641aa2 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,22 @@ package main import ( - "github.com/marcus-crane/khinsider/v2/cmd/khinsider" + "github.com/marcus-crane/khinsider/v2/cmd/khinsider" ) var ( - version = "v2.0.4-DEV" - commit = "n/a" - date = "n/a" - builtBy = "dev" + version = "v2.0.4" + commit = "n/a" + date = "n/a" + builtBy = "dev" ) func main() { - buildInfo := khinsider.BuildInfo{ - Version: version, - Commit: commit, - Date: date, - BuiltBy: builtBy, - } - khinsider.Execute(buildInfo) + buildInfo := khinsider.BuildInfo{ + Version: version, + Commit: commit, + Date: date, + BuiltBy: builtBy, + } + khinsider.Execute(buildInfo) } From 29fea1693c7ef0b89f024f08c1c3f61bcfa4de5d Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 16:28:23 +1300 Subject: [PATCH 09/11] Resolve lint suggestion --- internal/updater/updater.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 600982c..a2e0ae2 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -19,10 +19,7 @@ func isUnderHomebrew() bool { } _, err = exec.Command(brewExe, "list", "khinsider").Output() - if err != nil { - return false - } - return true + return err == nil } func updateCommand(version string) string { From 59ed601b3fe945c4e9c16f0e4aa974ca24dec27e Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 16:29:02 +1300 Subject: [PATCH 10/11] Change builtBy to be go install to differentiate non-binary users --- main.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index a641aa2..226d8da 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,22 @@ package main import ( - "github.com/marcus-crane/khinsider/v2/cmd/khinsider" + "github.com/marcus-crane/khinsider/v2/cmd/khinsider" ) var ( - version = "v2.0.4" - commit = "n/a" - date = "n/a" - builtBy = "dev" + version = "v2.0.4" + commit = "n/a" + date = "n/a" + builtBy = "go install" ) func main() { - buildInfo := khinsider.BuildInfo{ - Version: version, - Commit: commit, - Date: date, - BuiltBy: builtBy, - } - khinsider.Execute(buildInfo) + buildInfo := khinsider.BuildInfo{ + Version: version, + Commit: commit, + Date: date, + BuiltBy: builtBy, + } + khinsider.Execute(buildInfo) } From 28e4430525e413ce9dd5c38e9628179a8dd92441 Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Mon, 7 Feb 2022 16:29:42 +1300 Subject: [PATCH 11/11] Pick a better name to support users who download zip or build from scratch --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 226d8da..d0aecef 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ var ( version = "v2.0.4" commit = "n/a" date = "n/a" - builtBy = "go install" + builtBy = "source" ) func main() {