Skip to content

Commit

Permalink
[feat]: [CDS-82010]: Added support for all current case types support…
Browse files Browse the repository at this point in the history
…ed in migrator (#83)
  • Loading branch information
arvind-choudhary-h authored Oct 26, 2023
1 parent 3841b24 commit 1e7115b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 15 deletions.
54 changes: 43 additions & 11 deletions expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,72 @@ var ExpressionsMap = map[string]string{

var DynamicExpressions = map[string]interface{}{
"workflow.variables": func(key string) string {
return "<+stage.variables." + key + ">"
return "<+stage.variables." + formatString(key) + ">"
},
"pipeline.variables": func(key string) string {
return "<+pipeline.variables." + key + ">"
return "<+pipeline.variables." + formatString(key) + ">"
},
"serviceVariable": func(key string) string {
return "<+serviceVariables." + key + ">"
return "<+serviceVariables." + formatString(key) + ">"
},
"serviceVariables": func(key string) string {
return "<+serviceVariables." + key + ">"
return "<+serviceVariables." + formatString(key) + ">"
},
"service.variables": func(key string) string {
return "<+serviceVariables." + key + ">"
return "<+serviceVariables." + formatString(key) + ">"
},
"environmentVariable": func(key string) string {
return "<+env.variables." + key + ">"
return "<+env.variables." + formatString(key) + ">"
},
"environmentVariables": func(key string) string {
return "<+env.variables." + key + ">"
return "<+env.variables." + formatString(key) + ">"
},
"secrets.getValue(": func(key string) string {
return "<+secrets.getValue(\"" + getSecretKeyWithScope(key) + "\")>"
return "<+secrets.getValue(\"" + TrimQuotes(getSecretKeyWithScope(formatString(key))) + "\")>"
},
"app.defaults": func(key string) string {
return "<+variable." + key + ">"
return "<+variable." + formatString(key) + ">"
},
"configFile.getAsBase64(": func(key string) string {
return "<+configFile.getAsBase64(\"" + key + "\")>"
return "<+configFile.getAsBase64(\"" + TrimQuotes(formatString(key)) + "\")>"
},
"configFile.getAsString(": func(key string) string {
return "<+configFile.getAsString(\"" + key + "\")>"
return "<+configFile.getAsString(\"" + TrimQuotes(formatString(key)) + "\")>"
},
}

func formatString(key string) string {
var result string
defer func() {
if r := recover(); r != nil {
log.Errorf("Panic occurred: %v", r)
result = "FIX_ME"
}
}()

identifierCase := migrationReq.IdentifierCase
log.Debugf("key: %s, format: %s", key, identifierCase)
if len(key) == 0 {
result = "FIX_ME"
} else {
switch identifierCase {
case "CAMEL_CASE":
result = ToCamelCase(key)
case "LOWER_CASE":
result = ToLowerCase(key)
case "SNAKE_CASE":
result = ToSnakeCase(key)
case "HARNESS_UI_FORMAT":
result = GenerateHarnessUIFormatIdentifier(key)
default:
log.Info("Defaulting to Camel Case")
result = ToCamelCase(key)
}
}

return result
}

func getSecretKeyWithScope(key string) string {
camelCase := ToCamelCase(key)
switch migrationReq.SecretScope {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jedib0t/go-pretty/v6 v6.4.4 h1:N+gz6UngBPF4M288kiMURPHELDMIhF/Em35aYuKrsSc=
github.com/jedib0t/go-pretty/v6 v6.4.4/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
Expand Down
Binary file modified harness-upgrade
Binary file not shown.
55 changes: 51 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/yaml.v3"
"os"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/iancoleman/strcase"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"gopkg.in/yaml.v3"
"os"
"regexp"
"strings"
"unicode"
)

const (
Expand Down Expand Up @@ -172,6 +177,48 @@ func ReadFile(absFilePath string) (string, error) {
return string(d), err
}

func ToLowerCase(input string) string {
return strings.ToLower(input)
}

func ToSnakeCase(input string) string {
return strcase.ToSnake(input)
}

func TrimQuotes(input string) string {
if strings.HasPrefix(input, "\"") && strings.HasSuffix(input, "\"") {
return input[1 : len(input)-1]
}

return input
}

func GenerateHarnessUIFormatIdentifier(name string) string {
name = removeAccents(name)
name = stripStartingChars(name)
name = stripSpecialChars(name)
name = strings.ReplaceAll(name, " ", "_")

return name
}

func removeAccents(input string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
output, _, err := transform.String(t, input)
if err != nil {
return ""
}
return output
}

func stripStartingChars(s string) string {
return regexp.MustCompile("^[0-9-$]*").ReplaceAllString(s, "")
}

func stripSpecialChars(s string) string {
return regexp.MustCompile("[^0-9a-zA-Z_$ ]").ReplaceAllString(s, "")
}

func ToCamelCase(s string) string {
s = strings.TrimSpace(s)
s = strings.ToLower(s)
Expand Down

0 comments on commit 1e7115b

Please sign in to comment.