github.com/docker-library/go-dockerlibrary@v0.0.0-20200821205225-669fbe5c1d52/pkg/templatelib/lib_example_test.go (about) 1 package templatelib_test 2 3 import ( 4 "os" 5 "text/template" 6 7 "github.com/docker-library/go-dockerlibrary/pkg/templatelib" 8 ) 9 10 func Example_prefixSuffix() { 11 tmpl, err := template.New("github-or-html").Funcs(templatelib.FuncMap).Parse(` 12 {{- . -}} 13 14 {{- if hasPrefix "https://github.com/" . -}} 15 {{- " " -}} GitHub 16 {{- end -}} 17 18 {{- if hasSuffix ".html" . -}} 19 {{- " " -}} HTML 20 {{- end -}} 21 22 {{- "\n" -}} 23 `) 24 if err != nil { 25 panic(err) 26 } 27 28 err = tmpl.Execute(os.Stdout, "https://github.com/example/example") 29 if err != nil { 30 panic(err) 31 } 32 33 err = tmpl.Execute(os.Stdout, "https://example.com/test.html") 34 if err != nil { 35 panic(err) 36 } 37 38 err = tmpl.Execute(os.Stdout, "https://example.com") 39 if err != nil { 40 panic(err) 41 } 42 43 err = tmpl.Execute(os.Stdout, "https://github.com/example/example/raw/master/test.html") 44 if err != nil { 45 panic(err) 46 } 47 48 // Output: 49 // https://github.com/example/example GitHub 50 // https://example.com/test.html HTML 51 // https://example.com 52 // https://github.com/example/example/raw/master/test.html GitHub HTML 53 } 54 55 func Example_ternary() { 56 tmpl, err := template.New("huge-if-true").Funcs(templatelib.FuncMap).Parse(` 57 {{- range $a := . -}} 58 {{ printf "%#v: %s\n" $a (ternary "HUGE" "not so huge" $a) }} 59 {{- end -}} 60 `) 61 62 err = tmpl.Execute(os.Stdout, []interface{}{ 63 true, 64 false, 65 "true", 66 "false", 67 "", 68 nil, 69 1, 70 0, 71 9001, 72 []bool{}, 73 []bool{false}, 74 }) 75 if err != nil { 76 panic(err) 77 } 78 79 // Output: 80 // true: HUGE 81 // false: not so huge 82 // "true": HUGE 83 // "false": HUGE 84 // "": not so huge 85 // <nil>: not so huge 86 // 1: HUGE 87 // 0: not so huge 88 // 9001: HUGE 89 // []bool{}: not so huge 90 // []bool{false}: HUGE 91 } 92 93 func Example_firstLast() { 94 tmpl, err := template.New("first-and-last").Funcs(templatelib.FuncMap).Parse(`First: {{ . | first }}, Last: {{ . | last }}`) 95 96 err = tmpl.Execute(os.Stdout, []interface{}{ 97 "a", 98 "b", 99 "c", 100 }) 101 if err != nil { 102 panic(err) 103 } 104 105 // Output: 106 // First: a, Last: c 107 } 108 109 func Example_json() { 110 tmpl, err := template.New("json").Funcs(templatelib.FuncMap).Parse(` 111 {{- json . -}} 112 `) 113 114 err = tmpl.Execute(os.Stdout, map[string]interface{}{ 115 "a": []string{"1", "2", "3"}, 116 "b": map[string]bool{"1": true, "2": false, "3": true}, 117 "c": nil, 118 }) 119 if err != nil { 120 panic(err) 121 } 122 123 // Output: 124 // {"a":["1","2","3"],"b":{"1":true,"2":false,"3":true},"c":null} 125 } 126 127 func Example_join() { 128 tmpl, err := template.New("join").Funcs(templatelib.FuncMap).Parse(` 129 Array: {{ . | join ", " }}{{ "\n" -}} 130 Args: {{ join ", " "a" "b" "c" -}} 131 `) 132 133 err = tmpl.Execute(os.Stdout, []string{ 134 "1", 135 "2", 136 "3", 137 }) 138 if err != nil { 139 panic(err) 140 } 141 142 // Output: 143 // Array: 1, 2, 3 144 // Args: a, b, c 145 } 146 147 func Example_trimReplaceGitToHttps() { 148 tmpl, err := template.New("git-to-https").Funcs(templatelib.FuncMap).Parse(` 149 {{- range . -}} 150 {{- . | replace "git://" "https://" | trimSuffixes ".git" }}{{ "\n" -}} 151 {{- end -}} 152 `) 153 154 err = tmpl.Execute(os.Stdout, []string{ 155 "git://github.com/jsmith/some-repo.git", 156 "https://github.com/jsmith/some-repo.git", 157 "https://github.com/jsmith/some-repo", 158 }) 159 if err != nil { 160 panic(err) 161 } 162 163 // Output: 164 // https://github.com/jsmith/some-repo 165 // https://github.com/jsmith/some-repo 166 // https://github.com/jsmith/some-repo 167 } 168 169 func Example_trimReplaceGitToGo() { 170 tmpl, err := template.New("git-to-go").Funcs(templatelib.FuncMap).Parse(` 171 {{- range . -}} 172 {{- . | trimPrefixes "git://" "http://" "https://" "ssh://" | trimSuffixes ".git" }}{{ "\n" -}} 173 {{- end -}} 174 `) 175 176 err = tmpl.Execute(os.Stdout, []string{ 177 "git://github.com/jsmith/some-repo.git", 178 "https://github.com/jsmith/some-repo.git", 179 "https://github.com/jsmith/some-repo", 180 "ssh://github.com/jsmith/some-repo.git", 181 "github.com/jsmith/some-repo", 182 }) 183 if err != nil { 184 panic(err) 185 } 186 187 // Output: 188 // github.com/jsmith/some-repo 189 // github.com/jsmith/some-repo 190 // github.com/jsmith/some-repo 191 // github.com/jsmith/some-repo 192 // github.com/jsmith/some-repo 193 } 194 195 func Example_getenv() { 196 tmpl, err := template.New("getenv").Funcs(templatelib.FuncMap).Parse(` 197 The FOO environment variable {{ getenv "FOO" "is set" "is not set" }}. {{- "\n" -}} 198 BAR: {{ getenv "BAR" "not set" }} {{- "\n" -}} 199 BAZ: {{ getenv "BAZ" "not set" }} {{- "\n" -}} 200 {{- $env := getenv "FOOBARBAZ" -}} 201 {{- if eq $env "" -}} 202 FOOBARBAZ {{- "\n" -}} 203 {{- end -}} 204 `) 205 206 os.Setenv("FOO", "") 207 os.Unsetenv("BAR") 208 os.Setenv("BAZ", "foobar") 209 os.Unsetenv("FOOBARBAZ") 210 211 err = tmpl.Execute(os.Stdout, nil) 212 if err != nil { 213 panic(err) 214 } 215 216 // Output: 217 // The FOO environment variable is not set. 218 // BAR: not set 219 // BAZ: foobar 220 // FOOBARBAZ 221 }