Skip to content

Commit 1bedd5e

Browse files
committed
feat: support environment variables as template vars
1 parent c4ecf17 commit 1bedd5e

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

caskethttp/httpserver/replacer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,19 @@ func (r *replacer) getSubstitution(key string) string {
296296
name := key[2 : len(key)-1]
297297
return query.Get(name)
298298
}
299+
// next check for environment variable
300+
if key[1] == '$' {
301+
name := key[2 : len(key)-1]
302+
// check for a default value
303+
if i := strings.Index(name, "="); i != -1 {
304+
if value := os.Getenv(name[:i]); value != "" {
305+
return value
306+
}
307+
return name[i+1:]
308+
}
309+
310+
return os.Getenv(name)
311+
}
299312

300313
// search default replacements in the end
301314
switch key {

caskethttp/httpserver/replacer_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func TestReplace(t *testing.T) {
7575
// add some headers after creating replacer
7676
request.Header.Set("CustomAdd", "casket")
7777
request.Header.Set("Cookie", "foo=bar; taste=delicious")
78+
// add some environment variables
79+
os.Setenv("TEST_ENV", "test")
80+
os.Setenv("TEST_ENV_EMPTY", "")
7881

7982
// add some response headers
8083
recordRequest.Header().Set("Custom", "CustomResponseHeader")
@@ -108,6 +111,11 @@ func TestReplace(t *testing.T) {
108111
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
109112
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is casket."},
110113
{"The Custom response header is {<Custom}.", "The Custom response header is CustomResponseHeader."},
114+
{"The env variable TEST_ENV is {$TEST_ENV}.", "The env variable TEST_ENV is test."},
115+
{"The env variable TEST_ENV is {$TEST_ENV=default}, not default.", "The env variable TEST_ENV is test, not default."},
116+
{"The env variable TEST_ENV_EMPTY is {$TEST_ENV_EMPTY}.", "The env variable TEST_ENV_EMPTY is ."},
117+
{"The env variable TEST_ENV_EMPTY defaults to {$TEST_ENV_EMPTY=default}.", "The env variable TEST_ENV_EMPTY defaults to default."},
118+
{"The env variable NO_EXIST defaults to {$NO_EXIST=default}.", "The env variable NO_EXIST defaults to default."},
111119
{"Bad {>Custom placeholder", "Bad {>Custom placeholder"},
112120
{"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost.local\\r\\n" +
113121
"Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: casket\\r\\n" +

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ github.com/tmpim/casket-plugins v0.0.4-0.20210411234607-8b023eeb664d h1:vn0yn5M2
783783
github.com/tmpim/casket-plugins v0.0.4-0.20210411234607-8b023eeb664d/go.mod h1:RYBOo9WtK3LmyjGZVzEm1ptZV9uvPprzvz0V6i9qyuI=
784784
github.com/tmpim/certmagic v0.12.4 h1:k8MDMZT65fadsCu7fUONdksu5L7NONN6kgtX6dLd18w=
785785
github.com/tmpim/certmagic v0.12.4/go.mod h1:GrjIQ+gs5GW+jmqRkOnfoBjIiuk12VV4zbxoWBs8lQc=
786+
github.com/tmpim/dnsproviders v0.4.2/go.mod h1:QNqmtAefbbf/2BYM2hhVHaFglA584r3ayGps32Gu/c8=
786787
github.com/tmpim/dnsproviders v0.4.3-0.20211231213508-66e13a82678d h1:5IkgvDcbqfqrb635H58scO8qwiCy9Qpq7rp7zEq9VJw=
787788
github.com/tmpim/dnsproviders v0.4.3-0.20211231213508-66e13a82678d/go.mod h1:QNqmtAefbbf/2BYM2hhVHaFglA584r3ayGps32Gu/c8=
788789
github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY=

0 commit comments

Comments
 (0)