Tip: Testing the templates on the fly - go-easygen/easygen GitHub Wiki
echo 'Name: some-init-method' > /tmp/var.yaml
$ EASYGEN_TS='{{.Name}}' easygen /tmp/var
some-init-method
$ easygen -ts '{{clk2uc .Name}}' /tmp/var
SomeInitMethod
I.e., with the environment variable EASYGEN_TS
or the -ts
option on cli, the .tmpl
template file is not used.
Here are more examples:
$ EASYGEN_TS='{{.Name}}' easygen -ts '{{clk2uc .Name}}' /tmp/var
SomeInitMethod
# when both `EASYGEN_TS` environment variable and `-ts` option are provided, the latter takes precedence
$ easygen -ts '{{$n := sprintf .Name}} {{$n}}' /tmp/var.yaml
some-init-method
$ easygen -ts '{{$n := sprintf "%s, %.2f" (clk2uc .Name) 12.3456}} {{$n}}' /tmp/var.yaml
SomeInitMethod, 12.35
$ easygen -ts '{{substr (clk2uc .Name) 4}}' /tmp/var
InitMethod
$ easygen -ts '{{substr (clk2uc .Name) 8 3}}' /tmp/var
Met
$ easygen -ts '{{substr (clk2uc .Name) 4 -2}}' /tmp/var
InitMeth
$ easygen -ts '{{substr (clk2uc .Name) -6}}' /tmp/var
Method
$ easygen -ts '{{substr "abcde" -3}}' /tmp/var
cde
I.e., command line value takes the highest priority, even overriding the environment variable EASYGEN_TS
's value.
As such, if you have a different naming convention than using .tmpl
for template file and .yaml
for data file, you can override them in environment variables, EASYGEN_ET
and EASYGEN_EY
, so that you don't need to use -et
and/or -ey
to override them from command line each time.
echo 'Name: myConstantVariable' > /tmp/var.yml
$ EASYGEN_EY=.yml easygen -ts '{{clc2ss .Name}}' /tmp/var
MY_CONSTANT_VARIABLE
You can use easygen
as an generic Go template testing tool with the -ts
commandline option. For example,
echo "Age: 16" > /tmp/age.yaml
$ easygen -ts "{{.Age}}" /tmp/age
16
$ easygen -ts '{{$age := .Age}}{{/* comments allowed but no extra spaces allowed outside comments */}}{{$age}}' /tmp/age
16
$ easygen -ts '{{.Age}} {{ENV "SHELL"}}{{ENV "DISPLAY"}} ({{ENV "NONONO"}})' /tmp/age
16 /bin/bash:0 ()
$ easygen -ts '{{ if eq (ENV "SHELL") "/bin/bash" }}Shell is bash{{end}}; {{ if ne (ENV "DISPLAY") "" }}DISPLAY is set{{end}}' /tmp/age
Shell is bash; DISPLAY is set
$ easygen -ts '{{$underAge := lt .Age 16}}{{ if not $underAge }}toast{{else}}no drinking{{end}}' /tmp/age
toast
# see https://pkg.go.dev/text/template#hdr-Functions
$ easygen -ts '{{ if false }}F{{else if true}}in else if{{end}}' /tmp/age
in else if
$ easygen -ts '{{printf "%x" .Age}}' /tmp/age
10
$ easygen -ts '{{date "Y4"}}' /tmp/age
2017
$
$ easygen -ts '{{date "I"}}' /tmp/age
2017-07-10
$ easygen -ts '{{date "."}}' /tmp/age
2017.07.10
$ easygen -ts '{{date ""}}' /tmp/age
20170710
$ easygen -ts '{{timestamp}}' /tmp/age
2017-07-10T08:53:25-04:00
$ easygen -ts '{{timestamp "unix"}}' /tmp/age
1499691221
echo '{FirstName: John, LastName: Doe}' > /tmp/name.yaml
$ easygen -ts '{{ print "Concatenated: " .FirstName "." .LastName }}' /tmp/name.yaml
Concatenated: John.Doe
$ easygen -ts '{{.FirstName}}'\''s full name is {{printf "%s%s" .FirstName .LastName | len}} letters long.' /tmp/name
John's full name is 7 letters long.
$ easygen -ts "{{.FirstName}} {{clk2ss .LastName}}'s full name is "'{{len (printf "%s%s" .FirstName .LastName)}} letters long.' /tmp/name
John DOE's full name is 7 letters long.
$ easygen -ts '{{ $wanted := regexpFindString .FirstName `(?i)^john$` }} {{- if gt (len $wanted) 0 -}} {{.FirstName}} Found!{{end}}' /tmp/name
John Found!
$ easygen -ts '{{ $wanted := regexpFindString .FirstName `(?i)^john$` }} {{- if $wanted -}} {{.FirstName}} Found!{{end}}' /tmp/name
John Found!
echo 'Name: some-init-method' > /tmp/var.yaml
$ easygen -ts '{{.Name}} {{6 | minus1}} {{minus1 6}} {{clk2lc .Name}} {{clk2uc .Name}}' /tmp/var
some-init-method 5 5 someInitMethod SomeInitMethod
# More built-in function examples. See https://github.com/go-easygen/easygen/issues/25
$ easygen -ts '{{stringsContains .Name "init"}}' /tmp/var
true
$ easygen -ts '{{stringsContains .Name "foobar"}}' /tmp/var
false
$ easygen -ts '{{if (stringsContains .Name "init")}}"{{.Name}}" contains "init"{{else}} there {{end}}!' /tmp/var
"some-init-method" contains "init"!
$ easygen -ts '{{if (stringsContains .Name "init")}}"{{.Name}}" splits into: {{range (stringsSplit .Name "-")}}{{.}} {{end}}{{else}} there {{end}}!' /tmp/var
"some-init-method" splits into: some init method !
$ echo 'Name: "%bob'"'"'s file"' | tee /tmp/var.yaml
Name: "%bob's file"
$ easygen -ts '{{quote4shell .Name}}' /tmp/var
'%bob'\''s file'
You can make use any existing test files. E.g., under /path/to/go-easygen/easygen
:
$ easygen -ts '{{ regexpReplaceAllString .StrTest "(?i)th[eo]se" "the" }}' test/strings.yaml
the rights belong to the people
# No missing data
$ easygen -ts '{{ .StrTest }}' test/strings.yaml
These rights belong to those people
$ easygen -ts '{{ coalesce .StrTest "Something else" }}' test/strings.yaml
These rights belong to those people
$ easygen -ts '{{ coalesce .Colors }}' test/list0.yaml
[red blue white]
# Empty data
$ easygen -ts '"{{ .StrEmpty }}"' test/strings.yaml
""
$ easygen -ts '{{ coalesce .StrEmpty "Something else" }}' test/strings.yaml
Something else
# Missing data
$ easygen -ts '"{{ .StrNone }}"' test/strings.yaml
"<no value>"
$ easygen -ts '{{ coalesce .StrNone "Not exist" }}' test/strings.yaml
Not exist
$ easygen -ts '{{ coalesce .Description "No description" }}' test/strings.yaml
No description
$ env | grep DISPLAY
DISPLAY=:0
$ easygen -ts '{{ coalesce (ENV "DISPLAY") "0" }}{{ coalesce (ENV "NONONO") "-1" }}' /tmp/age
:0-1
Note,
- To fix
<no value>
in the output cause by the none-exist variables,
I had to use{{if .Var}}{{.Var}}{{else}}No Value{{end}}
before. E.g., see "fix<no value>
"
With the help ofcoalesce
, it is now much simpler.
eq
Returns the boolean truth of arg1 == arg2
ne
Returns the boolean truth of arg1 != arg2
lt
Returns the boolean truth of arg1 < arg2
le
Returns the boolean truth of arg1 <= arg2
gt
Returns the boolean truth of arg1 > arg2
ge
Returns the boolean truth of arg1 >= arg2