-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compose: add experimental envsubst support
- Loading branch information
1 parent
904f96e
commit ef5c851
Showing
5 changed files
with
263 additions
and
5 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
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,76 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package project | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/azure/azure-dev/cli/azd/internal/scaffold" | ||
) | ||
|
||
func Test_evalEnvValue(t *testing.T) { | ||
tests := []struct { | ||
// input | ||
value string | ||
valueIsSecret bool | ||
// output | ||
want string | ||
wantParams []scaffold.Parameter | ||
}{ | ||
{"foo", false, "'foo'", nil}, | ||
{"${MY_VAR}", false, "myVar", []scaffold.Parameter{{Name: "myVar", Value: "${MY_VAR}", Type: "string"}}}, | ||
|
||
{"${MY_SECRET}", true, "mySecret", | ||
[]scaffold.Parameter{ | ||
{Name: "mySecret", Value: "${MY_SECRET}", Type: "string", Secret: true}}}, | ||
|
||
{"Hello, ${world:=okay}!", false, "world", | ||
[]scaffold.Parameter{ | ||
{Name: "world", Value: "${world:=okay}", Type: "string"}}}, | ||
|
||
{"${CAT} and ${DOG}", false, "'${cat} and ${dog}'", | ||
[]scaffold.Parameter{ | ||
{Name: "cat", Value: "${CAT}", Type: "string"}, | ||
{Name: "dog", Value: "${DOG}", Type: "string"}}}, | ||
|
||
{"${DB_HOST:='local'}:${DB_USERNAME:='okay'}", true, "'${dbHost}:${dbUsername}'", | ||
[]scaffold.Parameter{ | ||
{Name: "dbHost", Value: "${DB_HOST:='local'}", Type: "string", Secret: true}, | ||
{Name: "dbUsername", Value: "${DB_USERNAME:='okay'}", Type: "string", Secret: true}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.value, func(t *testing.T) { | ||
spec := &scaffold.InfraSpec{} | ||
evaluated := evalEnvSubst(tt.value, tt.valueIsSecret, spec) | ||
if tt.want != evaluated { | ||
t.Errorf("evalEnvValue() evaluatedValue = %v, want %v", evaluated, tt.want) | ||
} | ||
|
||
for i, param := range tt.wantParams { | ||
found := false | ||
for _, generated := range spec.Parameters { | ||
if generated.Name == param.Name { | ||
if generated.Secret != param.Secret { | ||
t.Errorf("evalEnvValue() secret = %v, want %v", generated.Secret, param.Secret) | ||
} | ||
|
||
if generated.Value != param.Value { | ||
t.Errorf("evalEnvValue() value = %v, want %v", generated.Value, param.Value) | ||
} | ||
|
||
if generated.Type != param.Type { | ||
t.Errorf("evalEnvValue() type = %v, want %v", generated.Type, param.Type) | ||
} | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
t.Errorf("evalEnvValue() parameter = %v not found", spec.Parameters[i].Name) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package project | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
type location struct { | ||
start int | ||
stop int | ||
} | ||
|
||
// parseEnvSubstVariables parses the envsubst expression(s) present in a string. | ||
// substitutions, returning the locations of the expressions and the names of the variables. | ||
// | ||
// It works with both: | ||
// - ${var} and | ||
// - ${var:=default} syntaxes | ||
func parseEnvSubstVariables(s string) (names []string, expressions []location) { | ||
inVar := false | ||
inVarName := false | ||
name := strings.Builder{} | ||
|
||
i := 0 | ||
start := 0 // start of the variable expression | ||
for i < len(s) { | ||
if s[i] == '$' && i+1 < len(s) && s[i+1] == '{' { // detect ${ sequence | ||
inVar = true | ||
inVarName = true | ||
start = i | ||
i += len("${") | ||
continue | ||
} | ||
|
||
if inVar { | ||
if inVarName { // detect the end of the variable name | ||
// a variable name can contain letters, digits, and underscores, and nothing else. | ||
if unicode.IsLetter(rune(s[i])) || unicode.IsDigit(rune(s[i])) || s[i] == '_' { | ||
_ = name.WriteByte(s[i]) | ||
} else { // a non-matching character means we've reached the end of the name | ||
inVarName = false | ||
} | ||
} | ||
|
||
if s[i] == '}' { // detect the end of the variable expression | ||
inVar = false | ||
names = append(names, name.String()) | ||
name.Reset() | ||
expressions = append(expressions, location{start, i}) | ||
} | ||
} | ||
|
||
i++ | ||
} | ||
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,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package project | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_parseEnvSubtVariables(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
wantNames []string | ||
wantExpressions []location | ||
}{ | ||
{"empty", "", nil, nil}, | ||
{"no variables", "foo", nil, nil}, | ||
{"one variable", "${foo}", []string{"foo"}, []location{{0, 5}}}, | ||
{"two variables", "${foo} ${bar}", []string{"foo", "bar"}, []location{{0, 5}, {7, 12}}}, | ||
{"two variables with text", "${foo:=value} ${bar#subs}", []string{"foo", "bar"}, []location{{0, 12}, {14, 24}}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotNames, gotExpressions := parseEnvSubstVariables(tt.input) | ||
if !reflect.DeepEqual(gotNames, tt.wantNames) { | ||
t.Errorf("parseEnvSubtVariables() gotNames = %v, want %v", gotNames, tt.wantNames) | ||
} | ||
if !reflect.DeepEqual(gotExpressions, tt.wantExpressions) { | ||
t.Errorf("parseEnvSubtVariables() gotExpressions = %v, want %v", gotExpressions, tt.wantExpressions) | ||
} | ||
}) | ||
} | ||
} |