Skip to content

Commit ea9f702

Browse files
Use sprig instead of custom go template functions
- Remove all except formatSubdomain and unquote - Improve curly template encodings like base64, first, etc. - Improve utils Keys and SortedKeys
1 parent 7e418ae commit ea9f702

12 files changed

Lines changed: 483 additions & 512 deletions

cmd/hub/compose/elaborate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func Elaborate(manifestFilename string,
120120
// at least there will be a warning for mismatched values
121121
setValuesFromState(stackManifest.Parameters, st, useStateStackParameters)
122122
stackManifest.Requires = connectStateProvides(stackManifest.Requires, st.Provides)
123-
platformProvides = util.MergeUnique(platformProvides, util.SortedKeys2(st.Provides))
123+
platformProvides = util.MergeUnique(platformProvides, util.SortedKeys(st.Provides))
124124
}
125125
if len(platformProvides) > 0 {
126126
stackManifest.Platform.Provides = util.MergeUnique(stackManifest.Platform.Provides, platformProvides)

cmd/hub/lifecycle/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ func addLockedParameter2(params []parameters.LockedParameter, name, env, value s
639639
}
640640

641641
func addHubProvides(params []parameters.LockedParameter, provides map[string][]string) []parameters.LockedParameter {
642-
return addLockedParameter2(params, "hub.provides", "HUB_PROVIDES", strings.Join(util.SortedKeys2(provides), " "))
642+
return addLockedParameter2(params, "hub.provides", "HUB_PROVIDES", strings.Join(util.SortedKeys(provides), " "))
643643
}
644644

645645
func maybeTestVerb(verb string, test bool) string {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package lifecycle
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"reflect"
7+
"regexp"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
// Converts the string into kubernetes acceptable name
13+
// which consist of kebab lower case with alphanumeric characters.
14+
// '.' is not allowed
15+
//
16+
// Arguments:
17+
//
18+
// First argument is a text to convert
19+
// Second optional argument is a size of the name (default is 63)
20+
// Third optional argument is a delimiter (default is '-')
21+
func formatSubdomain(args ...interface{}) (string, error) {
22+
if len(args) == 0 {
23+
return "", errors.New("hostname expects at least one argument")
24+
}
25+
arg0 := reflect.ValueOf(args[0])
26+
if arg0.Kind() != reflect.String {
27+
return "", errors.New("hostname expects string as first argument")
28+
}
29+
text := strings.TrimSpace(arg0.String())
30+
if text == "" {
31+
return "", nil
32+
}
33+
34+
size := 63
35+
if len(args) > 1 {
36+
arg1 := reflect.ValueOf(args[1])
37+
if arg1.Kind() == reflect.Int {
38+
size = int(reflect.ValueOf(args[1]).Int())
39+
} else if arg1.Kind() == reflect.String {
40+
size, _ = strconv.Atoi(arg1.String())
41+
} else {
42+
return "", fmt.Errorf("argument type %T not yet supported", args[1])
43+
}
44+
}
45+
46+
var del = "-"
47+
if len(args) > 2 {
48+
del = fmt.Sprintf("%v", args[2])
49+
}
50+
51+
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
52+
var matchNonAlphanumericEnd = regexp.MustCompile("[^a-zA-Z0-9]+$")
53+
var matchNonLetterStart = regexp.MustCompile("^[^a-zA-Z]+")
54+
var matchNonAnumericOrDash = regexp.MustCompile("[^a-zA-Z0-9-]+")
55+
var matchTwoOrMoreDashes = regexp.MustCompile("-{2,}")
56+
57+
text = matchNonLetterStart.ReplaceAllString(text, "")
58+
text = matchAllCap.ReplaceAllString(text, "${1}-${2}")
59+
text = matchNonAnumericOrDash.ReplaceAllString(text, "-")
60+
text = matchTwoOrMoreDashes.ReplaceAllString(text, "-")
61+
text = strings.ToLower(text)
62+
if len(text) > size {
63+
text = text[:size]
64+
}
65+
text = matchNonAlphanumericEnd.ReplaceAllString(text, "")
66+
if del != "-" {
67+
text = strings.ReplaceAll(text, "-", del)
68+
}
69+
return text, nil
70+
}
71+
72+
// Removes single or double or back quotes from the string
73+
func unquote(str string) (string, error) {
74+
result, err := strconv.Unquote(str)
75+
if err != nil && err.Error() == "invalid syntax" {
76+
return str, err
77+
}
78+
return result, err
79+
}
80+
81+
var hubGoTemplateFuncMap = map[string]interface{}{
82+
"formatSubdomain": formatSubdomain,
83+
"unquote": unquote,
84+
"uquote": unquote,
85+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) 2022 EPAM Systems, Inc.
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
package lifecycle
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestFormatSubdomain(t *testing.T) {
16+
result, _ := formatSubdomain("")
17+
assert.Equal(t, "", result)
18+
19+
result, _ = formatSubdomain("a")
20+
assert.Equal(t, "a", result)
21+
22+
result, _ = formatSubdomain("a b")
23+
assert.Equal(t, "a-b", result)
24+
25+
// dashes cannot repeat
26+
result, _ = formatSubdomain("A B c")
27+
assert.Equal(t, "a-b-c", result)
28+
29+
// cannot start and finish with dash
30+
result, _ = formatSubdomain("--a b c--")
31+
assert.Equal(t, "a-b-c", result)
32+
33+
// cannot start but can finish with digit
34+
result, _ = formatSubdomain("12a3 b c4")
35+
assert.Equal(t, "a3-b-c4", result)
36+
37+
// max length
38+
result, _ = formatSubdomain("a b c", 3)
39+
assert.Equal(t, "a-b", result)
40+
41+
// second param may be string
42+
result, _ = formatSubdomain("a b c", "3")
43+
assert.Equal(t, "a-b", result)
44+
45+
// max length and delimiter
46+
result, _ = formatSubdomain("a b c", 3, "_")
47+
assert.Equal(t, "a_b", result)
48+
}
49+
50+
func TestUnquote(t *testing.T) {
51+
result, _ := unquote("")
52+
assert.Equal(t, "", result)
53+
54+
result, _ = unquote("a")
55+
assert.Equal(t, "a", result)
56+
57+
result, _ = unquote("'a'")
58+
assert.Equal(t, "a", result)
59+
60+
result, _ = unquote("\"a\"")
61+
assert.Equal(t, "a", result)
62+
63+
result, _ = unquote("\"a")
64+
assert.Equal(t, "\"a", result)
65+
66+
result, _ = unquote("a\"")
67+
assert.Equal(t, "a\"", result)
68+
69+
result, err := unquote("'a")
70+
assert.Equal(t, "'a", result)
71+
assert.EqualError(t, err, "invalid syntax")
72+
73+
result, err = unquote("a'")
74+
assert.Equal(t, "a'", result)
75+
assert.EqualError(t, err, "invalid syntax")
76+
77+
result, _ = unquote("\"a'b\"")
78+
assert.Equal(t, "a'b", result)
79+
80+
_, err = unquote("'a\"b'")
81+
assert.EqualError(t, err, "invalid syntax")
82+
}

0 commit comments

Comments
 (0)