Documentation
¶
Overview ¶
Package templatelib implements a group of useful functions for use with the stdlib text/template package.
Usage:
tmpl, err := template.New("some-template").Funcs(templatelib.FuncMap).Parse("Hi, {{ join " " .Names }}")
Example (FirstLast) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("first-and-last").Funcs(templatelib.FuncMap).Parse(`First: {{ . | first }}, Last: {{ . | last }}`) err = tmpl.Execute(os.Stdout, []interface{}{ "a", "b", "c", }) if err != nil { panic(err) } }
Output: First: a, Last: c
Example (Getenv) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("getenv").Funcs(templatelib.FuncMap).Parse(` The FOO environment variable {{ getenv "FOO" "is set" "is not set" }}. {{- "\n" -}} BAR: {{ getenv "BAR" "not set" }} {{- "\n" -}} BAZ: {{ getenv "BAZ" "not set" }} {{- "\n" -}} {{- $env := getenv "FOOBARBAZ" -}} {{- if eq $env "" -}} FOOBARBAZ {{- "\n" -}} {{- end -}} `) os.Setenv("FOO", "") os.Unsetenv("BAR") os.Setenv("BAZ", "foobar") os.Unsetenv("FOOBARBAZ") err = tmpl.Execute(os.Stdout, nil) if err != nil { panic(err) } }
Output: The FOO environment variable is not set. BAR: not set BAZ: foobar FOOBARBAZ
Example (Join) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("join").Funcs(templatelib.FuncMap).Parse(` Array: {{ . | join ", " }}{{ "\n" -}} Args: {{ join ", " "a" "b" "c" -}} `) err = tmpl.Execute(os.Stdout, []string{ "1", "2", "3", }) if err != nil { panic(err) } }
Output: Array: 1, 2, 3 Args: a, b, c
Example (Json) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("json").Funcs(templatelib.FuncMap).Parse(` {{- json . -}} `) err = tmpl.Execute(os.Stdout, map[string]interface{}{ "a": []string{"1", "2", "3"}, "b": map[string]bool{"1": true, "2": false, "3": true}, "c": nil, }) if err != nil { panic(err) } }
Output: {"a":["1","2","3"],"b":{"1":true,"2":false,"3":true},"c":null}
Example (PrefixSuffix) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("github-or-html").Funcs(templatelib.FuncMap).Parse(` {{- . -}} {{- if hasPrefix "https://github.com/" . -}} {{- " " -}} GitHub {{- end -}} {{- if hasSuffix ".html" . -}} {{- " " -}} HTML {{- end -}} {{- "\n" -}} `) if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, "https://github.com/example/example") if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, "https://example.com/test.html") if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, "https://example.com") if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, "https://github.com/example/example/raw/master/test.html") if err != nil { panic(err) } }
Output: https://github.com/example/example GitHub https://example.com/test.html HTML https://example.com https://github.com/example/example/raw/master/test.html GitHub HTML
Example (Sha256sum) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("sha256sum").Funcs(templatelib.FuncMap).Parse(` {{- "Hello World!" | sha256sum -}} `) err = tmpl.Execute(os.Stdout, nil) if err != nil { panic(err) } }
Output: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
Example (Ternary) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("huge-if-true").Funcs(templatelib.FuncMap).Parse(` {{- range $a := . -}} {{ printf "%#v: %s\n" $a (ternary "HUGE" "not so huge" $a) }} {{- end -}} `) err = tmpl.Execute(os.Stdout, []interface{}{ true, false, "true", "false", "", nil, 1, 0, 9001, []bool{}, []bool{false}, }) if err != nil { panic(err) } }
Output: true: HUGE false: not so huge "true": HUGE "false": HUGE "": not so huge <nil>: not so huge 1: HUGE 0: not so huge 9001: HUGE []bool{}: not so huge []bool{false}: HUGE
Example (TrimReplaceGitToGo) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("git-to-go").Funcs(templatelib.FuncMap).Parse(` {{- range . -}} {{- . | trimPrefixes "git://" "http://" "https://" "ssh://" | trimSuffixes ".git" }}{{ "\n" -}} {{- end -}} `) err = tmpl.Execute(os.Stdout, []string{ "git://github.com/jsmith/some-repo.git", "https://github.com/jsmith/some-repo.git", "https://github.com/jsmith/some-repo", "ssh://github.com/jsmith/some-repo.git", "github.com/jsmith/some-repo", }) if err != nil { panic(err) } }
Output: github.com/jsmith/some-repo github.com/jsmith/some-repo github.com/jsmith/some-repo github.com/jsmith/some-repo github.com/jsmith/some-repo
Example (TrimReplaceGitToHttps) ¶
package main import ( "os" "text/template" "github.com/docker-library/bashbrew/pkg/templatelib" ) func main() { tmpl, err := template.New("git-to-https").Funcs(templatelib.FuncMap).Parse(` {{- range . -}} {{- . | replace "git://" "https://" | trimSuffixes ".git" }}{{ "\n" -}} {{- end -}} `) err = tmpl.Execute(os.Stdout, []string{ "git://github.com/jsmith/some-repo.git", "https://github.com/jsmith/some-repo.git", "https://github.com/jsmith/some-repo", }) if err != nil { panic(err) } }
Output: https://github.com/jsmith/some-repo https://github.com/jsmith/some-repo https://github.com/jsmith/some-repo
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var FuncMap = template.FuncMap{ "hasPrefix": swapStringsFuncBoolArgsOrder(strings.HasPrefix), "hasSuffix": swapStringsFuncBoolArgsOrder(strings.HasSuffix), "ternary": func(truthy interface{}, falsey interface{}, val interface{}) interface{} { if t, ok := template.IsTrue(val); !ok { panic(fmt.Sprintf(`template.IsTrue(%+v) says things are NOT OK`, val)) } else if t { return truthy } else { return falsey } }, "first": thingsActionFactory("first", true, func(args []interface{}, arg interface{}) interface{} { return arg }), "last": thingsActionFactory("last", false, func(args []interface{}, arg interface{}) interface{} { return arg }), "json": func(v interface{}) (string, error) { j, err := json.Marshal(v) return string(j), err }, "join": stringsActionFactory("join", true, strings.Join), "trimPrefixes": stringsActionFactory("trimPrefixes", false, stringsModifierActionFactory(strings.TrimPrefix)), "trimSuffixes": stringsActionFactory("trimSuffixes", false, stringsModifierActionFactory(strings.TrimSuffix)), "replace": stringsActionFactory("replace", false, func(strs []string, str string) string { return strings.NewReplacer(strs...).Replace(str) }), "getenv": thingsActionFactory("getenv", true, func(args []interface{}, arg interface{}) interface{} { var ( val = os.Getenv(arg.(string)) setVal interface{} = val unsetVal interface{} = "" ) if len(args) == 2 { setVal, unsetVal = args[0], args[1] } else if len(args) == 1 { unsetVal = args[0] } else if len(args) != 0 { panic(fmt.Sprintf(`expected between 1 and 3 arguments to "getenv", got %d`, len(args)+1)) } if val != "" { return setVal } else { return unsetVal } }), "sha256sum": func(input string) string { hash := sha256.Sum256([]byte(input)) return hex.EncodeToString(hash[:]) }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.