diff --git a/.github/workflows/build-publish.yml b/.github/workflows/build-publish.yml index 328c8cbe..23386f7a 100644 --- a/.github/workflows/build-publish.yml +++ b/.github/workflows/build-publish.yml @@ -78,7 +78,7 @@ jobs: - name: Build and push the image id: docker_build - uses: docker/build-push-action@v5 + uses: docker/build-push-action@v6 with: context: . build-args: DOCKER_GEN_VERSION=${{ steps.docker-gen_version.outputs.VERSION }} diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 059e9963..8f5751bf 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,7 +1,7 @@ ARG DOCKER_GEN_VERSION=main # Build docker-gen from scratch -FROM --platform=$BUILDPLATFORM golang:1.22.3-alpine as go-builder +FROM --platform=$BUILDPLATFORM golang:1.22.5-alpine as go-builder ENV CGO_ENABLED=0 @@ -24,7 +24,7 @@ RUN set -eux; \ go env | grep -E 'OS=|ARCH=|ARM=|AMD64='; \ go build -ldflags "-X main.buildVersion=${DOCKER_GEN_VERSION}" -o docker-gen ./cmd/docker-gen -FROM --platform=$TARGETPLATFORM alpine:3.20.0 +FROM --platform=$TARGETPLATFORM alpine:3.20.2 ARG DOCKER_GEN_VERSION ENV DOCKER_GEN_VERSION=${DOCKER_GEN_VERSION} \ diff --git a/Dockerfile.debian b/Dockerfile.debian index 721b8c41..9fabc40a 100644 --- a/Dockerfile.debian +++ b/Dockerfile.debian @@ -1,7 +1,7 @@ ARG DOCKER_GEN_VERSION=main # Build docker-gen from scratch -FROM --platform=$BUILDPLATFORM golang:1.22.3 as go-builder +FROM --platform=$BUILDPLATFORM golang:1.22.5 as go-builder ENV CGO_ENABLED=0 @@ -24,7 +24,7 @@ RUN set -eux; \ go env | grep -E 'OS=|ARCH=|ARM=|AMD64='; \ go build -ldflags "-X main.buildVersion=${DOCKER_GEN_VERSION}" -o docker-gen ./cmd/docker-gen -FROM --platform=$TARGETPLATFORM debian:12.5-slim +FROM --platform=$TARGETPLATFORM debian:12.6-slim ARG DOCKER_GEN_VERSION ENV DOCKER_GEN_VERSION=${DOCKER_GEN_VERSION} \ diff --git a/README.md b/README.md index db72a6c6..0dcd8296 100644 --- a/README.md +++ b/README.md @@ -398,23 +398,13 @@ For example, this is a JSON version of an emitted RuntimeContainer struct: - _`groupByLabelWithDefault $containers $label $defaultValue`_: Returns the same as `groupBy` but grouping by the given label's value. Containers that do not have the `$label` set are included in the map under the `$defaultValue` key. - _`include $file`_: Returns content of `$file`, and empty string if file reading error. - _`intersect $slice1 $slice2`_: Returns the strings that exist in both string slices. -- _`json $value`_: Returns the JSON representation of `$value` as a `string`. - _`fromYaml $string` / `mustFromYaml $string`_: Similar to [Sprig's `fromJson` / `mustFromJson`](https://github.com/Masterminds/sprig/blob/master/docs/defaults.md#fromjson-mustfromjson), but for YAML. - _`toYaml $dict` / `mustToYaml $dict`_: Similar to [Sprig's `toJson` / `mustToJson`](https://github.com/Masterminds/sprig/blob/master/docs/defaults.md#tojson-musttojson), but for YAML. - _`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. -- _`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) -- _`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) -- _`sha1 $string`_: Returns the hexadecimal representation of the SHA1 hash of `$string`. -- _`split $string $sep`_: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split) -- _`splitN $string $sep $count`_: Splits `$string` into a slice of substrings delimited by `$sep`, with number of substrings returned determined by `$count`. Alias for [`strings.SplitN`](https://golang.org/pkg/strings/#SplitN) - _`sortStringsAsc $strings`_: Returns a slice of strings `$strings` sorted in ascending order. - _`sortStringsDesc $strings`_: Returns a slice of strings `$strings` sorted in descending (reverse) order. - _`sortObjectsByKeysAsc $objects $fieldPath`_: Returns the array `$objects`, sorted in ascending order based on the values of a field path expression `$fieldPath`. - _`sortObjectsByKeysDesc $objects $fieldPath`_: Returns the array `$objects`, sorted in descending (reverse) order based on the values of a field path expression `$fieldPath`. -- _`trimPrefix $prefix $string`_: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged. -- _`trimSuffix $suffix $string`_: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged. -- _`toLower $string`_: Replace capital letters in `$string` to lowercase. -- _`toUpper $string`_: Replace lowercase letters in `$string` to uppercase. - _`when $condition $trueValue $falseValue`_: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise - _`where $items $fieldPath $value`_: Filters an array or slice 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. Returns an array of items having that value. - _`whereNot $items $fieldPath $value`_: Filters an array or slice 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. Returns an array of items **not** having that value. @@ -426,6 +416,30 @@ For example, this is a JSON version of an emitted RuntimeContainer struct: - _`whereLabelDoesNotExist $containers $label`_: Filters a slice of containers based on the non-existence of the label `$label`. - _`whereLabelValueMatches $containers $label $pattern`_: Filters a slice of containers based on the existence of the label `$label` with values matching the regular expression `$pattern`. +Sprig functions that have the same name as docker-gen function (but different behaviour) are made available with the `sprig` prefix: + +- _`sprigCoalesce ...`_: Alias for Sprig's [`coalesce`](https://masterminds.github.io/sprig/defaults.html). +- _`sprigContains $string $string`_: Alias for Sprig's [`contains`](https://masterminds.github.io/sprig/strings.html). +- _`sprigDir $path`_: Alias for Sprig's [`dir`](https://masterminds.github.io/sprig/paths.html). +- _`sprigReplace $old $new $string`_: Alias for Sprig's [`replace`](https://masterminds.github.io/sprig/strings.html). +- _`sprigSplit $sep $string`_: Alias for Sprig's [`split`](https://masterminds.github.io/sprig/string_slice.html). +- _`sprigSplitn $sep $count $string"`_: Alias for Sprig's [`splitn`](https://masterminds.github.io/sprig/string_slice.html). + +Some functions are aliases for Go's [`strings`](https://pkg.go.dev/strings) package functions: + +- _`parseBool $string`_: Alias for [`strconv.ParseBool`](http://golang.org/pkg/strconv/#ParseBool). Returns the boolean value represented by `$string`. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error. +- _`replace $string $old $new $count`_: Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace). Replaces up to `$count` occurences of `$old` with `$new` in `$string`. +- _`split $string $sep`_: Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split). Splits `$string` into a slice of substrings delimited by `$sep`. +- _`splitN $string $sep $count`_: Alias for [`strings.SplitN`](https://golang.org/pkg/strings/#SplitN). Splits `$string` into a slice of substrings delimited by `$sep`, with number of substrings returned determined by `$count`. +- _`toLower $string`_: Alias for [`strings.ToLower`](https://pkg.go.dev/strings#ToLower). Replace capital letters in `$string` to lowercase. +- _`toUpper $string`_: Alias for [`strings.ToUpper`](https://pkg.go.dev/strings#ToUpper). Replace lowercase letters in `$string` to uppercase. + +Those have been aliased to Sprig functions with the same behaviour as the original docker-gen function: + +- _`json $value`_: Alias for Sprig's [`mustToJson`](https://masterminds.github.io/sprig/defaults.html) +- _`parseJson $string`_: Alias for Sprig's [`mustFromJson`](https://masterminds.github.io/sprig/defaults.html). +- _`sha1 $string`_: Alias for Sprig's [`sha1sum`](https://masterminds.github.io/sprig/crypto.html). + --- ### Examples diff --git a/go.mod b/go.mod index 4999b5cb..79c492c5 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.0 require ( github.com/BurntSushi/toml v1.3.2 github.com/Masterminds/sprig/v3 v3.2.3 - github.com/fsouza/go-dockerclient v1.11.0 + github.com/fsouza/go-dockerclient v1.11.2 github.com/stretchr/testify v1.9.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -14,11 +14,11 @@ require ( github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Masterminds/goutils v1.1.1 // indirect github.com/Masterminds/semver/v3 v3.2.0 // indirect - github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/Microsoft/go-winio v0.6.2 // indirect github.com/containerd/containerd v1.6.26 // indirect github.com/containerd/log v0.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/docker/docker v25.0.5+incompatible // indirect + github.com/docker/docker v27.1.1+incompatible // indirect github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -30,6 +30,7 @@ require ( github.com/kr/text v0.2.0 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/reflectwalk v1.0.0 // indirect + github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/patternmatcher v0.6.0 // indirect github.com/moby/sys/sequential v0.5.0 // indirect github.com/moby/sys/user v0.1.0 // indirect @@ -44,8 +45,6 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/spf13/cast v1.3.1 // indirect golang.org/x/crypto v0.17.0 // indirect - golang.org/x/mod v0.8.0 // indirect - golang.org/x/sys v0.18.0 // indirect - golang.org/x/tools v0.6.0 // indirect + golang.org/x/sys v0.22.0 // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect ) diff --git a/go.sum b/go.sum index c6f09355..311949e2 100644 --- a/go.sum +++ b/go.sum @@ -10,10 +10,8 @@ github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7Y github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= -github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= -github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= -github.com/Microsoft/hcsshim v0.9.10 h1:TxXGNmcbQxBKVWvjvTocNb6jrPyeHlk5EiDhhgHgggs= -github.com/Microsoft/hcsshim v0.9.10/go.mod h1:7pLA8lDk46WKDWlVsENo92gC0XFa8rbKfyFRBqxEbCc= +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/containerd/containerd v1.6.26 h1:VVfrE6ZpyisvB1fzoY8Vkiq4sy+i5oF4uk7zu03RaHs= github.com/containerd/containerd v1.6.26/go.mod h1:I4TRdsdoo5MlKob5khDJS2EPT1l1oMNaE2MBm6FrwxM= github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= @@ -24,14 +22,14 @@ github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/docker/docker v25.0.5+incompatible h1:UmQydMduGkrD5nQde1mecF/YnSbTOaPeFIeP5C4W+DE= -github.com/docker/docker v25.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY= +github.com/docker/docker v27.1.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/fsouza/go-dockerclient v1.11.0 h1:4ZAk6W7rPAtPXm7198EFqA5S68rwnNQORxlOA5OurCA= -github.com/fsouza/go-dockerclient v1.11.0/go.mod h1:0I3TQCRseuPTzqlY4Y3ajfsg2VAdMQoazrkxJTiJg8s= +github.com/fsouza/go-dockerclient v1.11.2 h1:Wos4OMUwIjOW2rt8Z10TZSJHxgQH0KcYyf3O86dqFII= +github.com/fsouza/go-dockerclient v1.11.2/go.mod h1:HZN6ky2Mg5mfZO/WZBFDe6XCricqTnDJntfXHZTYnQQ= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -60,6 +58,8 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk= github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc= @@ -107,8 +107,6 @@ golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -121,8 +119,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -133,13 +129,13 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= -golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= -golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= +golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= @@ -150,8 +146,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/template/functions.go b/internal/template/functions.go index e828bf51..191846d9 100644 --- a/internal/template/functions.go +++ b/internal/template/functions.go @@ -1,11 +1,7 @@ package template import ( - "bytes" - "crypto/sha1" - "encoding/json" "fmt" - "io" "log" "os" "reflect" @@ -81,29 +77,6 @@ func contains(input interface{}, key interface{}) bool { return false } -func hashSha1(input string) string { - h := sha1.New() - io.WriteString(h, input) - return fmt.Sprintf("%x", h.Sum(nil)) -} - -func marshalJson(input interface{}) (string, error) { - var buf bytes.Buffer - enc := json.NewEncoder(&buf) - if err := enc.Encode(input); err != nil { - return "", err - } - return strings.TrimSuffix(buf.String(), "\n"), nil -} - -func unmarshalJson(input string) (interface{}, error) { - var v interface{} - if err := json.Unmarshal([]byte(input), &v); err != nil { - return nil, err - } - return v, nil -} - // arrayClosest find the longest matching substring in values // that matches input func arrayClosest(values []string, input string) string { @@ -140,26 +113,6 @@ func coalesce(input ...interface{}) interface{} { return nil } -// trimPrefix returns a string without the prefix, if present -func trimPrefix(prefix, s string) string { - return strings.TrimPrefix(s, prefix) -} - -// trimSuffix returns a string without the suffix, if present -func trimSuffix(suffix, s string) string { - return strings.TrimSuffix(s, suffix) -} - -// toLower return the string in lower case -func toLower(s string) string { - return strings.ToLower(s) -} - -// toUpper return the string in upper case -func toUpper(s string) string { - return strings.ToUpper(s) -} - // when returns the trueValue when the condition is true and the falseValue otherwise func when(condition bool, trueValue, falseValue interface{}) interface{} { if condition { diff --git a/internal/template/functions_test.go b/internal/template/functions_test.go index 79ca29e2..a3cd4cdc 100644 --- a/internal/template/functions_test.go +++ b/internal/template/functions_test.go @@ -1,14 +1,11 @@ package template import ( - "bytes" - "encoding/json" "os" "path" "reflect" "testing" - "github.com/nginx-proxy/docker-gen/internal/context" "github.com/stretchr/testify/assert" ) @@ -130,85 +127,6 @@ func TestSplitN(t *testing.T) { tests.run(t) } -func TestTrimPrefix(t *testing.T) { - const prefix = "tcp://" - const str = "tcp://127.0.0.1:2375" - const trimmed = "127.0.0.1:2375" - got := trimPrefix(prefix, str) - if got != trimmed { - t.Fatalf("expected trimPrefix(%s,%s) to be %s, got %s", prefix, str, trimmed, got) - } -} - -func TestTrimSuffix(t *testing.T) { - const suffix = ".local" - const str = "myhost.local" - const trimmed = "myhost" - got := trimSuffix(suffix, str) - if got != trimmed { - t.Fatalf("expected trimSuffix(%s,%s) to be %s, got %s", suffix, str, trimmed, got) - } -} - -func TestToLower(t *testing.T) { - const str = ".RaNd0m StrinG_" - const lowered = ".rand0m string_" - assert.Equal(t, lowered, toLower(str), "Unexpected value from toLower()") -} - -func TestToUpper(t *testing.T) { - const str = ".RaNd0m StrinG_" - const uppered = ".RAND0M STRING_" - assert.Equal(t, uppered, toUpper(str), "Unexpected value from toUpper()") -} - -func TestSha1(t *testing.T) { - sum := hashSha1("/path") - if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" { - t.Fatal("Incorrect SHA1 sum") - } -} - -func TestJson(t *testing.T) { - containers := []*context.RuntimeContainer{ - { - Env: map[string]string{ - "VIRTUAL_HOST": "demo1.localhost", - }, - ID: "1", - }, - { - Env: map[string]string{ - "VIRTUAL_HOST": "demo1.localhost,demo3.localhost", - }, - ID: "2", - }, - { - Env: map[string]string{ - "VIRTUAL_HOST": "demo2.localhost", - }, - ID: "3", - }, - } - output, err := marshalJson(containers) - if err != nil { - t.Fatal(err) - } - - buf := bytes.NewBufferString(output) - dec := json.NewDecoder(buf) - if err != nil { - t.Fatal(err) - } - var decoded []*context.RuntimeContainer - if err := dec.Decode(&decoded); err != nil { - t.Fatal(err) - } - if len(decoded) != len(containers) { - t.Fatalf("Incorrect unmarshaled container count. Expected %d, got %d.", len(containers), len(decoded)) - } -} - func TestParseJson(t *testing.T) { tests := templateTestList{ {`{{parseJson .}}`, `null`, ``}, diff --git a/internal/template/template.go b/internal/template/template.go index c7c0607d..c6dd8d5d 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -57,7 +57,10 @@ func newTemplate(name string) *template.Template { } return buf.String(), nil } - tmpl.Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{ + + sprigFuncMap := sprig.TxtFuncMap() + + tmpl.Funcs(sprigFuncMap).Funcs(template.FuncMap{ "closest": arrayClosest, "coalesce": coalesce, "comment": comment, @@ -71,29 +74,24 @@ func newTemplate(name string) *template.Template { "groupByMulti": groupByMulti, "groupByLabel": groupByLabel, "groupByLabelWithDefault": groupByLabelWithDefault, - "json": marshalJson, "include": include, "intersect": intersect, "keys": keys, "replace": strings.Replace, "parseBool": strconv.ParseBool, - "parseJson": unmarshalJson, "fromYaml": fromYaml, "toYaml": toYaml, "mustFromYaml": mustFromYaml, "mustToYaml": mustToYaml, "queryEscape": url.QueryEscape, - "sha1": hashSha1, "split": strings.Split, "splitN": strings.SplitN, "sortStringsAsc": sortStringsAsc, "sortStringsDesc": sortStringsDesc, "sortObjectsByKeysAsc": sortObjectsByKeysAsc, "sortObjectsByKeysDesc": sortObjectsByKeysDesc, - "trimPrefix": trimPrefix, - "trimSuffix": trimSuffix, - "toLower": toLower, - "toUpper": toUpper, + "toLower": strings.ToLower, + "toUpper": strings.ToUpper, "when": when, "where": where, "whereNot": whereNot, @@ -104,7 +102,21 @@ func newTemplate(name string) *template.Template { "whereLabelExists": whereLabelExists, "whereLabelDoesNotExist": whereLabelDoesNotExist, "whereLabelValueMatches": whereLabelValueMatches, + + // legacy docker-gen template function aliased to their Sprig clone + "json": sprigFuncMap["mustToJson"], + "parseJson": sprigFuncMap["mustFromJson"], + "sha1": sprigFuncMap["sha1sum"], + + // aliases to sprig template functions masked by docker-gen functions with the same name + "sprigCoalesce": sprigFuncMap["coalesce"], + "sprigContains": sprigFuncMap["contains"], + "sprigDir": sprigFuncMap["dir"], + "sprigReplace": sprigFuncMap["replace"], + "sprigSplit": sprigFuncMap["split"], + "sprigSplitn": sprigFuncMap["splitn"], }) + return tmpl }