From 8fd5e5deda3fffc2c799387098466b3ada3f37b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Sat, 11 Jan 2025 20:08:54 +0000 Subject: [PATCH] feat(envx): add env --- envx/env.go | 25 +++++++++++++++++++++++++ envx/env_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 envx/env.go create mode 100644 envx/env_test.go diff --git a/envx/env.go b/envx/env.go new file mode 100644 index 0000000..7f2c16b --- /dev/null +++ b/envx/env.go @@ -0,0 +1,25 @@ +package envx + +// Env is the environment variables. +type Env map[string]string + +// Copy returns a copy of the environment. +func (e Env) Copy() Env { + out := Env{} + for k, v := range e { + out[k] = v + } + + return out +} + +// Strings returns the current environment as a list of strings, suitable for +// os executions. +func (e Env) Strings() []string { + result := make([]string, 0, len(e)) + for k, v := range e { + result = append(result, k+"="+v) + } + + return result +} diff --git a/envx/env_test.go b/envx/env_test.go new file mode 100644 index 0000000..e19cb46 --- /dev/null +++ b/envx/env_test.go @@ -0,0 +1,26 @@ +package envx_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/zeiss/pkg/envx" +) + +func TestEnv(t *testing.T) { + t.Parallel() + + // Create a new environment. + e := envx.Env{ + "key1": "value1", + "key2": "value2", + } + + e2 := e.Copy() + assert.Equal(t, e, e2) + + strings := e.Strings() + assert.Len(t, strings, 2) + assert.Contains(t, strings, "key1=value1") + assert.Contains(t, strings, "key2=value2") +}