Skip to content

Commit 338283c

Browse files
committed
add feature include files into template, parsing several templates files for use of standard go 'template' function.
1 parent cfd2934 commit 338283c

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

README.md

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

209209
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

211+
For parsing several templates, split path with `;` (for example `template = "nginx.tmpl;header.tmpl"`). This makes possible to use go nested templates through standard `template` function.
212+
211213
#### Emit Structure
212214

213215
Within the templates, the object emitted by docker-gen will be a structure consisting of following Go structs:
@@ -365,6 +367,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
365367
* *`groupByKeys $containers $fieldPath`*: Returns the same as `groupBy` but only returns the keys of the map.
366368
* *`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.
367369
* *`groupByLabel $containers $label`*: Returns the same as `groupBy` but grouping by the given label's value.
370+
* *`include $file`*: Returns content of `$file`, and empty string if file reading error.
368371
* *`intersect $slice1 $slice2`*: Returns the strings that exist in both string slices.
369372
* *`json $value`*: Returns the JSON representation of `$value` as a `string`.
370373
* *`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.

internal/template/functions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"io/ioutil"
1010
"log"
11+
"os"
1112
"reflect"
1213
"strings"
1314
)
@@ -31,6 +32,14 @@ func keys(input interface{}) (interface{}, error) {
3132
return k, nil
3233
}
3334

35+
func include(file string) string {
36+
data, err := os.ReadFile(file)
37+
if err != nil {
38+
return ""
39+
}
40+
return string(data)
41+
}
42+
3443
func intersect(l1, l2 []string) []string {
3544
m := make(map[string]bool)
3645
m2 := make(map[string]bool)

internal/template/functions_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ func TestKeysNil(t *testing.T) {
7878
}
7979
}
8080

81+
func TestInclude(t *testing.T) {
82+
data := include("some_random_file")
83+
assert.Equal(t, "", data)
84+
85+
_ = os.WriteFile("/tmp/docker-gen-test-temp-file", []byte("some string"), 0o777)
86+
data = include("/tmp/docker-gen-test-temp-file")
87+
assert.Equal(t, "some string", data)
88+
_ = os.Remove("/tmp/docker-gen-test-temp-file")
89+
}
90+
8191
func TestIntersect(t *testing.T) {
8292
i := intersect([]string{"foo.fo.com", "bar.com"}, []string{"foo.bar.com"})
8393
assert.Len(t, i, 0, "Expected no match")

internal/template/template.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func newTemplate(name string) *template.Template {
5454
"groupByMulti": groupByMulti,
5555
"groupByLabel": groupByLabel,
5656
"json": marshalJson,
57+
"include": include,
5758
"intersect": intersect,
5859
"keys": keys,
5960
"replace": strings.Replace,
@@ -207,13 +208,14 @@ func GenerateFile(config config.Config, containers context.Context) bool {
207208
}
208209

209210
func executeTemplate(templatePath string, containers context.Context) []byte {
210-
tmpl, err := newTemplate(filepath.Base(templatePath)).ParseFiles(templatePath)
211+
templatePathList := strings.Split(templatePath, ";")
212+
tmpl, err := newTemplate(filepath.Base(templatePath)).ParseFiles(templatePathList...)
211213
if err != nil {
212214
log.Fatalf("Unable to parse template: %s", err)
213215
}
214216

215217
buf := new(bytes.Buffer)
216-
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePath), &containers)
218+
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePathList[0]), &containers)
217219
if err != nil {
218220
log.Fatalf("Template error: %s\n", err)
219221
}

0 commit comments

Comments
 (0)