Files
magpie/internal/agent/stash.go
T
yetone 402a96c39d Rename dial to magpie, with a magpie for a logo
The product, module path (github.com/yetone/magpie), binaries, env vars
(MAGPIE_ADDR, MAGPIE_DEV_UI, MAGPIE_THEME, MAGPIE_DEBUG), config and cache
folders (~/.config/magpie, ~/.cache/magpie), bundle id (com.yetone.magpie),
gateway provider id and token, and every UI string now say magpie.

On first run magpie copies ~/.config/dial and ~/.cache/dial to the new
folders when they don't exist yet (copy, not move, so a still-running dial
keeps working; the old folders can be deleted by hand later).

The logo is a small round magpie in profile: one path for the header and
the tray, and the app tile adds the white belly and the blue-green tail.
The header mark is drawn in ink rather than the accent colour, which
fought the text in light mode.
2026-09-23 23:55:11 +08:00

58 lines
1.2 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"github.com/yetone/magpie/internal/provider"
)
// The stash remembers what an agent's config said before magpie pointed it
// at the gateway, so switching back restores it instead of guessing.
func stashPath() string { return filepath.Join(filepath.Dir(provider.Path()), "stash.json") }
func stashLoad() map[string]string {
out := map[string]string{}
if b, err := os.ReadFile(stashPath()); err == nil {
json.Unmarshal(b, &out)
}
return out
}
func stash(kv map[string]string) {
m := stashLoad()
for k, v := range kv {
if v == "" {
delete(m, k)
} else {
m[k] = v
}
}
b, _ := json.MarshalIndent(m, "", " ")
os.MkdirAll(filepath.Dir(stashPath()), 0o755)
os.WriteFile(stashPath(), b, 0o600)
}
// forget drops stashed values without restoring them.
func forget(keys ...string) {
m := stashLoad()
for _, k := range keys {
delete(m, k)
}
b, _ := json.MarshalIndent(m, "", " ")
os.WriteFile(stashPath(), b, 0o600)
}
func unstash(key string) string {
m := stashLoad()
v := m[key]
if v != "" {
delete(m, key)
b, _ := json.MarshalIndent(m, "", " ")
os.WriteFile(stashPath(), b, 0o600)
}
return v
}