Skip to content

Commit 22eb874

Browse files
rhansenbuchdag
authored andcommitted
feat: add template functions from sprig
1 parent ed4febb commit 22eb874

File tree

7 files changed

+74
-125
lines changed

7 files changed

+74
-125
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ e75a60548dc9 = 1 # a key can be either container name (nginx) or ID
206206

207207
### Templating
208208

209-
The templates used by docker-gen are written using the Go [text/template](http://golang.org/pkg/text/template/) language. In addition to the [built-in functions](http://golang.org/pkg/text/template/#hdr-Functions) supplied by Go, docker-gen provides a number of additional functions to make it simpler (or possible) to generate your desired output.
209+
The templates used by docker-gen are written using the Go [text/template](http://golang.org/pkg/text/template/) language. In addition to the [built-in functions](http://golang.org/pkg/text/template/#hdr-Functions) supplied by Go, docker-gen uses [sprig](https://masterminds.github.io/sprig/) and some additional functions to make it simpler (or possible) to generate your desired output.
210210

211211
#### Emit Structure
212212

@@ -354,23 +354,20 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
354354

355355
#### Functions
356356

357+
* [Functions from Go](https://pkg.go.dev/text/template#hdr-Functions)
358+
* [Functions from Sprig v3](https://masterminds.github.io/sprig/), except for those that have the same name as one of the following functions.
357359
* *`closest $array $value`*: Returns the longest matching substring in `$array` that matches `$value`
358360
* *`coalesce ...`*: Returns the first non-nil argument.
359361
* *`contains $map $key`*: Returns `true` if `$map` contains `$key`. Takes maps from `string` to any type.
360-
* *`dict $key $value ...`*: Creates a map from a list of pairs. Each `$key` value must be a `string`, but the `$value` can be any type (or `nil`). Useful for passing more than one value as a pipeline context to subtemplates.
361362
* *`dir $path`*: Returns an array of filenames in the specified `$path`.
362363
* *`exists $path`*: Returns `true` if `$path` refers to an existing file or directory. Takes a string.
363-
* *`first $array`*: Returns the first value of an array or nil if the arry is nil or empty.
364364
* *`groupBy $containers $fieldPath`*: Groups an array of `RuntimeContainer` instances based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value, which must be a string. Returns a map from the value of the field path expression to an array of containers having that value. Containers that do not have a value for the field path in question are omitted.
365365
* *`groupByKeys $containers $fieldPath`*: Returns the same as `groupBy` but only returns the keys of the map.
366366
* *`groupByMulti $containers $fieldPath $sep`*: Like `groupBy`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. A container whose `$fieldPath` value contains a list of strings will show up in the map output under each of those strings.
367367
* *`groupByLabel $containers $label`*: Returns the same as `groupBy` but grouping by the given label's value.
368-
* *`hasPrefix $prefix $string`*: Returns whether `$prefix` is a prefix of `$string`.
369-
* *`hasSuffix $suffix $string`*: Returns whether `$suffix` is a suffix of `$string`.
370368
* *`intersect $slice1 $slice2`*: Returns the strings that exist in both string slices.
371369
* *`json $value`*: Returns the JSON representation of `$value` as a `string`.
372370
* *`keys $map`*: Returns the keys from `$map`. If `$map` is `nil`, a `nil` is returned. If `$map` is not a `map`, an error will be thrown.
373-
* *`last $array`*: Returns the last value of an array.
374371
* *`parseBool $string`*: parseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error. Alias for [`strconv.ParseBool`](http://golang.org/pkg/strconv/#ParseBool)
375372
* *`replace $string $old $new $count`*: Replaces up to `$count` occurences of `$old` with `$new` in `$string`. Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace)
376373
* *`sha1 $string`*: Returns the hexadecimal representation of the SHA1 hash of `$string`.
@@ -382,7 +379,6 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
382379
* *`sortObjectsByKeysDesc $objects $fieldPath`: Returns the array `$objects`, sorted in descending (reverse) order based on the values of a field path expression `$fieldPath`.
383380
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
384381
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
385-
* *`trim $string`*: Removes whitespace from both sides of `$string`.
386382
* *`toLower $string`*: Replace capital letters in `$string` to lowercase.
387383
* *`toUpper $string`*: Replace lowercase letters in `$string` to uppercase.
388384
* *`when $condition $trueValue $falseValue`*: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ require (
1010

1111
require (
1212
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
13+
github.com/Masterminds/goutils v1.1.1 // indirect
14+
github.com/Masterminds/semver/v3 v3.1.1 // indirect
15+
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
1316
github.com/Microsoft/go-winio v0.5.2 // indirect
1417
github.com/Microsoft/hcsshim v0.9.2 // indirect
1518
github.com/containerd/cgroups v1.0.3 // indirect
@@ -20,7 +23,12 @@ require (
2023
github.com/docker/go-units v0.4.0 // indirect
2124
github.com/gogo/protobuf v1.3.2 // indirect
2225
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
26+
github.com/google/uuid v1.2.0 // indirect
2327
github.com/gorilla/mux v1.8.0 // indirect
28+
github.com/huandu/xstrings v1.3.1 // indirect
29+
github.com/imdario/mergo v0.3.12 // indirect
30+
github.com/mitchellh/copystructure v1.0.0 // indirect
31+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
2432
github.com/moby/sys/mount v0.2.0 // indirect
2533
github.com/moby/sys/mountinfo v0.5.0 // indirect
2634
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
@@ -30,8 +38,11 @@ require (
3038
github.com/opencontainers/runc v1.1.0 // indirect
3139
github.com/pkg/errors v0.9.1 // indirect
3240
github.com/pmezard/go-difflib v1.0.0 // indirect
41+
github.com/shopspring/decimal v1.2.0 // indirect
3342
github.com/sirupsen/logrus v1.8.1 // indirect
43+
github.com/spf13/cast v1.3.1 // indirect
3444
go.opencensus.io v0.23.0 // indirect
45+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
3546
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
3647
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
3748
)

go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
6262
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
6363
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
6464
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
65+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
66+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
67+
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
68+
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
69+
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
70+
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
6571
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
6672
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
6773
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw=
@@ -468,6 +474,7 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
468474
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
469475
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
470476
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
477+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
471478
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
472479
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
473480
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
@@ -516,12 +523,15 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
516523
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
517524
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
518525
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
526+
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
527+
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
519528
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
520529
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
521530
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
522531
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
523532
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
524533
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
534+
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
525535
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
526536
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
527537
github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9FCCAVRQ=
@@ -586,6 +596,8 @@ github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3N
586596
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
587597
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4=
588598
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
599+
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
600+
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
589601
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
590602
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
591603
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
@@ -594,6 +606,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
594606
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
595607
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
596608
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
609+
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
610+
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
597611
github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc=
598612
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
599613
github.com/moby/sys/mount v0.2.0 h1:WhCW5B355jtxndN5ovugJlMFJawbUODuW8fSnEH6SSM=
@@ -740,6 +754,8 @@ github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24
740754
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
741755
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
742756
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
757+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
758+
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
743759
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
744760
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
745761
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
@@ -759,6 +775,8 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO
759775
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
760776
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
761777
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
778+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
779+
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
762780
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
763781
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
764782
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
@@ -884,11 +902,13 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
884902
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
885903
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
886904
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
905+
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
887906
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
888907
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
889908
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
890909
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
891910
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
911+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
892912
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
893913
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
894914
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

internal/template/functions.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"crypto/sha1"
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"io"
109
"io/ioutil"
@@ -13,16 +12,6 @@ import (
1312
"strings"
1413
)
1514

16-
// hasPrefix returns whether a given string is a prefix of another string
17-
func hasPrefix(prefix, s string) bool {
18-
return strings.HasPrefix(s, prefix)
19-
}
20-
21-
// hasSuffix returns whether a given string is a suffix of another string
22-
func hasSuffix(suffix, s string) bool {
23-
return strings.HasSuffix(s, suffix)
24-
}
25-
2615
func keys(input interface{}) (interface{}, error) {
2716
if input == nil {
2817
return nil, nil
@@ -77,21 +66,6 @@ func contains(input interface{}, key interface{}) bool {
7766
return false
7867
}
7968

80-
func dict(values ...interface{}) (map[string]interface{}, error) {
81-
if len(values)%2 != 0 {
82-
return nil, errors.New("invalid dict call")
83-
}
84-
dict := make(map[string]interface{}, len(values)/2)
85-
for i := 0; i < len(values); i += 2 {
86-
key, ok := values[i].(string)
87-
if !ok {
88-
return nil, errors.New("dict keys must be strings")
89-
}
90-
dict[key] = values[i+1]
91-
}
92-
return dict, nil
93-
}
94-
9569
func hashSha1(input string) string {
9670
h := sha1.New()
9771
io.WriteString(h, input)
@@ -115,28 +89,6 @@ func unmarshalJson(input string) (interface{}, error) {
11589
return v, nil
11690
}
11791

118-
// arrayFirst returns first item in the array or nil if the
119-
// input is nil or empty
120-
func arrayFirst(input interface{}) interface{} {
121-
if input == nil {
122-
return nil
123-
}
124-
125-
arr := reflect.ValueOf(input)
126-
127-
if arr.Len() == 0 {
128-
return nil
129-
}
130-
131-
return arr.Index(0).Interface()
132-
}
133-
134-
// arrayLast returns last item in the array
135-
func arrayLast(input interface{}) interface{} {
136-
arr := reflect.ValueOf(input)
137-
return arr.Index(arr.Len() - 1).Interface()
138-
}
139-
14092
// arrayClosest find the longest matching substring in values
14193
// that matches input
14294
func arrayClosest(values []string, input string) string {
@@ -183,11 +135,6 @@ func trimSuffix(suffix, s string) string {
183135
return strings.TrimSuffix(s, suffix)
184136
}
185137

186-
// trim returns the string without leading or trailing whitespace
187-
func trim(s string) string {
188-
return strings.TrimSpace(s)
189-
}
190-
191138
// toLower return the string in lower case
192139
func toLower(s string) string {
193140
return strings.ToLower(s)

internal/template/functions_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,6 @@ func TestIntersect(t *testing.T) {
9292
assert.Len(t, i, 2, "Expected exactly two matches")
9393
}
9494

95-
func TestHasPrefix(t *testing.T) {
96-
const prefix = "tcp://"
97-
const str = "tcp://127.0.0.1:2375"
98-
if !hasPrefix(prefix, str) {
99-
t.Fatalf("expected %s to have prefix %s", str, prefix)
100-
}
101-
}
102-
103-
func TestHasSuffix(t *testing.T) {
104-
const suffix = ".local"
105-
const str = "myhost.local"
106-
if !hasSuffix(suffix, str) {
107-
t.Fatalf("expected %s to have suffix %s", str, suffix)
108-
}
109-
}
110-
11195
func TestSplitN(t *testing.T) {
11296
tests := templateTestList{
11397
{`{{index (splitN . "/" 2) 0}}`, "example.com/path", `example.com`},
@@ -139,15 +123,6 @@ func TestTrimSuffix(t *testing.T) {
139123
}
140124
}
141125

142-
func TestTrim(t *testing.T) {
143-
const str = " myhost.local "
144-
const trimmed = "myhost.local"
145-
got := trim(str)
146-
if got != trimmed {
147-
t.Fatalf("expected trim(%s) to be %s, got %s", str, trimmed, got)
148-
}
149-
}
150-
151126
func TestToLower(t *testing.T) {
152127
const str = ".RaNd0m StrinG_"
153128
const lowered = ".rand0m string_"
@@ -160,39 +135,6 @@ func TestToUpper(t *testing.T) {
160135
assert.Equal(t, uppered, toUpper(str), "Unexpected value from toUpper()")
161136
}
162137

163-
func TestDict(t *testing.T) {
164-
containers := []*context.RuntimeContainer{
165-
{
166-
Env: map[string]string{
167-
"VIRTUAL_HOST": "demo1.localhost",
168-
},
169-
ID: "1",
170-
},
171-
{
172-
Env: map[string]string{
173-
"VIRTUAL_HOST": "demo1.localhost,demo3.localhost",
174-
},
175-
ID: "2",
176-
},
177-
{
178-
Env: map[string]string{
179-
"VIRTUAL_HOST": "demo2.localhost",
180-
},
181-
ID: "3",
182-
},
183-
}
184-
d, err := dict("/", containers)
185-
if err != nil {
186-
t.Fatal(err)
187-
}
188-
if d["/"] == nil {
189-
t.Fatalf("did not find containers in dict: %s", d)
190-
}
191-
if d["MISSING"] != nil {
192-
t.Fail()
193-
}
194-
}
195-
196138
func TestSha1(t *testing.T) {
197139
sum := hashSha1("/path")
198140
if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" {

0 commit comments

Comments
 (0)