fuzzyworld

template / ci-testing / files / name_pattern.go

Branch ci-testing · default branch · all branches

name_pattern.go

Blame: the commit that last changed each line · file · history

c62570e2 2022-09-23 Pavel Safronov1package main
2
71bdf574 2022-09-23 Pavel Safronov3import (
4 "errors"
05f9e8ef 2022-10-06 Pavel Safronov5 "fmt"
71bdf574 2022-09-23 Pavel Safronov6 "strings"
7 "text/template"
8 "time"
5c81baec 2022-11-20 Pavel Safronov9
10 "github.com/Masterminds/sprig/v3"
71bdf574 2022-09-23 Pavel Safronov11)
c62570e2 2022-09-23 Pavel Safronov12
687d5626 2022-10-28 Pavel Safronov13func createNewNameForPatterns(
14 now time.Time,
8c15a714 2022-11-04 Pavel Safronov15 patternData PrePatternData,
687d5626 2022-10-28 Pavel Safronov16 projectName string,
17 namePatterns []ParameterizedText,
10676da3 2022-11-06 Pavel Safronov18 extras map[string]string,
19 validator func(newName string) bool) (string, error) {
5e7795dc 2022-10-21 Pavel Safronov20 for index, namePattern := range namePatterns {
71bdf574 2022-09-23 Pavel Safronov21
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 Safronov31 template = template.Funcs(sprig.FuncMap())
71bdf574 2022-09-23 Pavel Safronov32
33 for {
34
1a02ef4e 2022-10-21 Pavel Safronov35 template, err := template.Parse(string(namePattern))
71bdf574 2022-09-23 Pavel Safronov36 if err != nil {
8c15a714 2022-11-04 Pavel Safronov37 return "", fmt.Errorf("error parsing name pattern %d: %w", index, err)
71bdf574 2022-09-23 Pavel Safronov38 }
39
40 sb := new(strings.Builder)
41 err = template.Execute(sb, patternData)
42 if err != nil {
8c15a714 2022-11-04 Pavel Safronov43 return "", fmt.Errorf("error executing name pattern %d: %w", index, err)
71bdf574 2022-09-23 Pavel Safronov44 }
45
46 newName := sb.String()
10676da3 2022-11-06 Pavel Safronov47 isValid := validator(newName)
71bdf574 2022-09-23 Pavel Safronov48 useIndex := invokeCount > 0
5e7795dc 2022-10-21 Pavel Safronov49 isLastPattern := index == len(namePatterns)-1
71bdf574 2022-09-23 Pavel Safronov50
10676da3 2022-11-06 Pavel Safronov51 // if new name is valid, use this name
52 if isValid {
8c15a714 2022-11-04 Pavel Safronov53 return newName, nil
71bdf574 2022-09-23 Pavel Safronov54 }
10676da3 2022-11-06 Pavel Safronov55
71bdf574 2022-09-23 Pavel Safronov56 // 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 Safronov64 return "", errors.New("cannot use function getVersion in a pattern that is not the last pattern")
71bdf574 2022-09-23 Pavel Safronov65 }
66
67 startingVersion++
68 }
69 }
8c15a714 2022-11-04 Pavel Safronov70 return "", errors.New("couldn't find a new name")
71bdf574 2022-09-23 Pavel Safronov71}