Skip to content

Commit 1078ec1

Browse files
TheRealSpaceShiparkadijs
authored andcommitted
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 a361136 commit 1078ec1

12 files changed

+458
-502
lines changed

cmd/hub/compose/elaborate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func Elaborate(manifestFilename string,
118118
// at least there will be a warning for mismatched values
119119
setValuesFromState(stackManifest.Parameters, st, useStateStackParameters)
120120
stackManifest.Requires = connectStateProvides(stackManifest.Requires, st.Provides)
121-
platformProvides = util.MergeUnique(platformProvides, util.SortedKeys2(st.Provides))
121+
platformProvides = util.MergeUnique(platformProvides, util.SortedKeys(st.Provides))
122122
}
123123
if len(platformProvides) > 0 {
124124
stackManifest.Platform.Provides = util.MergeUnique(stackManifest.Platform.Provides, platformProvides)

cmd/hub/lifecycle/deploy.go

+1-1
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 {
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package lifecycle
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"regexp"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
// Converts the string into kubernetes acceptable name
12+
// which consist of kebab lower case with alphanumeric characters.
13+
// '.' is not allowed
14+
//
15+
// Arguments:
16+
//
17+
// First argument is a text to convert
18+
// Second optional argument is a size of the name (default is 63)
19+
// Third optional argument is a delimiter (default is '-')
20+
func formatSubdomain(args ...interface{}) (string, error) {
21+
if len(args) == 0 {
22+
return "", fmt.Errorf("hostname expects at least one argument")
23+
}
24+
arg0 := reflect.ValueOf(args[0])
25+
if arg0.Kind() != reflect.String {
26+
return "", fmt.Errorf("hostname expects string as first argument")
27+
}
28+
text := strings.TrimSpace(arg0.String())
29+
if text == "" {
30+
return "", nil
31+
}
32+
33+
size := 63
34+
if len(args) > 1 {
35+
arg1 := reflect.ValueOf(args[1])
36+
if arg1.Kind() == reflect.Int {
37+
size = int(reflect.ValueOf(args[1]).Int())
38+
} else if arg1.Kind() == reflect.String {
39+
size, _ = strconv.Atoi(arg1.String())
40+
} else {
41+
return "", fmt.Errorf("Argument type %T not yet supported", args[1])
42+
}
43+
}
44+
45+
var del = "-"
46+
if len(args) > 2 {
47+
del = fmt.Sprintf("%v", args[2])
48+
}
49+
50+
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
51+
var matchNonAlphanumericEnd = regexp.MustCompile("[^a-zA-Z0-9]+$")
52+
var matchNonLetterStart = regexp.MustCompile("^[^a-zA-Z]+")
53+
var matchNonAnumericOrDash = regexp.MustCompile("[^a-zA-Z0-9-]+")
54+
var matchTwoOrMoreDashes = regexp.MustCompile("-{2,}")
55+
56+
text = matchNonLetterStart.ReplaceAllString(text, "")
57+
text = matchAllCap.ReplaceAllString(text, "${1}-${2}")
58+
text = matchNonAnumericOrDash.ReplaceAllString(text, "-")
59+
text = matchTwoOrMoreDashes.ReplaceAllString(text, "-")
60+
text = strings.ToLower(text)
61+
if len(text) > size {
62+
text = text[:size]
63+
}
64+
text = matchNonAlphanumericEnd.ReplaceAllString(text, "")
65+
if del != "-" {
66+
text = strings.ReplaceAll(text, "-", del)
67+
}
68+
return text, nil
69+
}
70+
71+
// Removes single or double or back quotes from the string
72+
func unquote(str string) (string, error) {
73+
result, err := strconv.Unquote(str)
74+
if err != nil && err.Error() == "invalid syntax" {
75+
return str, err
76+
}
77+
return result, err
78+
}
79+
80+
var hubGoTemplateFuncMap = map[string]interface{}{
81+
"formatSubdomain": formatSubdomain,
82+
"unquote": unquote,
83+
"uquote": unquote,
84+
}
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)