Commit 45ac1108 authored by Simon Lightfoot's avatar Simon Lightfoot Committed by Matt Holt

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.
parent eb3bbc40
......@@ -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
......
......@@ -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)
......
......@@ -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, "")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment