Files
yetone d6daa40ce3 update, gui: Settings › About › Command line says what magpie runs in a new terminal and adds it (PAMI on Discord) — the app downloaded from the site, and every Windows one, left terminals without the magpie command, and nothing in the GUI said whether it was there. A Command line row now asks each shell installed (zsh, bash, fish and the login shell; PowerShell and cmd on Windows) for its PATH and says, with a dot each, whether its magpie runs this app, another copy (with that copy's version, read from its .app's Info.plist or Homebrew Cellar folder, never by running it), or none. Add to PATH links this app's program (Homebrew's opt link, the AppImage) into the folder on the default shell's PATH whose magpie runs first, else ~/.local/bin or the like when on PATH, replacing another copy there as install.sh's ln -sf does; on Windows it puts the app's folder first in HKCU\Environment Path and broadcasts WM_SETTINGCHANGE. A shell whose PATH lacks that folder gets its own button naming its profile (~/.zshrc, ~/.bash_profile on the Mac or ~/.bashrc, fish's config.fish), which links into ~/.local/bin and adds that folder with one marked line, once; no profile is written otherwise. A translocated or disk-image app is told to move first and gets no button. It says new terminals take the change (on Windows: new PowerShell/cmd windows, restart Windows Terminal). GET/POST /api/cli; en/zh/ja/de. TestCLIOnPath (temp HOME, stubbed shells: link, fish profile once, a shadowing copy replaced, no folder → zsh's profile kept and appended, an unknown shell refused, plist version, translocated refused) and TestPathFirst are new; cli-path.test.cjs (WebKit, en and zh: per-shell dots, Add posts {} and turns green, fish's button posts fish alone, Windows lists PowerShell and cmd, translocated gets no button, no scroll, no select, no left border, translations) fails without the change; cli-behind and update-auto pass. Not tried on a real Windows machine. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass (TestOTelWholeWaterfallFallback and TestDevinSignedInOnlyInMagpie failed once each under load and pass alone).
2026-10-05 17:49:20 +08:00

98 lines
2.6 KiB
Go

//go:build !windows
package update
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
)
// installedShells are the shells installed here that a terminal may open:
// zsh, bash and fish, the login shell ($SHELL) marked, and that one too
// when it is another (nu, sh), with no profile magpie writes.
func installedShells() []cliShell {
login := os.Getenv("SHELL")
var out []cliShell
for _, n := range []string{"zsh", "bash", "fish"} {
p := ""
for _, d := range []string{"/bin", "/usr/bin", "/opt/homebrew/bin", "/usr/local/bin"} {
if st, err := os.Stat(filepath.Join(d, n)); err == nil && !st.IsDir() {
p = filepath.Join(d, n)
break
}
}
if p == "" {
p, _ = exec.LookPath(n)
}
if p == "" {
continue
}
out = append(out, cliShell{Name: n, Path: p, Default: filepath.Base(login) == n})
}
if login != "" && filepath.IsAbs(login) && !slices.ContainsFunc(out, func(s cliShell) bool { return s.Default }) {
out = append([]cliShell{{Name: filepath.Base(login), Path: login, Default: true}}, out...)
}
return out
}
// cliDir is the folder a link to exe goes in for a shell with PATH dirs:
// where the magpie it runs now (cmd) is, when that is another copy in a
// folder magpie can write — the link takes its place, as install.sh's
// ln -sf does — else the first of the folders a user's commands usually go
// in that is on it and can be written to; "" when none is.
func cliDir(exe string, dirs []string, cmd string) string {
if cmd != "" {
if d := filepath.Dir(cmd); Writable(d) {
return d
}
return ""
}
var cands []string
if h := os.Getenv("HOME"); filepath.IsAbs(h) {
cands = append(cands, filepath.Join(h, ".local", "bin"), filepath.Join(h, "bin"))
}
home := len(cands)
cands = append(cands, "/opt/homebrew/bin", "/usr/local/bin", "/home/linuxbrew/.linuxbrew/bin")
for i, d := range cands {
if !slices.Contains(dirs, d) {
continue
}
st, err := os.Stat(d)
if err == nil && st.IsDir() && Writable(d) {
return d
}
// on PATH but not made yet: the home's are made
if i < home && os.IsNotExist(err) {
return d
}
}
return ""
}
// cliPlace links exe as magpie in dir, over what is there.
func cliPlace(exe, dir string) error {
dst := filepath.Join(dir, "magpie")
if sameFile(dst, exe) {
return nil
}
if st, err := os.Lstat(dst); err == nil && st.IsDir() {
return fmt.Errorf("%s is a folder", dst)
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
tmp := dst + ".magpie-new"
os.Remove(tmp)
if err := os.Symlink(exe, tmp); err != nil {
return err
}
if err := os.Rename(tmp, dst); err != nil {
os.Remove(tmp)
return err
}
return nil
}