Files
skillshare/internal/version/homebrew.go
T
Willie 779ec26ff5 fix(upgrade): elevate only the binary replacement with sudo
When the binary lived in a root-owned directory, upgrade re-ran the whole
command under sudo. The built-in skill download, UI asset cache, and oplog
then landed in the user's home as root-owned files, and a later git pull
of the skills source failed with "unable to unlink old
'skillshare/SKILL.md': Permission denied".

The sudo child now only receives an internal --replace-binary=<version>
flag that downloads and swaps the binary. The parent waits for it and
does everything else as the invoking user. The needs-password hint shown
by the CLI and dashboard drops its sudo prefix, since a fully root
upgrade reproduces the same breakage.
2026-09-27 02:10:00 +08:00

79 lines
2.3 KiB
Go

package version
import (
"encoding/json"
"fmt"
"os/exec"
"strings"
)
// InstallMethod indicates how skillshare was installed.
type InstallMethod int
const (
InstallDirect InstallMethod = iota // GitHub release / go install / manual
InstallBrew // Homebrew (brew install)
)
// UpgradeCommand returns the CLI command the user should run to upgrade.
func (m InstallMethod) UpgradeCommand() string {
if m == InstallBrew {
return "brew upgrade skillshare"
}
return "skillshare upgrade"
}
// DetectInstallMethod determines the installation method from the resolved
// executable path. The caller should resolve symlinks before calling this.
func DetectInstallMethod(execPath string) InstallMethod {
homebrewPrefixes := []string{
"/usr/local/Cellar/skillshare",
"/opt/homebrew/Cellar/skillshare",
"/home/linuxbrew/.linuxbrew/Cellar/skillshare",
}
for _, prefix := range homebrewPrefixes {
if strings.HasPrefix(execPath, prefix) {
return InstallBrew
}
}
return InstallDirect
}
// brewInfoResult mirrors the subset of `brew info --json=v2` we need.
type brewInfoResult struct {
Formulae []struct {
Versions struct {
Stable string `json:"stable"`
} `json:"versions"`
} `json:"formulae"`
}
// FetchBrewLatestVersion returns the latest version available in the
// Homebrew formula by running `brew info --json=v2 skillshare`.
func FetchBrewLatestVersion() (string, error) {
out, err := exec.Command("brew", "info", "--json=v2", "skillshare").Output()
if err != nil {
return "", fmt.Errorf("brew info failed: %w", err)
}
var info brewInfoResult
if err := json.Unmarshal(out, &info); err != nil {
return "", fmt.Errorf("failed to parse brew info: %w", err)
}
if len(info.Formulae) == 0 || info.Formulae[0].Versions.Stable == "" {
return "", fmt.Errorf("no stable version found in brew info")
}
return info.Formulae[0].Versions.Stable, nil
}
// SudoUpgradeHint is the command shown when an upgrade needs a password but
// has no terminal to ask on. The dashboard matches on it to explain the failure.
// It has no sudo prefix: the CLI elevates only the binary replacement, while a
// fully root upgrade leaves root-owned skill files that later break git pull.
const SudoUpgradeHint = "skillshare upgrade"
// SudoUpgradeHintPrefix precedes SudoUpgradeHint in the CLI error.
const SudoUpgradeHintPrefix = "run in a terminal: "