diff --git a/internal/prepare.go b/internal/prepare.go index e6614b16..e4521ca6 100644 --- a/internal/prepare.go +++ b/internal/prepare.go @@ -25,6 +25,7 @@ import ( "os" "path/filepath" "regexp" + "strconv" "strings" "time" @@ -38,11 +39,18 @@ import ( ) var ( - charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + charsetLowercaseLetters = []rune("abcdefghijklmnopqrstuvwxyz") + charsetUppercaseLetters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ") + charsetLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + charsetAlphanumeric = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + charsetAlphanumericLower = []rune("abcdefghijklmnopqrstuvwxyz0123456789") dataSourceRegex = regexp.MustCompile(`\${data\.(.*?)}`) randomStrRegex = regexp.MustCompile(`\${Rand\.(.*?)}`) + defaultRandomStrLength = 8 + caseDirectory = "case" ) @@ -153,10 +161,32 @@ func (p *preparer) injectValues(manifestData string, dataSourceMap map[string]st // Inject random strings randomKeys := randomStrRegex.FindAllStringSubmatch(manifestData, -1) for _, randomKey := range randomKeys { - switch randomKey[1] { + randStrLength := defaultRandomStrLength + randKind, randLength, found := strings.Cut(randomKey[1], "#") + if found { + if rsr, err := strconv.Atoi(randLength); err == nil && rsr > 0 { + randStrLength = rsr + } + } + switch randKind { case "RFC1123Subdomain": r := generateRFC1123SubdomainCompatibleString() manifestData = strings.Replace(manifestData, randomKey[0], r, 1) + case "Alphanumeric": + r := generateAlphanumericString(randStrLength) + manifestData = strings.Replace(manifestData, randomKey[0], r, 1) + case "AlphanumericLowercase": + r := generateAlphanumericLowercaseString(randStrLength) + manifestData = strings.Replace(manifestData, randomKey[0], r, 1) + case "LettersLowercase": + r := generateLowercaseLetterString(randStrLength) + manifestData = strings.Replace(manifestData, randomKey[0], r, 1) + case "LettersUppercase": + r := generateUppercaseLetterString(randStrLength) + manifestData = strings.Replace(manifestData, randomKey[0], r, 1) + case "Letters": + r := generateLetterString(randStrLength) + manifestData = strings.Replace(manifestData, randomKey[0], r, 1) default: continue } @@ -172,3 +202,34 @@ func generateRFC1123SubdomainCompatibleString() string { } return fmt.Sprintf("op-%s", string(s)) } + +func generateStringWithCharset(charset []rune, length int) string { + if length < 1 { + length = defaultRandomStrLength + } + s := make([]rune, length) + for i := range s { + s[i] = charset[rand.Intn(len(charset))] //nolint:gosec // no need for crypto/rand here + } + return string(s) +} + +func generateAlphanumericString(length int) string { + return generateStringWithCharset(charsetAlphanumeric, length) +} + +func generateAlphanumericLowercaseString(length int) string { + return generateStringWithCharset(charsetAlphanumericLower, length) +} + +func generateLowercaseLetterString(length int) string { + return generateStringWithCharset(charsetLowercaseLetters, length) +} + +func generateUppercaseLetterString(length int) string { + return generateStringWithCharset(charsetUppercaseLetters, length) +} + +func generateLetterString(length int) string { + return generateStringWithCharset(charsetLetters, length) +}