Skip to content

Commit

Permalink
Fix the secret identifier format to honor the case configured (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness authored Jul 17, 2024
1 parent f528be6 commit 2999dd1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 15 additions & 2 deletions expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var DynamicExpressions = map[string]interface{}{
return "<+env.variables." + formatString(key) + ">"
},
"secrets.getValue(": func(key string) string {
return "<+secrets.getValue(\"" + TrimQuotes(getSecretKeyWithScope(formatString(key))) + "\")>"
return "<+secrets.getValue(\"" + TrimQuotes(getSecretKeyWithScope(formatSecret(key))) + "\")>"
},
"app.defaults": func(key string) string {
return "<+variable." + formatString(key) + ">"
Expand All @@ -168,7 +168,19 @@ var DynamicExpressions = map[string]interface{}{
},
}

func formatSecret(key string) string {
identifierCase := migrationReq.IdentifierCase
if strings.Contains(migrationReq.Flags, "ALLOW_HARNESS_UI_FORMAT_WITH_HYPHENS") {
identifierCase = "HARNESS_UI_FORMAT_WITH_HYPHEN"
}
return formatStringWithCase(key, identifierCase)
}

func formatString(key string) string {
return formatStringWithCase(key, migrationReq.IdentifierCase)
}

func formatStringWithCase(key, identifierCase string) string {
var result string
defer func() {
if r := recover(); r != nil {
Expand All @@ -177,7 +189,6 @@ func formatString(key string) string {
}
}()

identifierCase := migrationReq.IdentifierCase
log.Debugf("key: %s, format: %s", key, identifierCase)
if len(key) == 0 {
result = "FIX_ME"
Expand All @@ -191,6 +202,8 @@ func formatString(key string) string {
result = ToSnakeCase(key)
case "HARNESS_UI_FORMAT":
result = GenerateHarnessUIFormatIdentifier(key)
case "HARNESS_UI_FORMAT_WITH_HYPHEN":
result = GenerateHarnessUIFormatWithHyphensIdentifier(key)
default:
log.Info("Defaulting to Camel Case")
result = ToCamelCase(key)
Expand Down
5 changes: 5 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func TrimQuotes(input string) string {
return input
}

func GenerateHarnessUIFormatWithHyphensIdentifier(name string) string {
newId := GenerateHarnessUIFormatIdentifier(strings.ReplaceAll(name, "-", "_"))
return strings.ReplaceAll(newId, "_", "-")
}

func GenerateHarnessUIFormatIdentifier(name string) string {
pattern := regexp.MustCompile(`<\+([^>]+)>`)
matches := pattern.FindAllStringSubmatch(name, -1)
Expand Down

0 comments on commit 2999dd1

Please sign in to comment.