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
11 changes: 10 additions & 1 deletion internal/version/install_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ type InstallInfo struct {
}

const rawInstallCommand = "curl -sSL https://github.com/groundsgg/grounds-cli/releases/latest/download/install.sh | bash"
const homebrewFormulaUpdateCommand = "brew upgrade groundsgg/tap/grounds"
const homebrewCaskUpdateCommand = "brew upgrade --cask groundsgg/tap/grounds"

func DetectInstallMethod(executablePath, homeDir string) InstallInfo {
normalized := normalizePath(executablePath)
if resolved, err := filepath.EvalSymlinks(executablePath); err == nil {
normalized = normalizePath(resolved)
}
home := normalizePath(homeDir)

if strings.Contains(normalized, "/homebrew/cellar/") || strings.Contains(normalized, "/cellar/grounds/") {
return InstallInfo{Method: InstallHomebrew, UpdateCommand: "brew upgrade groundsgg/tap/grounds"}
return InstallInfo{Method: InstallHomebrew, UpdateCommand: homebrewFormulaUpdateCommand}
}

if strings.Contains(normalized, "/caskroom/grounds/") {
return InstallInfo{Method: InstallHomebrew, UpdateCommand: homebrewCaskUpdateCommand}
}

if strings.Contains(normalized, "/scoop/apps/grounds/") {
Expand Down
46 changes: 45 additions & 1 deletion internal/version/install_method_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package version

import "testing"
import (
"os"
"path/filepath"
"testing"
)

func TestDetectInstallMethod(t *testing.T) {
tests := []struct {
Expand All @@ -16,6 +20,17 @@ func TestDetectInstallMethod(t *testing.T) {
wantMethod: InstallHomebrew,
wantCommand: "brew upgrade groundsgg/tap/grounds",
},
{
name: "homebrew cask linux target",
path: "/home/linuxbrew/.linuxbrew/Caskroom/grounds/0.1.14/grounds",
wantMethod: InstallHomebrew,
wantCommand: "brew upgrade --cask groundsgg/tap/grounds",
},
{
name: "homebrew prefix bin path without resolved target is unknown",
path: "/tmp/nonexistent-homebrew-prefix/bin/grounds",
wantMethod: InstallUnknown,
},
{
name: "scoop",
path: `C:\Users\Lukas\scoop\apps\grounds\current\grounds.exe`,
Expand Down Expand Up @@ -67,3 +82,32 @@ func TestDetectInstallMethod(t *testing.T) {
})
}
}

func TestDetectInstallMethodResolvesHomebrewCaskSymlink(t *testing.T) {
root := t.TempDir()
targetDir := filepath.Join(root, "Caskroom", "grounds", "0.1.14")
if err := os.MkdirAll(targetDir, 0o755); err != nil {
t.Fatalf("create target dir: %v", err)
}
target := filepath.Join(targetDir, "grounds")
if err := os.WriteFile(target, []byte("binary"), 0o755); err != nil {
t.Fatalf("create target: %v", err)
}

binDir := filepath.Join(root, "bin")
if err := os.MkdirAll(binDir, 0o755); err != nil {
t.Fatalf("create bin dir: %v", err)
}
link := filepath.Join(binDir, "grounds")
if err := os.Symlink(target, link); err != nil {
t.Fatalf("create symlink: %v", err)
}

got := DetectInstallMethod(link, "")
if got.Method != InstallHomebrew {
t.Fatalf("method = %q, want %q", got.Method, InstallHomebrew)
}
if got.UpdateCommand != homebrewCaskUpdateCommand {
t.Fatalf("update command = %q, want %q", got.UpdateCommand, homebrewCaskUpdateCommand)
}
}
Loading