Files
magpie/internal/appdir/appdir.go
T
yetone 65f3a6417a settings: portable mode — a data folder beside magpie keeps everything magpie keeps (#508, suixinex) — like VS Code's portable mode, a folder named "data" (or a file named ".portable", the data folder then made beside it) next to magpie makes it keep all its own files there instead of the user's profile. Where magpie keeps its files is now decided in one place, internal/appdir: Config() (~/.config/magpie, $XDG_CONFIG_HOME/magpie; portable: data/), Cache() (~/.cache/magpie, $XDG_CACHE_HOME/magpie; portable: data/cache) and SystemCache() (the OS's cache folder, which the library's market lists and an update staged where magpie may not write used; portable: data/cache). settings.Dir()/Path(), provider.Path(), profile.Path(), the models.dev catalog, the exchange rate and the plugin market cache go through it, and everything built on them follows: providers, logins and saved accounts' folders, usage ledger, routing history and request archive, plugins, library, backups, sync state and lock, caller keys, redact key, stats. "Beside magpie" is the folder holding the executable with links followed; for a Mac app the folder holding Magpie.app (inside the bundle would break its signature), and for an AppImage the folder holding the .AppImage, not where it is mounted. The decision is made once, at start: an update moves the running copy aside (into .magpie-update on a Mac, to magpie.exe.old on Windows) and its files must not move with it; the updated copy replaces the executable or bundle only, so the data folder beside it is left alone. A portable magpie doesn't copy an old dial install's folders in. Not moved, as they aren't magpie's own: the agents' configs (~/.claude, ~/.codex…), the sign-ins agents keep in the system keychain (a saved Claude account's Claude Code keychain item is named after its folder's path on a Mac, so a portable folder moved elsewhere or to another Mac signs that account in again), autostart and link-handler registrations, and temporary folders. The single-instance id already covers the config folder, so a portable and an installed magpie run side by side, but the gateway port is one. Settings shows the config folder as portable. TestResolve (data folder, .portable, a file named data, a Mac bundle), TestResolveSymlinkedExe, TestResolveAppImage (Linux), TestFolders and TestPortableSettings (Path() lands in the data folder, Save writes there, ~/.config/magpie untouched); TestPortableSettings fails without the change (Path() is ~/.config/magpie/settings.json), TestResolveSymlinkedExe without following links. A nogui build run from a folder with data/ under a sandbox HOME wrote profiles.json, cli-identity.json and cache/models.json into data/ and nothing of magpie's into HOME. go vet ./internal/..., go build -tags nogui ./... (darwin, GOOS=linux), GOOS=windows go build ./..., go test -tags nogui ./... pass.
2026-10-02 13:00:22 +08:00

152 lines
4.5 KiB
Go

// Package appdir decides where magpie keeps its own files: the one place
// every other package asks (#508).
//
// Installed, that is ~/.config/magpie ($XDG_CONFIG_HOME/magpie) for what
// magpie keeps and ~/.cache/magpie ($XDG_CACHE_HOME/magpie) for what it
// can fetch again, as it always was. Portable, when a folder named "data"
// (or a file named ".portable") sits beside magpie, everything magpie
// keeps goes into that data folder and its caches into data/cache, and
// nothing of magpie's own is written to the user's profile, the way VS
// Code's portable mode works. "Beside magpie" is the folder holding the
// executable, with links followed; for a Mac app it is the folder holding
// Magpie.app (a file put inside the bundle would break its signature), and
// for an AppImage the folder holding the .AppImage file, not the folder it
// is mounted at while it runs.
//
// What isn't magpie's own stays where its owner reads it: the agents'
// configs (~/.claude, ~/.codex…), the sign-ins they keep in the system
// keychain, the folders the OS reads for autostart and link handling, and
// temporary folders.
//
// The decision is made once, from the executable as it was when magpie
// started: an update moving the running copy aside (into .magpie-update on
// a Mac, to magpie.exe.old on Windows) leaves it running from somewhere
// else, and its files must not move with it.
package appdir
import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)
const (
// DataName is the folder beside magpie that makes it portable and
// holds its files.
DataName = "data"
// MarkerName is a file beside magpie that makes it portable too, its
// files then going into a data folder made beside it.
MarkerName = ".portable"
)
var (
once sync.Once
portable string // the data folder, or "" when installed
)
// Portable is the data folder magpie keeps everything in, or "" when it
// isn't portable.
func Portable() string {
once.Do(func() {
exe, err := os.Executable()
if err != nil {
return
}
portable = Resolve(exe)
})
return portable
}
// UseExecutable decides again as if magpie ran from exe ("" for an
// installed magpie, whatever runs): for tests.
func UseExecutable(exe string) {
once.Do(func() {})
portable = ""
if exe != "" {
portable = Resolve(exe)
}
}
// Resolve is the data folder of a magpie run from exe, or "" when it isn't
// portable.
func Resolve(exe string) string {
if real, err := filepath.EvalSymlinks(exe); err == nil {
exe = real
}
base := Beside(exe)
data := filepath.Join(base, DataName)
if fi, err := os.Stat(data); err == nil && fi.IsDir() {
return absolute(data)
}
if fi, err := os.Stat(filepath.Join(base, MarkerName)); err == nil && !fi.IsDir() {
return absolute(data)
}
return ""
}
// Beside is the folder a portable magpie run from exe keeps its data folder
// in: the executable's own, the one holding the .app on a Mac, the one
// holding the .AppImage on Linux.
func Beside(exe string) string {
if img := os.Getenv("APPIMAGE"); img != "" && runtime.GOOS == "linux" {
return filepath.Dir(img)
}
dir := filepath.Dir(exe)
// …/Magpie.app/Contents/MacOS/magpie
if filepath.Base(dir) == "MacOS" && filepath.Base(filepath.Dir(dir)) == "Contents" {
if app := filepath.Dir(filepath.Dir(dir)); strings.EqualFold(filepath.Ext(app), ".app") {
return filepath.Dir(app)
}
}
return dir
}
func absolute(p string) string {
if a, err := filepath.Abs(p); err == nil {
return a
}
return p
}
// Config is the folder magpie keeps its settings, providers, usage and the
// rest of its own state in.
func Config() string {
if p := Portable(); p != "" {
return p
}
if x := os.Getenv("XDG_CONFIG_HOME"); x != "" {
return filepath.Join(x, "magpie")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "magpie")
}
// Cache is the folder for what magpie can fetch again (the models.dev
// catalog, exchange rates, market lists).
func Cache() string {
if p := Portable(); p != "" {
return filepath.Join(p, "cache")
}
if x := os.Getenv("XDG_CACHE_HOME"); x != "" {
return filepath.Join(x, "magpie")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".cache", "magpie")
}
// SystemCache is the cache folder the OS names for magpie
// (~/Library/Caches/magpie on a Mac, %LocalAppData%\magpie on Windows),
// which a few caches have always used; portable, it is Cache.
func SystemCache() (string, error) {
if p := Portable(); p != "" {
return filepath.Join(p, "cache"), nil
}
d, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(d, "magpie"), nil
}