Skip to content

Commit 1db27a0

Browse files
Merge pull request #2 from marcelocarlos/allow-custom-delimiters
New option: `-d` to support custom delimiters
2 parents b7f4963 + 57405eb commit 1db27a0

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ echo "v1: {{ .key1 }}" | datasubst --json-data examples/basic-data.json
3434
# Using stdin - YAML
3535
echo "v3: {{ .key2.first.key3 }}" | datasubst --yaml-data examples/basic-data.yaml
3636
# Using stdin - env
37-
echo "example: {{ .VAR3 }}" | VAR3="v3" datasubst --env-data
37+
echo "{{ .TEST1 }} {{ .TEST2 }}" | TEST1="hello" TEST2="world" datasubst --env-data
38+
39+
# Using additional options, such -s (strict mode) and -d (change delimiters)
40+
echo "(( .TEST ))" | TEST="hi" datasubst --env-data -d '((:))' -s
3841
```
3942

4043
See [examples](./examples/) for more.

main.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Options:
2525
-i, --input INPUT Input template file in go template format.
2626
-o, --output OUTPUT Write the output to the file at OUTPUT.
2727
-s, --strict Strict mode (causes an error if a key is missing)
28+
-d, --delimiters Set the delimiters used in the templates in the format <left>:<right> (default: '{{:}}')
2829
--help Display this help and exit.
2930
--version Output version information and exit.
3031
@@ -33,7 +34,8 @@ INPUT defaults to standard input and OUTPUT defaults to standard output.
3334
Examples:
3435
$ datasubst --input examples/basic-input.txt --json-data examples/basic-data.json
3536
$ echo "v3: {{ .key2.first.key3 }}" | datasubst --yaml-data examples/basic-data.yaml
36-
$ TEST1="hello" TEST2="world" datasubst --input examples/basic-input-env.txt --env-data`
37+
$ echo "{{ .TEST1 }} {{ .TEST2 }}" | TEST1="hello" TEST2="world" datasubst --env-data
38+
$ echo "(( .TEST ))" | TEST="hi" datasubst --env-data -d '((:))'`
3739

3840
var Version string
3941

@@ -92,8 +94,8 @@ func main() {
9294
}
9395

9496
var (
95-
inputFile, outputFile, jsonDataFile, yamlDataFile string
96-
envFlag, strictFlag, helpFlag, versionFlag bool
97+
inputFile, outputFile, jsonDataFile, yamlDataFile, delimiters string
98+
envFlag, strictFlag, helpFlag, versionFlag bool
9799
)
98100

99101
flag.StringVar(&inputFile, "input", "", "input template file in go template format")
@@ -106,6 +108,8 @@ func main() {
106108
flag.StringVar(&outputFile, "o", "", "write the output to the file at OUTPUT")
107109
flag.StringVar(&yamlDataFile, "yaml-data", "", "input data source in YAML format")
108110
flag.StringVar(&yamlDataFile, "y", "", "input data source in YAML format")
111+
flag.StringVar(&delimiters, "delimiters", "", "Set the delimiters used in the templates in the format <left>:<right> (default: '{{:}}')")
112+
flag.StringVar(&delimiters, "d", "", "Set the delimiters used in the templates in the format <left>:<right> (default: '{{:}}')")
109113
flag.BoolVar(&strictFlag, "strict", false, "strict mode (causes an error if a key is missing)")
110114
flag.BoolVar(&strictFlag, "s", false, "strict mode (causes an error if a key is missing)")
111115
flag.BoolVar(&versionFlag, "version", false, "output version information and exit")
@@ -159,12 +163,18 @@ func main() {
159163
if err != nil {
160164
log.Fatalf("Error opening data file: %v\n", err)
161165
}
162-
// Parse Input
163-
missingkey := "default"
166+
tpl := template.New("template")
164167
if strictFlag {
165-
missingkey = "error"
168+
tpl.Option("missingkey=error")
166169
}
167-
tpl, err := template.New("template").Option(fmt.Sprintf("missingkey=%s", missingkey)).Parse(string(tplStr))
170+
if delimiters != "" {
171+
if strings.Count(delimiters, ":") != 1 || delimiters[len(delimiters)-1:] == ":" || delimiters[0:1] == ":" {
172+
log.Fatal("Error: invalid delimiter format. Must be '<left>:<right>' and ':'")
173+
}
174+
d := strings.Split(delimiters, ":")
175+
tpl.Delims(d[0], d[1])
176+
}
177+
tpl, err = tpl.Parse(string(tplStr))
168178
if err != nil {
169179
log.Fatalf("Error parsing template: %v\n", err)
170180
}

0 commit comments

Comments
 (0)