template / ci-testing / files / name_pattern.go
Branch ci-testing · default branch · all branches
name_pattern.go
c62570e2 2022-09-23 Pavel Safronov | 1 | package main |
| 2 | ||
71bdf574 2022-09-23 Pavel Safronov | 3 | import ( |
| 4 | "errors" | |
05f9e8ef 2022-10-06 Pavel Safronov | 5 | "fmt" |
71bdf574 2022-09-23 Pavel Safronov | 6 | "strings" |
| 7 | "text/template" | |
| 8 | "time" | |
5c81baec 2022-11-20 Pavel Safronov | 9 | |
| 10 | "github.com/Masterminds/sprig/v3" | |
71bdf574 2022-09-23 Pavel Safronov | 11 | ) |
c62570e2 2022-09-23 Pavel Safronov | 12 | |
687d5626 2022-10-28 Pavel Safronov | 13 | func createNewNameForPatterns( |
| 14 | now time.Time, | |
8c15a714 2022-11-04 Pavel Safronov | 15 | patternData PrePatternData, |
687d5626 2022-10-28 Pavel Safronov | 16 | projectName string, |
| 17 | namePatterns []ParameterizedText, | |
10676da3 2022-11-06 Pavel Safronov | 18 | extras map[string]string, |
| 19 | validator func(newName string) bool) (string, error) { | |
5e7795dc 2022-10-21 Pavel Safronov | 20 | for index, namePattern := range namePatterns { |
71bdf574 2022-09-23 Pavel Safronov | 21 | |
| 22 | startingVersion := 1 | |
| 23 | invokeCount := 0 | |
| 24 | funcMap := template.FuncMap{ | |
| 25 | "getVersion": func() int { | |
| 26 | invokeCount++ | |
| 27 | return startingVersion | |
| 28 | }, | |
| 29 | } | |
| 30 | template := template.New("template-name").Funcs(funcMap) | |
5c81baec 2022-11-20 Pavel Safronov | 31 | template = template.Funcs(sprig.FuncMap()) |
71bdf574 2022-09-23 Pavel Safronov | 32 | |
| 33 | for { | |
| 34 | ||
1a02ef4e 2022-10-21 Pavel Safronov | 35 | template, err := template.Parse(string(namePattern)) |
71bdf574 2022-09-23 Pavel Safronov | 36 | if err != nil { |
8c15a714 2022-11-04 Pavel Safronov | 37 | return "", fmt.Errorf("error parsing name pattern %d: %w", index, err) |
71bdf574 2022-09-23 Pavel Safronov | 38 | } |
| 39 | ||
| 40 | sb := new(strings.Builder) | |
| 41 | err = template.Execute(sb, patternData) | |
| 42 | if err != nil { | |
8c15a714 2022-11-04 Pavel Safronov | 43 | return "", fmt.Errorf("error executing name pattern %d: %w", index, err) |
71bdf574 2022-09-23 Pavel Safronov | 44 | } |
| 45 | ||
| 46 | newName := sb.String() | |
10676da3 2022-11-06 Pavel Safronov | 47 | isValid := validator(newName) |
71bdf574 2022-09-23 Pavel Safronov | 48 | useIndex := invokeCount > 0 |
5e7795dc 2022-10-21 Pavel Safronov | 49 | isLastPattern := index == len(namePatterns)-1 |
71bdf574 2022-09-23 Pavel Safronov | 50 | |
10676da3 2022-11-06 Pavel Safronov | 51 | // if new name is valid, use this name |
| 52 | if isValid { | |
8c15a714 2022-11-04 Pavel Safronov | 53 | return newName, nil |
71bdf574 2022-09-23 Pavel Safronov | 54 | } |
10676da3 2022-11-06 Pavel Safronov | 55 | |
71bdf574 2022-09-23 Pavel Safronov | 56 | // if index is not being used (template did not invoke getVersion) |
| 57 | // then we're done | |
| 58 | if !useIndex { | |
| 59 | break | |
| 60 | } | |
| 61 | ||
| 62 | // if getVersion was used, but this isn't the last pattern, report an error | |
| 63 | if useIndex && !isLastPattern { | |
8c15a714 2022-11-04 Pavel Safronov | 64 | return "", errors.New("cannot use function getVersion in a pattern that is not the last pattern") |
71bdf574 2022-09-23 Pavel Safronov | 65 | } |
| 66 | ||
| 67 | startingVersion++ | |
| 68 | } | |
| 69 | } | |
8c15a714 2022-11-04 Pavel Safronov | 70 | return "", errors.New("couldn't find a new name") |
71bdf574 2022-09-23 Pavel Safronov | 71 | } |