Files
magpie/internal/edit/env.go
T
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00

96 lines
2.5 KiB
Go

package edit
import (
"strings"
"github.com/yetone/magpie/internal/lazyre"
)
// dotenv files (KEY=value per line) as Gemini CLI reads from ~/.gemini/.env.
var envLine = lazyre.New(`^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$`)
// GetEnvFile reads one key from a dotenv file.
func GetEnvFile(path, key string) (string, bool) {
raw, err := readHeld(path)
if err != nil {
return "", false
}
// A key set twice takes its last value: dotenv readers, Gemini CLI's among
// them, let a later line override an earlier one.
lines := splitLines(string(raw))
for i := len(lines) - 1; i >= 0; i-- {
if m := envLine.FindStringSubmatch(lines[i]); m != nil && m[1] == key {
if v, ok := unquote(m[2]); ok {
return v, true
}
v := m[2]
if i := strings.IndexByte(v, '#'); i >= 0 {
v = strings.TrimSpace(v[:i])
}
return v, true
}
}
return "", false
}
// SetEnvFile sets keys in a dotenv file, replacing lines in place and
// appending new ones. Other lines are kept byte for byte.
func SetEnvFile(path string, kvs ...KV) error {
raw, err := Read(path)
if err != nil {
return err
}
lines := splitLines(string(raw))
for _, kv := range kvs {
v := toString(kv.Value)
if strings.ContainsAny(v, " #\"'$\r\n") {
// Gemini CLI's dotenv keeps backslashes literal inside single quotes.
if !strings.ContainsAny(v, "'\r\n") {
v = "'" + v + "'"
} else {
v = `"` + strings.ReplaceAll(strings.ReplaceAll(v, `\`, `\\`), `"`, `\"`) + `"`
}
}
lines = setLine(lines, kv.Path, kv.Path+"="+v, nil, func(l string) (string, bool) {
if m := envLine.FindStringSubmatch(l); m != nil {
return m[1], true
}
return "", false
})
}
lines = markEnv(lines, string(raw))
return WriteAtomic(path, []byte(joinLinesLike(lines, string(raw))))
}
// DelEnvFile removes keys from a dotenv file, and the file when they were
// all it held (Gemini CLI's .env, made by magpie for its key).
func DelEnvFile(path string, keys ...string) error {
raw, err := Read(path)
if err != nil || len(raw) == 0 {
return err
}
drop := map[string]bool{}
for _, k := range keys {
drop[k] = true
}
var out []string
changed := false
for _, l := range splitLines(string(raw)) {
if m := envLine.FindStringSubmatch(l); m != nil && drop[m[1]] {
changed = true
continue
}
out = append(out, l)
}
if !changed {
return nil
}
// a note magpie put above a key goes with it
out = markEnv(out, string(raw))
if strings.TrimSpace(strings.Join(withoutNotes(out), "")) == "" {
return Remove(path)
}
return WriteAtomic(path, []byte(joinLines(out)))
}