Skip to content

Commit 61e3cfd

Browse files
committed
refactor: replace docker-gen functions with sprig identical functions
(1/2)
1 parent 7266981 commit 61e3cfd

File tree

3 files changed

+18
-54
lines changed

3 files changed

+18
-54
lines changed

internal/template/functions.go

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package template
22

33
import (
4-
"bytes"
5-
"crypto/sha1"
6-
"encoding/json"
74
"fmt"
8-
"io"
95
"log"
106
"os"
117
"reflect"
128
"regexp"
139
"strings"
10+
11+
"github.com/Masterminds/sprig/v3"
1412
)
1513

14+
var sprigFuncMap = sprig.TxtFuncMap()
15+
1616
func keys(input interface{}) (interface{}, error) {
1717
if input == nil {
1818
return nil, nil
@@ -82,26 +82,15 @@ func contains(input interface{}, key interface{}) bool {
8282
}
8383

8484
func hashSha1(input string) string {
85-
h := sha1.New()
86-
io.WriteString(h, input)
87-
return fmt.Sprintf("%x", h.Sum(nil))
85+
return sprigFuncMap["sha1sum"].(func(string) string)(input)
8886
}
8987

9088
func marshalJson(input interface{}) (string, error) {
91-
var buf bytes.Buffer
92-
enc := json.NewEncoder(&buf)
93-
if err := enc.Encode(input); err != nil {
94-
return "", err
95-
}
96-
return strings.TrimSuffix(buf.String(), "\n"), nil
89+
return sprigFuncMap["mustToJson"].(func(interface{}) (string, error))(input)
9790
}
9891

9992
func unmarshalJson(input string) (interface{}, error) {
100-
var v interface{}
101-
if err := json.Unmarshal([]byte(input), &v); err != nil {
102-
return nil, err
103-
}
104-
return v, nil
93+
return sprigFuncMap["mustFromJson"].(func(string) (interface{}, error))(input)
10594
}
10695

10796
// arrayClosest find the longest matching substring in values
@@ -132,32 +121,17 @@ func dirList(path string) ([]string, error) {
132121

133122
// coalesce returns the first non nil argument
134123
func coalesce(input ...interface{}) interface{} {
135-
for _, v := range input {
136-
if v != nil {
137-
return v
138-
}
139-
}
140-
return nil
124+
return sprigFuncMap["coalesce"].(func(...interface{}) interface{})(input...)
141125
}
142126

143127
// trimPrefix returns a string without the prefix, if present
144128
func trimPrefix(prefix, s string) string {
145-
return strings.TrimPrefix(s, prefix)
129+
return sprigFuncMap["trimPrefix"].(func(string, string) string)(prefix, s)
146130
}
147131

148132
// trimSuffix returns a string without the suffix, if present
149133
func trimSuffix(suffix, s string) string {
150-
return strings.TrimSuffix(s, suffix)
151-
}
152-
153-
// toLower return the string in lower case
154-
func toLower(s string) string {
155-
return strings.ToLower(s)
156-
}
157-
158-
// toUpper return the string in upper case
159-
func toUpper(s string) string {
160-
return strings.ToUpper(s)
134+
return sprigFuncMap["trimSuffix"].(func(string, string) string)(suffix, s)
161135
}
162136

163137
// when returns the trueValue when the condition is true and the falseValue otherwise

internal/template/functions_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,6 @@ func TestTrimSuffix(t *testing.T) {
150150
}
151151
}
152152

153-
func TestToLower(t *testing.T) {
154-
const str = ".RaNd0m StrinG_"
155-
const lowered = ".rand0m string_"
156-
assert.Equal(t, lowered, toLower(str), "Unexpected value from toLower()")
157-
}
158-
159-
func TestToUpper(t *testing.T) {
160-
const str = ".RaNd0m StrinG_"
161-
const uppered = ".RAND0M STRING_"
162-
assert.Equal(t, uppered, toUpper(str), "Unexpected value from toUpper()")
163-
}
164-
165153
func TestSha1(t *testing.T) {
166154
sum := hashSha1("/path")
167155
if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" {

internal/template/template.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func newTemplate(name string) *template.Template {
5959
}
6060
tmpl.Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{
6161
"closest": arrayClosest,
62-
"coalesce": coalesce,
6362
"comment": comment,
6463
"contains": contains,
6564
"dir": dirList,
@@ -71,19 +70,16 @@ func newTemplate(name string) *template.Template {
7170
"groupByMulti": groupByMulti,
7271
"groupByLabel": groupByLabel,
7372
"groupByLabelWithDefault": groupByLabelWithDefault,
74-
"json": marshalJson,
7573
"include": include,
7674
"intersect": intersect,
7775
"keys": keys,
7876
"replace": strings.Replace,
7977
"parseBool": strconv.ParseBool,
80-
"parseJson": unmarshalJson,
8178
"fromYaml": fromYaml,
8279
"toYaml": toYaml,
8380
"mustFromYaml": mustFromYaml,
8481
"mustToYaml": mustToYaml,
8582
"queryEscape": url.QueryEscape,
86-
"sha1": hashSha1,
8783
"split": strings.Split,
8884
"splitN": strings.SplitN,
8985
"sortStringsAsc": sortStringsAsc,
@@ -92,8 +88,8 @@ func newTemplate(name string) *template.Template {
9288
"sortObjectsByKeysDesc": sortObjectsByKeysDesc,
9389
"trimPrefix": trimPrefix,
9490
"trimSuffix": trimSuffix,
95-
"toLower": toLower,
96-
"toUpper": toUpper,
91+
"toLower": strings.ToLower,
92+
"toUpper": strings.ToUpper,
9793
"when": when,
9894
"where": where,
9995
"whereNot": whereNot,
@@ -104,6 +100,12 @@ func newTemplate(name string) *template.Template {
104100
"whereLabelExists": whereLabelExists,
105101
"whereLabelDoesNotExist": whereLabelDoesNotExist,
106102
"whereLabelValueMatches": whereLabelValueMatches,
103+
}).Funcs(template.FuncMap{
104+
// docker-gen template function replaced by their Sprig clone
105+
"coalesce": coalesce,
106+
"json": marshalJson,
107+
"parseJson": unmarshalJson,
108+
"sha1": hashSha1,
107109
})
108110
return tmpl
109111
}

0 commit comments

Comments
 (0)