From 311d5bf569dfb791a249552b019207ead1a3649e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20La=CC=88mmer?= Date: Fri, 20 Nov 2026 15:10:32 +0100 Subject: [PATCH] FR-7467: show the upgrade command in the update notice Co-Authored-By: Claude Opus 5.5 --- install.sh | 2 +- internal/cmd/doctor/doctor.go | 6 ++- internal/cmd/root/root.go | 10 +++- internal/update/install.go | 90 +++++++++++++++++++++++++++++++ internal/update/install_test.go | 48 +++++++++++++++++ internal/update/writable_other.go | 6 +++ internal/update/writable_unix.go | 9 ++++ scripts/test-install.sh | 2 +- 8 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 internal/update/install.go create mode 100644 internal/update/install_test.go create mode 100644 internal/update/writable_other.go create mode 100644 internal/update/writable_unix.go diff --git a/install.sh b/install.sh index c75d6bd..11a2287 100755 --- a/install.sh +++ b/install.sh @@ -137,7 +137,7 @@ main() { if [ "$manager" = "Homebrew" ]; then printf 'Update it with: brew upgrade frbit\n' else - printf 'Update it with: npm update --global @fortrabbit/cli\n' + printf 'Update it with: npm install --global @fortrabbit/cli@latest\n' fi if [ "$setup_agent" -eq 1 ]; then run_setup_agent "$existing" diff --git a/internal/cmd/doctor/doctor.go b/internal/cmd/doctor/doctor.go index 8ef5c8c..8747255 100644 --- a/internal/cmd/doctor/doctor.go +++ b/internal/cmd/doctor/doctor.go @@ -138,7 +138,11 @@ func versionCheck(ctx context.Context, version string, p *Probes) Check { case err != nil: return Check{Name: "Version", Status: StatusWarn, Detail: fmt.Sprintf("%s, latest release unknown: %v", current, err)} case latest != "": - return Check{Name: "Version", Status: StatusWarn, Detail: fmt.Sprintf("%s, %s is out", current, latest), Fix: "update frbit the way you installed it: https://docs.fortrabbit.com/platform/automation/cli/install"} + fix, docsURL := update.Upgrade() + if fix == "" { + fix = docsURL + } + return Check{Name: "Version", Status: StatusWarn, Detail: fmt.Sprintf("%s, %s is out", current, latest), Fix: fix} default: return Check{Name: "Version", Status: StatusOK, Detail: current + ", the latest release"} } diff --git a/internal/cmd/root/root.go b/internal/cmd/root/root.go index e40675a..39d56c2 100644 --- a/internal/cmd/root/root.go +++ b/internal/cmd/root/root.go @@ -32,6 +32,7 @@ import ( "github.com/fortrabbit/frbit-cli/internal/cmdutil" "github.com/fortrabbit/frbit-cli/internal/iostreams" envlink "github.com/fortrabbit/frbit-cli/internal/link" + "github.com/fortrabbit/frbit-cli/internal/update" "github.com/spf13/cobra" ) @@ -138,11 +139,18 @@ func notifyUpdate(command *cobra.Command, factory *app.Factory) { return } + hint := "Upgrade with: " + upgrade, docsURL := update.Upgrade() + if upgrade == "" { + hint, upgrade = "See ", docsURL + } _, _ = fmt.Fprintf( command.ErrOrStderr(), - "\nUpdate available: v%s → %s. See https://docs.fortrabbit.com/platform/concepts/cli\n", + "\nUpdate available: v%s → %s. %s%s\n", strings.TrimPrefix(factory.Version, "v"), latest, + hint, + upgrade, ) } diff --git a/internal/update/install.go b/internal/update/install.go new file mode 100644 index 0000000..c226733 --- /dev/null +++ b/internal/update/install.go @@ -0,0 +1,90 @@ +package update + +import ( + "os" + "path/filepath" + "regexp" + "runtime" + "strings" +) + +// Method is the way frbit was installed, read from where its binary lives. +type Method int + +const ( + MethodUnknown Method = iota + MethodHomebrew + MethodNPM + MethodScript +) + +// installMethod names a method only where it is certain; anything else, such as npx, pnpm, mise or Nix, is unknown. +func installMethod(path, home string, exists func(string) bool) Method { + slashed := filepath.ToSlash(path) + dir := filepath.ToSlash(filepath.Dir(path)) + npmPrefix, _, npmLayout := strings.Cut(slashed, "/lib/node_modules/@fortrabbit/") + switch { + case strings.Contains(slashed, "/Cellar/frbit/"): + return MethodHomebrew + case strings.Contains(slashed, "/_npx/"), strings.Contains(slashed, "/.pnpm/"), strings.Contains(slashed, "/.volta/"): + return MethodUnknown + // A global npm install keeps the package in /lib/node_modules and links it as /bin/frbit; a project one does not. + case npmLayout: + if exists(npmPrefix + "/bin/frbit") { + return MethodNPM + } + return MethodUnknown + case dir == "/usr/local/bin", home != "" && dir == filepath.ToSlash(filepath.Join(home, ".local", "bin")): + return MethodScript + } + return MethodUnknown +} + +// InstallDocsURL documents every install method and how to upgrade it. +const InstallDocsURL = "https://docs.fortrabbit.com/platform/automation/cli/install#updates" + +const installScript = "curl -fsSL https://www.fortrabbit.com/install.sh | " + +// Upgrade returns the shell command that upgrades the running frbit, or the docs link when no single command does. +func Upgrade() (command, docsURL string) { + path, err := os.Executable() + if err != nil { + return "", InstallDocsURL + } + if target, err := filepath.EvalSymlinks(path); err == nil { + path = target + } + home, _ := os.UserHomeDir() + if target, err := filepath.EvalSymlinks(home); err == nil { + home = target + } + dir := filepath.Dir(path) + return upgrade(installMethod(path, home, lexists), runtime.GOOS, dir, writable(dir)) +} + +func upgrade(method Method, goos, dir string, canWrite bool) (command, docsURL string) { + if goos == "windows" { + return "", InstallDocsURL + } + switch method { + case MethodHomebrew: + return "brew upgrade frbit", "" + case MethodNPM: + if canWrite { + return "npm install --global @fortrabbit/cli@latest", "" + } + case MethodScript: + // Name the folder: install.sh's defaults can pick another one or take the binary for a brew or npm one. + if canWrite && shellSafe.MatchString(dir) { + return installScript + "FRBIT_INSTALL_DIR=" + dir + " sh", "" + } + } + return "", InstallDocsURL +} + +var shellSafe = regexp.MustCompile(`^[A-Za-z0-9._/-]+$`) + +func lexists(path string) bool { + _, err := os.Lstat(path) + return err == nil +} diff --git a/internal/update/install_test.go b/internal/update/install_test.go new file mode 100644 index 0000000..b9498a2 --- /dev/null +++ b/internal/update/install_test.go @@ -0,0 +1,48 @@ +package update + +import "testing" + +func TestInstallMethod(t *testing.T) { + for path, want := range map[string]Method{ + "/opt/homebrew/Cellar/frbit/1.21.0/bin/frbit": MethodHomebrew, + "/usr/local/lib/node_modules/@fortrabbit/cli-darwin-arm64/bin/frbit": MethodNPM, + "/usr/local/lib/node_modules/@fortrabbit/cli/node_modules/@fortrabbit/cli-darwin-arm64/bin/frbit": MethodNPM, + "/work/app/lib/node_modules/@fortrabbit/cli-linux-x64/bin/frbit": MethodUnknown, + "/home/me/.volta/tools/image/packages/@fortrabbit/cli/lib/node_modules/@fortrabbit/cli/bin/frbit": MethodUnknown, + "/home/me/.local/bin/frbit": MethodScript, + "/usr/local/bin/frbit": MethodScript, + "/home/me/.npm/_npx/abc123/lib/node_modules/@fortrabbit/cli-linux-x64/bin/frbit": MethodUnknown, + "/home/me/project/node_modules/@fortrabbit/cli-linux-x64/bin/frbit": MethodUnknown, + "/home/me/.local/share/pnpm/global/5/.pnpm/x/node_modules/@fortrabbit/cli-linux/bin/frbit": MethodUnknown, + "/home/me/.local/share/mise/installs/frbit/1.21.0/frbit": MethodUnknown, + "/nix/store/abc-frbit-1.21.0/bin/frbit": MethodUnknown, + } { + if got := installMethod(path, "/home/me", func(p string) bool { return p == "/usr/local/bin/frbit" }); got != want { + t.Errorf("installMethod(%s) = %v, want %v", path, got, want) + } + } +} + +func TestUpgrade(t *testing.T) { + for _, tc := range []struct { + method Method + goos, dir string + canWrite bool + command, url string + }{ + {MethodHomebrew, "darwin", "/opt/homebrew/Cellar/frbit/1.21.0/bin", true, "brew upgrade frbit", ""}, + {MethodNPM, "linux", "/usr/local/lib/node_modules/@fortrabbit/cli-linux-x64/bin", true, "npm install --global @fortrabbit/cli@latest", ""}, + {MethodNPM, "linux", "/usr/lib/node_modules/@fortrabbit/cli-linux-x64/bin", false, "", InstallDocsURL}, + {MethodScript, "linux", "/home/me/.local/bin", true, "curl -fsSL https://www.fortrabbit.com/install.sh | FRBIT_INSTALL_DIR=/home/me/.local/bin sh", ""}, + {MethodScript, "darwin", "/usr/local/bin", false, "", InstallDocsURL}, + {MethodScript, "darwin", "/Users/Jane Doe/.local/bin", true, "", InstallDocsURL}, + {MethodScript, "linux", "/home/a&b/.local/bin", true, "", InstallDocsURL}, + {MethodUnknown, "linux", "/nix/store/abc/bin", false, "", InstallDocsURL}, + {MethodNPM, "windows", "", true, "", InstallDocsURL}, + } { + command, url := upgrade(tc.method, tc.goos, tc.dir, tc.canWrite) + if command != tc.command || url != tc.url { + t.Errorf("upgrade(%v, %s, %s) = %q, %q; want %q, %q", tc.method, tc.goos, tc.dir, command, url, tc.command, tc.url) + } + } +} diff --git a/internal/update/writable_other.go b/internal/update/writable_other.go new file mode 100644 index 0000000..83ae53f --- /dev/null +++ b/internal/update/writable_other.go @@ -0,0 +1,6 @@ +//go:build !unix + +package update + +// writable cannot check without unix; Windows gets the docs link either way. +func writable(string) bool { return true } diff --git a/internal/update/writable_unix.go b/internal/update/writable_unix.go new file mode 100644 index 0000000..378213f --- /dev/null +++ b/internal/update/writable_unix.go @@ -0,0 +1,9 @@ +//go:build unix + +package update + +import "golang.org/x/sys/unix" + +func writable(dir string) bool { + return unix.Access(dir, unix.W_OK) == nil +} diff --git a/scripts/test-install.sh b/scripts/test-install.sh index 045ca31..4027ba7 100755 --- a/scripts/test-install.sh +++ b/scripts/test-install.sh @@ -98,7 +98,7 @@ printf '%s\n' "$out" | grep -qx 'managed frbit doctor' || { printf 'doctor did n test ! -e "$test_dir/home/.local/bin/frbit" || { printf 'installed next to a Homebrew frbit\n' >&2; exit 1; } out=$(isolated_install "$test_dir/npm/bin:$test_dir/npm-tool:$test_dir:$sys_path") -printf '%s\n' "$out" | grep -q 'npm update --global @fortrabbit/cli' || { printf 'npm frbit was not kept\n' >&2; exit 1; } +printf '%s\n' "$out" | grep -q 'npm install --global @fortrabbit/cli@latest' || { printf 'npm frbit was not kept\n' >&2; exit 1; } out=$(isolated_install "$test_dir/other/bin:$test_dir/home/.local/bin:$test_dir:$sys_path") printf '%s\n' "$out" | grep -q 'shadows the new install' || { printf 'shadowing frbit was not reported\n' >&2; exit 1; }