Skip to content

Commit

Permalink
feat: support environment variables as template vars
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachase committed Oct 8, 2022
1 parent c4ecf17 commit 1bedd5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions caskethttp/httpserver/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ func (r *replacer) getSubstitution(key string) string {
name := key[2 : len(key)-1]
return query.Get(name)
}
// next check for environment variable
if key[1] == '$' {
name := key[2 : len(key)-1]
// check for a default value
if i := strings.Index(name, "="); i != -1 {
if value := os.Getenv(name[:i]); value != "" {
return value
}
return name[i+1:]
}

return os.Getenv(name)
}

// search default replacements in the end
switch key {
Expand Down
8 changes: 8 additions & 0 deletions caskethttp/httpserver/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestReplace(t *testing.T) {
// add some headers after creating replacer
request.Header.Set("CustomAdd", "casket")
request.Header.Set("Cookie", "foo=bar; taste=delicious")
// add some environment variables
os.Setenv("TEST_ENV", "test")
os.Setenv("TEST_ENV_EMPTY", "")

// add some response headers
recordRequest.Header().Set("Custom", "CustomResponseHeader")
Expand Down Expand Up @@ -108,6 +111,11 @@ func TestReplace(t *testing.T) {
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is casket."},
{"The Custom response header is {<Custom}.", "The Custom response header is CustomResponseHeader."},
{"The env variable TEST_ENV is {$TEST_ENV}.", "The env variable TEST_ENV is test."},
{"The env variable TEST_ENV is {$TEST_ENV=default}, not default.", "The env variable TEST_ENV is test, not default."},
{"The env variable TEST_ENV_EMPTY is {$TEST_ENV_EMPTY}.", "The env variable TEST_ENV_EMPTY is ."},
{"The env variable TEST_ENV_EMPTY defaults to {$TEST_ENV_EMPTY=default}.", "The env variable TEST_ENV_EMPTY defaults to default."},
{"The env variable NO_EXIST defaults to {$NO_EXIST=default}.", "The env variable NO_EXIST defaults to default."},
{"Bad {>Custom placeholder", "Bad {>Custom placeholder"},
{"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost.local\\r\\n" +
"Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: casket\\r\\n" +
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ github.com/tmpim/casket-plugins v0.0.4-0.20210411234607-8b023eeb664d h1:vn0yn5M2
github.com/tmpim/casket-plugins v0.0.4-0.20210411234607-8b023eeb664d/go.mod h1:RYBOo9WtK3LmyjGZVzEm1ptZV9uvPprzvz0V6i9qyuI=
github.com/tmpim/certmagic v0.12.4 h1:k8MDMZT65fadsCu7fUONdksu5L7NONN6kgtX6dLd18w=
github.com/tmpim/certmagic v0.12.4/go.mod h1:GrjIQ+gs5GW+jmqRkOnfoBjIiuk12VV4zbxoWBs8lQc=
github.com/tmpim/dnsproviders v0.4.2/go.mod h1:QNqmtAefbbf/2BYM2hhVHaFglA584r3ayGps32Gu/c8=
github.com/tmpim/dnsproviders v0.4.3-0.20211231213508-66e13a82678d h1:5IkgvDcbqfqrb635H58scO8qwiCy9Qpq7rp7zEq9VJw=
github.com/tmpim/dnsproviders v0.4.3-0.20211231213508-66e13a82678d/go.mod h1:QNqmtAefbbf/2BYM2hhVHaFglA584r3ayGps32Gu/c8=
github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY=
Expand Down

0 comments on commit 1bedd5e

Please sign in to comment.