Skip to content

Commit 82bf72c

Browse files
committed
add feature include files into template, parsing several templates files for use of standard go 'template' function.
1 parent b429924 commit 82bf72c

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 provides a number of 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:
@@ -367,6 +369,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
367369
* *`groupByLabel $containers $label`*: Returns the same as `groupBy` but grouping by the given label's value.
368370
* *`hasPrefix $prefix $string`*: Returns whether `$prefix` is a prefix of `$string`.
369371
* *`hasSuffix $suffix $string`*: Returns whether `$suffix` is a suffix of `$string`.
372+
* *`include $file`*: Returns content of `$file`, and empty string if file reading error.
370373
* *`intersect $slice1 $slice2`*: Returns the strings that exist in both string slices.
371374
* *`json $value`*: Returns the JSON representation of `$value` as a `string`.
372375
* *`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
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"io/ioutil"
1111
"log"
12+
"os"
1213
"reflect"
1314
"strings"
1415
)
@@ -42,6 +43,14 @@ func keys(input interface{}) (interface{}, error) {
4243
return k, nil
4344
}
4445

46+
func include(file string) string {
47+
data, err := os.ReadFile(file)
48+
if err != nil {
49+
return ""
50+
}
51+
return string(data)
52+
}
53+
4554
func intersect(l1, l2 []string) []string {
4655
m := make(map[string]bool)
4756
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
@@ -57,6 +57,7 @@ func newTemplate(name string) *template.Template {
5757
"hasPrefix": hasPrefix,
5858
"hasSuffix": hasSuffix,
5959
"json": marshalJson,
60+
"include": include,
6061
"intersect": intersect,
6162
"keys": keys,
6263
"last": arrayLast,
@@ -212,13 +213,14 @@ func GenerateFile(config config.Config, containers context.Context) bool {
212213
}
213214

214215
func executeTemplate(templatePath string, containers context.Context) []byte {
215-
tmpl, err := newTemplate(filepath.Base(templatePath)).ParseFiles(templatePath)
216+
templatePathList := strings.Split(templatePath, ";")
217+
tmpl, err := newTemplate(filepath.Base(templatePath)).ParseFiles(templatePathList...)
216218
if err != nil {
217219
log.Fatalf("Unable to parse template: %s", err)
218220
}
219221

220222
buf := new(bytes.Buffer)
221-
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePath), &containers)
223+
err = tmpl.ExecuteTemplate(buf, filepath.Base(templatePathList[0]), &containers)
222224
if err != nil {
223225
log.Fatalf("Template error: %s\n", err)
224226
}

0 commit comments

Comments
 (0)