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") +}