Skip to content

Commit

Permalink
Update version, remove fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
albertrdixon committed Mar 10, 2015
1 parent 6299c52 commit 3a896e2
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 216 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ lint:
golint ./...

build:
go fmt ./...
godep go build -ldflags $(LDFLAGS) $(BINARY)

install:
go fmt ./...
godep go install -ldflags $(LDFLAGS) $(BINARIES)

clean:
go clean ./...
rm -rf tnator
rm -rf t2
40 changes: 20 additions & 20 deletions stack/stack_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package stack

import (
"testing"
"testing"
)

var stacktests = []struct {
list []interface{}
list []interface{}
}{
{[]interface{}{"one", "two", "three"}},
{[]interface{}{1, 2, 3}},
{[]interface{}{"one", "two", "three"}},
{[]interface{}{1, 2, 3}},
}

func TestStack(t *testing.T) {
for _, st := range stacktests {
i, s := 0, NewStack()
for _, in := range st.list {
if s.Len() != i {
t.Errorf("stack.Len(): %d, want %d", s.Len(), i)
}
s.Push(in)
i++
}
for j := 2; j >= 0; j-- {
item := s.Pop()
if st.list[j] != item {
t.Errorf("stack.Pop(): %v, want %v", item, st.list[j])
}
}
}
for _, st := range stacktests {
i, s := 0, NewStack()
for _, in := range st.list {
if s.Len() != i {
t.Errorf("stack.Len(): %d, want %d", s.Len(), i)
}
s.Push(in)
i++
}
for j := 2; j >= 0; j-- {
item := s.Pop()
if st.list[j] != item {
t.Errorf("stack.Pop(): %v, want %v", item, st.list[j])
}
}
}
}
154 changes: 77 additions & 77 deletions template/template.go
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
package template

import (
"crypto/sha1"
"fmt"
"github.com/albertrdixon/tmplnator/stack"
"github.com/oxtoacart/bpool"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"crypto/sha1"
"fmt"
"github.com/albertrdixon/tmplnator/stack"
"github.com/oxtoacart/bpool"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)

type Template struct {
template *template.Template
Sha1 string
Vars map[string]string
Src string
template *template.Template
Sha1 string
Vars map[string]string
Src string
}

var (
files map[string]*File
varMap map[string]string
bp *bpool.BufferPool
files map[string]*File
varMap map[string]string
bp *bpool.BufferPool
)

func (t Template) Write() error {
b := bp.Get()
defer bp.Put(b)
b := bp.Get()
defer bp.Put(b)

err := t.template.Execute(b, t)
if err != nil {
return err
}
err := t.template.Execute(b, t)
if err != nil {
return err
}

file := files[t.Sha1]
fmt.Printf("==> Generating %q from %q\n", file.Destination(), t.Src)
if err := os.MkdirAll(file.DestinationDir(), 0755); err != nil {
return err
}
f, err := os.OpenFile(file.Destination(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(file.Mode))
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(b.Bytes())
if err != nil {
return err
}
return nil
file := files[t.Sha1]
fmt.Printf("==> Generating %q from %q\n", file.Destination(), t.Src)
if err := os.MkdirAll(file.DestinationDir(), 0755); err != nil {
return err
}
f, err := os.OpenFile(file.Destination(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(file.Mode))
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(b.Bytes())
if err != nil {
return err
}
return nil
}

func ParseDirectory(dir string, prefix string) (*stack.Stack, error) {
st := stack.NewStack()
return st, filepath.Walk(dir, walkfunc(st, prefix))
st := stack.NewStack()
return st, filepath.Walk(dir, walkfunc(st, prefix))
}

func walkfunc(tmplStack *stack.Stack, prefix string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if info.Mode().IsRegular() {
return ParseTemplate(path, prefix, tmplStack)
}
return nil
}
return func(path string, info os.FileInfo, err error) error {
if info.Mode().IsRegular() {
return ParseTemplate(path, prefix, tmplStack)
}
return nil
}
}

func ParseTemplate(path string, prefix string, st *stack.Stack) error {
base := filepath.Base(path)
t := new(Template)
t.Src = path
t.Vars = varMap
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
base := filepath.Base(path)
t := new(Template)
t.Src = path
t.Vars = varMap
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}

t.Sha1 = fmt.Sprintf("%x", sha1.New().Sum(contents))
template, err := newTemplate(path).Parse(string(contents))
if err != nil {
return err
}
t.Sha1 = fmt.Sprintf("%x", sha1.New().Sum(contents))
template, err := newTemplate(path).Parse(string(contents))
if err != nil {
return err
}

t.template = template
st.Push(t)
files[t.Sha1] = newFile(prefix, base)
return nil
t.template = template
st.Push(t)
files[t.Sha1] = newFile(prefix, base)
return nil
}

func newTemplate(path string) *template.Template {
return template.New(path).Funcs(newFuncMap())
return template.New(path).Funcs(newFuncMap())
}

func envMap() map[string]string {
env := make(map[string]string, len(os.Environ()))
for _, val := range os.Environ() {
index := strings.Index(val, "=")
env[val[:index]] = val[index+1:]
}
return env
env := make(map[string]string, len(os.Environ()))
for _, val := range os.Environ() {
index := strings.Index(val, "=")
env[val[:index]] = val[index+1:]
}
return env
}

func iface(list []string) []interface{} {
vals := make([]interface{}, len(list))
for i, v := range list {
vals[i] = v
}
return vals
vals := make([]interface{}, len(list))
for i, v := range list {
vals[i] = v
}
return vals
}

func init() {
bp = bpool.NewBufferPool(48)
varMap = envMap()
files = make(map[string]*File)
bp = bpool.NewBufferPool(48)
varMap = envMap()
files = make(map[string]*File)
}
Loading

0 comments on commit 3a896e2

Please sign in to comment.