env: Format() emits only managed vars, not entire process env

This commit is contained in:
Levi Neely 2026-06-15 10:49:30 +02:00
parent 1dbc75cecc
commit 391f633785
2 changed files with 8 additions and 6 deletions

10
pkg/env/env.go vendored
View File

@ -106,12 +106,14 @@ func All() map[string]string {
return m
}
// Format returns the full process environment as NAME=VALUE lines, suitable
// for serving as ollie/env.
// Format returns the managed environment variables as NAME=VALUE lines,
// suitable for serving as ollie/env.
func Format() []byte {
var sb strings.Builder
for _, pair := range os.Environ() {
fmt.Fprintf(&sb, "%s\n", pair)
for _, k := range managed {
if v := os.Getenv(k); v != "" {
fmt.Fprintf(&sb, "%s=%s\n", k, v)
}
}
return []byte(sb.String())
}

4
pkg/env/env_test.go vendored
View File

@ -45,9 +45,9 @@ func TestAll(t *testing.T) {
}
func TestFormat(t *testing.T) {
t.Setenv("OLLIE_TEST_FMT", "xyz")
t.Setenv("OLLIE_TMP_PATH", "/tmp/test-fmt")
out := string(Format())
if !strings.Contains(out, "OLLIE_TEST_FMT=xyz") {
if !strings.Contains(out, "OLLIE_TMP_PATH=/tmp/test-fmt") {
t.Errorf("Format() missing expected var, got:\n%s", out)
}
// Every line should be NAME=VALUE\n