Skip to content

Commit

Permalink
Merge 4b5db4d into a196fe2
Browse files Browse the repository at this point in the history
  • Loading branch information
vhvb1989 committed Feb 22, 2024
2 parents a196fe2 + 4b5db4d commit d62cc4c
Show file tree
Hide file tree
Showing 30 changed files with 808 additions and 113 deletions.
2 changes: 2 additions & 0 deletions cli/azd/cmd/container.go
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/ioc"
"github.com/azure/azure-dev/cli/azd/pkg/keyvault"
"github.com/azure/azure-dev/cli/azd/pkg/kubelogin"
"github.com/azure/azure-dev/cli/azd/pkg/kustomize"
"github.com/azure/azure-dev/cli/azd/pkg/lazy"
Expand Down Expand Up @@ -440,6 +441,7 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
container.MustRegisterSingleton(azcli.NewAdService)
container.MustRegisterSingleton(azcli.NewContainerRegistryService)
container.MustRegisterSingleton(containerapps.NewContainerAppService)
container.MustRegisterSingleton(keyvault.NewKeyVaultService)
container.MustRegisterScoped(project.NewContainerHelper)
container.MustRegisterSingleton(azcli.NewSpringService)

Expand Down
18 changes: 16 additions & 2 deletions cli/azd/internal/scaffold/funcs.go
Expand Up @@ -158,14 +158,17 @@ func ContainerAppSecretName(name string) string {
return strings.ReplaceAll(strings.ToLower(name), "_", "-")
}

// alphanumericAndDashesRegex is a regular expression pattern used to match alphanumeric characters and dashes enclosed
// in square brackets.
var alphanumericAndDashesRegex = regexp.MustCompile(`(\['[a-zA-Z0-9\-]+'\])`)

// ToDotNotation receives a string and if it is on the form of "${inputs['resourceName']['inputName']}" it returns a new
// string using dot notation, i.e. "${inputs.resourceName.InputName}".
// Otherwise, the original string is returned adding quotes.
// Note: If resourceName or inputName container `-`
func ToDotNotation(s string) string {
if strings.HasPrefix(s, "${inputs['") && strings.HasSuffix(s, "']}") {
re := regexp.MustCompile(`(\['[a-zA-Z0-9\-]+'\])`)
updated := re.ReplaceAllStringFunc(s, func(sub string) string {
updated := alphanumericAndDashesRegex.ReplaceAllStringFunc(s, func(sub string) string {
noBrackets := strings.TrimRight(strings.TrimLeft(sub, "['"), "']")
if !strings.Contains(noBrackets, "-") {
return "." + noBrackets
Expand All @@ -177,6 +180,17 @@ func ToDotNotation(s string) string {
return fmt.Sprintf("'%s'", s)
}

// camelCaseRegex is a regular expression used to match camel case patterns.
// It matches a lowercase letter or digit followed by an uppercase letter.
var camelCaseRegex = regexp.MustCompile(`([a-z0-9])([A-Z])`)

// EnvFormat takes an input parameter like `fooParam` which is expected to be in camel case and returns it in
// upper snake case with env var template, like `${AZURE_FOO_PARAM}`.
func EnvFormat(src string) string {
snake := strings.ToUpper(camelCaseRegex.ReplaceAllString(src, "${1}_${2}"))
return fmt.Sprintf("${AZURE_%s}", snake)
}

// ContainerAppInfix returns a suitable infix for a container app resource.
//
// The name is treated to only contain alphanumeric and dash characters, with no repeated dashes, and no dashes
Expand Down
46 changes: 46 additions & 0 deletions cli/azd/internal/scaffold/funcs_test.go
Expand Up @@ -105,3 +105,49 @@ func Test_ToDotNotation(t *testing.T) {
})
}
}

func Test_EnvFormat(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "no uppercase letters",
input: "myenv",
expected: "${AZURE_MYENV}",
},
{
name: "single uppercase letter",
input: "myEnv",
expected: "${AZURE_MY_ENV}",
},
{
name: "multiple uppercase letters",
input: "myEnvFormat",
expected: "${AZURE_MY_ENV_FORMAT}",
},
{
name: "uppercase letters at the beginning",
input: "EnvFormat",
expected: "${AZURE_ENV_FORMAT}",
},
{
name: "uppercase letters at the end",
input: "envFormaT",
expected: "${AZURE_ENV_FORMA_T}",
},
{
name: "uppercase letters in the middle",
input: "envFormatString",
expected: "${AZURE_ENV_FORMAT_STRING}",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := EnvFormat(tt.input)
assert.Equal(t, tt.expected, actual)
})
}
}

0 comments on commit d62cc4c

Please sign in to comment.