-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for {{env(VAR)}} filling
- Loading branch information
1 parent
bc4d9e2
commit 1f734e8
Showing
5 changed files
with
102 additions
and
2 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
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,34 @@ | ||
package vars | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var paramRe = regexp.MustCompile(`(?m)\((.*)\)`) | ||
|
||
type EnvVars struct { | ||
valueMap map[string]string | ||
ready bool | ||
} | ||
|
||
func (ev EnvVars) Fill(holders []string) map[string][]string { | ||
vars := make(map[string][]string) | ||
for _, tag := range holders { | ||
if strings.HasPrefix(tag, "env(") { | ||
var name = ev.getInputParam(tag) | ||
log.Debugf("looking for envar %s", name) | ||
vars[tag] = append(vars[tag], os.Getenv(name)) | ||
} | ||
} | ||
return vars | ||
} | ||
|
||
func (ev EnvVars) getInputParam(param string) string { | ||
match := paramRe.FindStringSubmatch(param) | ||
if len(match) > 1 { | ||
return match[1] | ||
} | ||
return "" | ||
} |
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,58 @@ | ||
package vars | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const Name = "___MMOCK__TEST__ENV__VAR" | ||
|
||
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
|
||
func randSeq(n int) string { | ||
randString := make([]rune, n) | ||
for i := range randString { | ||
randString[i] = letters[rand.Intn(len(letters))] | ||
} | ||
return string(randString) | ||
} | ||
|
||
func TestEnvVar(t *testing.T) { | ||
var value = randSeq(18) | ||
os.Setenv(Name, value) | ||
|
||
var tag = fmt.Sprintf("env(%s)", Name) | ||
holders := []string{tag} | ||
env := EnvVars{} | ||
|
||
result := env.Fill(holders) | ||
foundValue, found := result[tag] | ||
|
||
os.Unsetenv(Name) | ||
|
||
if !found { | ||
t.Errorf("tag %s was not found", tag) | ||
} | ||
|
||
if !strings.Contains(foundValue[0], value) { | ||
t.Errorf("EnvVar %s Expected: %s, Actual: %s", Name, value, foundValue[0]) | ||
} | ||
|
||
var varNotPresent = "__NOT_PRESENT___" | ||
|
||
tag = fmt.Sprintf("env(%s)", varNotPresent) | ||
holders = []string{tag} | ||
env = EnvVars{} | ||
|
||
result = env.Fill(holders) | ||
foundValue, found = result[tag] | ||
|
||
os.Unsetenv(varNotPresent) | ||
|
||
if found && foundValue[0] != "" { | ||
t.Errorf("tag %s was found with value %v", tag, foundValue[0]) | ||
} | ||
} |
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
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