-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(action): add functions to help with passing env vars into container
Signed-off-by: AtomicFS <[email protected]>
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |