diff --git a/internal/cli/runtime.go b/internal/cli/runtime.go index c4725a3..2e3e659 100644 --- a/internal/cli/runtime.go +++ b/internal/cli/runtime.go @@ -7,6 +7,7 @@ package cli import ( "io" "os" + "strings" "golang.org/x/term" ) @@ -101,3 +102,14 @@ func (r Runtime) SetVerbose(verbose bool) { r.setVerbose(verbose) } } + +// EnvValue returns the value and presence of one environment entry. +func (r Runtime) EnvValue(name string) (string, bool) { + prefix := name + "=" + for _, entry := range r.environment { + if strings.HasPrefix(entry, prefix) { + return strings.TrimPrefix(entry, prefix), true + } + } + return "", false +} diff --git a/internal/cli/runtime_test.go b/internal/cli/runtime_test.go index 2f16142..d087095 100644 --- a/internal/cli/runtime_test.go +++ b/internal/cli/runtime_test.go @@ -14,6 +14,7 @@ func TestCLIRuntime(t *testing.T) { {"stream defaults", testRuntimeStreams}, {"terminal predicates", testRuntimeTerminal}, {"verbosity callback", testRuntimeVerbose}, + {"environment lookup", testRuntimeEnvValue}, {"instance isolation", testRuntimeIsolation}, } for _, scenario := range scenarios { @@ -76,6 +77,17 @@ func testRuntimeVerbose(t *testing.T) { } } +func testRuntimeEnvValue(t *testing.T) { + runtime, _, _ := runtimeFixture(t, []string{"CATTERY_REPO=repo", "OTHER=x"}, nil) + value, present := runtime.EnvValue("CATTERY_REPO") + if !present || value != "repo" { + t.Fatalf("CATTERY_REPO = %q present = %v, want repo true", value, present) + } + if _, present := runtime.EnvValue("MISSING"); present { + t.Fatal("a missing variable must report absent") + } +} + func testRuntimeIsolation(t *testing.T) { first, _, _ := runtimeFixture(t, []string{"A=1", "B=2"}, nil) second, _, _ := runtimeFixture(t, []string{"C=3"}, nil)