Skip to content

Commit

Permalink
feat(envx): add env
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jan 11, 2025
1 parent c250921 commit 8fd5e5d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions envx/env.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions envx/env_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 8fd5e5d

Please sign in to comment.