Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/doctor/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
}
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
)
}

Expand Down
90 changes: 90 additions & 0 deletions internal/update/install.go
Original file line number Diff line number Diff line change
@@ -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 <prefix>/lib/node_modules and links it as <prefix>/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
}
48 changes: 48 additions & 0 deletions internal/update/install_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 6 additions & 0 deletions internal/update/writable_other.go
Original file line number Diff line number Diff line change
@@ -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 }
9 changes: 9 additions & 0 deletions internal/update/writable_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion scripts/test-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down