summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2016-01-23 21:16:13 -0500
committerDave Henderson <dhenderson@gmail.com>2016-01-23 21:16:13 -0500
commite027120266a82dcd7d37ec1d0d14873fa70742fe (patch)
tree6623e3f62dcd69d2151a6fea9e5cd9703fcb7b4a
parent6e8d894ec5de37591eee4e73bc06c74c0f3725b6 (diff)
💄 slight refactoring & adding some vague unit tests...
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--context.go20
-rw-r--r--context_test.go21
-rw-r--r--main.go28
3 files changed, 50 insertions, 19 deletions
diff --git a/context.go b/context.go
new file mode 100644
index 00000000..939883ea
--- /dev/null
+++ b/context.go
@@ -0,0 +1,20 @@
+package main
+
+import (
+ "os"
+ "strings"
+)
+
+// Context for templates
+type Context struct {
+}
+
+// Env - Map environment variables for use in a template
+func (c *Context) Env() map[string]string {
+ env := make(map[string]string)
+ for _, i := range os.Environ() {
+ sep := strings.Index(i, "=")
+ env[i[0:sep]] = i[sep+1:]
+ }
+ return env
+}
diff --git a/context_test.go b/context_test.go
new file mode 100644
index 00000000..4e339533
--- /dev/null
+++ b/context_test.go
@@ -0,0 +1,21 @@
+package main
+
+import (
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestEnvMapifiesEnvironment(t *testing.T) {
+ c := &Context{}
+ env := c.Env()
+ assert.Equal(t, env["USER"], os.Getenv("USER"))
+}
+
+func TestEnvGetsUpdatedEnvironment(t *testing.T) {
+ c := &Context{}
+ assert.Empty(t, c.Env()["FOO"])
+ os.Setenv("FOO", "foo")
+ assert.Equal(t, c.Env()["FOO"], "foo")
+}
diff --git a/main.go b/main.go
index 32dcbf36..b59d3513 100644
--- a/main.go
+++ b/main.go
@@ -4,9 +4,9 @@ import (
"bufio"
"flag"
"fmt"
+ "io"
"log"
"os"
- "strings"
"text/template"
)
@@ -23,31 +23,21 @@ func init() {
}
}
-// Context for templates
-type Context struct {
-}
-
-// Env - Map environment variables for use in a template
-func (c *Context) Env() map[string]string {
- env := make(map[string]string)
- for _, i := range os.Environ() {
- sep := strings.Index(i, "=")
- env[i[0:sep]] = i[sep+1:]
- }
- return env
-}
-
-func main() {
- s := bufio.NewScanner(os.Stdin)
+func RunTemplate(in io.Reader, out io.Writer) {
+ s := bufio.NewScanner(in)
for s.Scan() {
tmpl, err := template.New("template").Option("missingkey=error").Parse(s.Text())
if err != nil {
log.Fatalf("Line %q: %v\n", s.Text(), err)
}
- if err := tmpl.Execute(os.Stdout, &Context{}); err != nil {
+ if err := tmpl.Execute(out, &Context{}); err != nil {
panic(err)
}
- os.Stdout.Write([]byte("\n"))
+ out.Write([]byte("\n"))
}
}
+
+func main() {
+ RunTemplate(os.Stdin, os.Stdout)
+}