Skip to content

Commit

Permalink
feat(action): add functions to help with passing env vars into container
Browse files Browse the repository at this point in the history
Signed-off-by: AtomicFS <[email protected]>
  • Loading branch information
AtomicFS committed Jan 8, 2025
1 parent 3599c9f commit 254a9c5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
23 changes: 23 additions & 0 deletions action/environment/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT

// Package environment for is interacting with environment variables
package environment

import (
"os"
)

// FetchEnvVars when provided with list of environment variables is to
// return a map of variables and values for those that exist in the environment
func FetchEnvVars(variables []string) map[string]string {
result := make(map[string]string)

for _, variable := range variables {
value, exists := os.LookupEnv(variable)
if exists {
result[variable] = value
}
}

return result
}
62 changes: 62 additions & 0 deletions action/environment/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT

// Package environment
package environment

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidateConfig(t *testing.T) {
testCases := []struct {
name string
envCreate map[string]string
envSearchFor []string
envExpectedResult map[string]string
}{
{
name: "no env vars created, no found",
envCreate: map[string]string{},
envSearchFor: []string{"MY_VAR"},
envExpectedResult: map[string]string{},
},
{
name: "one created, one found",
envCreate: map[string]string{
"MY_VAR": "my_val",
},
envSearchFor: []string{"MY_VAR"},
envExpectedResult: map[string]string{
"MY_VAR": "my_val",
},
},
{
name: "some created, some found",
envCreate: map[string]string{
"MY_VAR": "my_val",
"MY_VAR2": "my_val2",
},
envSearchFor: []string{"MY_VAR"},
envExpectedResult: map[string]string{
"MY_VAR": "my_val",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for key, value := range tc.envCreate {
os.Setenv(key, value)
defer os.Unsetenv(key)
fmt.Printf("Setting %s = %s\n", key, value)
}

result := FetchEnvVars(tc.envSearchFor)

assert.Equal(t, tc.envExpectedResult, result)
})
}
}

0 comments on commit 254a9c5

Please sign in to comment.