From 45ac11088e9a0d34aa6dd275b372cf3723cb9170 Mon Sep 17 00:00:00 2001 From: Simon Lightfoot Date: Mon, 15 Aug 2016 18:15:58 +0100 Subject: [PATCH] Added support for environment variables to 'templates' module. (#1035) * * Added support for environment variables to 'templates' module. * Fixed flaw in test caused by environment variable ordering during testing on CI. * Updated some local variables to camel-case. * Reverted changes to replacer as environment variables are processed elsewhere. * Removed PrintEnv functionality in favour of documenting using template range. --- caddyhttp/httpserver/context.go | 21 ++++++++++++++++++++- caddyhttp/httpserver/context_test.go | 22 ++++++++++++++++++++++ caddyhttp/httpserver/replacer_test.go | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/caddyhttp/httpserver/context.go b/caddyhttp/httpserver/context.go index 51e01c95..0e5eb90b 100644 --- a/caddyhttp/httpserver/context.go +++ b/caddyhttp/httpserver/context.go @@ -13,6 +13,7 @@ import ( "time" "github.com/russross/blackfriday" + "os" ) // This file contains the context and functions available for @@ -57,6 +58,19 @@ func (c Context) Header(name string) string { return c.Req.Header.Get(name) } +// Env gets a map of the environment variables. +func (c Context) Env() map[string]string { + osEnv := os.Environ() + envVars := make(map[string]string, len(osEnv)) + for _, env := range osEnv { + data := strings.SplitN(env, "=", 2) + if len(data) == 2 { + envVars[data[0]] = data[1] + } + } + return envVars +} + // IP gets the (remote) IP address of the client making the request. func (c Context) IP() string { ip, _, err := net.SplitHostPort(c.Req.RemoteAddr) @@ -243,11 +257,16 @@ func (c Context) ToUpper(s string) string { return strings.ToUpper(s) } -// Split is a passthrough to strings.Split. It will split the first argument at each instance of the separator and return a slice of strings. +// Split is a pass-through to strings.Split. It will split the first argument at each instance of the separator and return a slice of strings. func (c Context) Split(s string, sep string) []string { return strings.Split(s, sep) } +// Join is a pass-through to strings.Join. It will join the first argument slice with the separator in the second argument and return the result. +func (c Context) Join(a []string, sep string) string { + return strings.Join(a, sep) +} + // Slice will convert the given arguments into a slice. func (c Context) Slice(elems ...interface{}) []interface{} { return elems diff --git a/caddyhttp/httpserver/context_test.go b/caddyhttp/httpserver/context_test.go index 332a649d..5405287e 100644 --- a/caddyhttp/httpserver/context_test.go +++ b/caddyhttp/httpserver/context_test.go @@ -224,6 +224,28 @@ func TestHeader(t *testing.T) { } } +func TestEnv(t *testing.T) { + context := getContextOrFail(t) + + name := "ENV_TEST_NAME" + testValue := "TEST_VALUE" + os.Setenv(name, testValue) + + notExisting := "ENV_TEST_NOT_EXISTING" + os.Unsetenv(notExisting) + + env := context.Env() + if value := env[name]; value != testValue { + t.Errorf("Expected env-variable %s value '%s', found '%s'", + name, testValue, value) + } + + if value, ok := env[notExisting]; ok { + t.Errorf("Expected empty env-variable %s, found '%s'", + notExisting, value) + } +} + func TestIP(t *testing.T) { context := getContextOrFail(t) diff --git a/caddyhttp/httpserver/replacer_test.go b/caddyhttp/httpserver/replacer_test.go index e07d2932..cfd52fb2 100644 --- a/caddyhttp/httpserver/replacer_test.go +++ b/caddyhttp/httpserver/replacer_test.go @@ -121,7 +121,7 @@ func TestSet(t *testing.T) { request, err := http.NewRequest("POST", "http://localhost", reader) if err != nil { - t.Fatalf("Request Formation Failed \n") + t.Fatalf("Request Formation Failed: %s\n", err.Error()) } repl := NewReplacer(request, recordRequest, "")