Skip to content

Commit 669cf2f

Browse files
committed
Add trimPrefix(prefix,s) and trimSuffix(suffix,s)
1 parent a3b0172 commit 669cf2f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Within those templates, the object emitted by docker-gen will have [this structu
122122
* *`replace $string $old $new $count`*: Replaces up to `$count` occurences of `$old` with `$new` in `$string`. Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace)
123123
* *`sha1 $string`*: Returns the hexadecimal representation of the SHA1 hash of `$string`.
124124
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
125+
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
126+
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
125127

126128
===
127129

template.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ func coalesce(input ...interface{}) interface{} {
169169
return nil
170170
}
171171

172+
// trimPrefix returns whether a given string is a prefix of another string
173+
func trimPrefix(prefix, s string) string {
174+
return strings.TrimPrefix(s, prefix)
175+
}
176+
177+
// trimSuffix returns whether a given string is a suffix of another string
178+
func trimSuffix(suffix, s string) string {
179+
return strings.TrimSuffix(s, suffix)
180+
}
181+
172182
func generateFile(config Config, containers Context) bool {
173183
templatePath := config.Template
174184
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
@@ -189,6 +199,8 @@ func generateFile(config Config, containers Context) bool {
189199
"replace": strings.Replace,
190200
"sha1": hashSha1,
191201
"split": strings.Split,
202+
"trimPrefix": trimPrefix,
203+
"trimSuffix": trimSuffix,
192204
}).ParseFiles(templatePath)
193205
if err != nil {
194206
log.Fatalf("unable to parse template: %s", err)

template_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ func TestHasSuffix(t *testing.T) {
120120
}
121121
}
122122

123+
func TestTrimPrefix(t *testing.T) {
124+
const prefix = "tcp://"
125+
const str = "tcp://127.0.0.1:2375"
126+
const trimmed = "127.0.0.1:2375"
127+
got := trimPrefix(prefix, str)
128+
if got != trimmed {
129+
t.Fatalf("expected trimPrefix(%s,%s) to be %s, got %s", prefix, str, trimmed, got)
130+
}
131+
}
132+
133+
func TestTrimSuffix(t *testing.T) {
134+
const suffix = ".local"
135+
const str = "myhost.local"
136+
const trimmed = "myhost"
137+
got := trimSuffix(suffix, str)
138+
if got != trimmed {
139+
t.Fatalf("expected trimSuffix(%s,%s) to be %s, got %s", suffix, str, trimmed, got)
140+
}
141+
}
142+
123143
func TestDict(t *testing.T) {
124144
containers := []*RuntimeContainer{
125145
&RuntimeContainer{

0 commit comments

Comments
 (0)