-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathlib_example_test.go
221 lines (188 loc) · 4.53 KB
/
lib_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package templatelib_test
import (
"os"
"text/template"
"github.com/docker-library/bashbrew/pkg/templatelib"
)
func Example_prefixSuffix() {
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
}
func Example_ternary() {
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
}
func Example_firstLast() {
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
}
func Example_json() {
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}
}
func Example_join() {
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
}
func Example_trimReplaceGitToHttps() {
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
}
func Example_trimReplaceGitToGo() {
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
}
func Example_getenv() {
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
}