Skip to content

Commit

Permalink
Merge pull request #1 from albertrdixon/feature/multi-os
Browse files Browse the repository at this point in the history
Feature/multi os
  • Loading branch information
albertrdixon committed Mar 10, 2015
2 parents aad1ca1 + 28b7209 commit 6c1e831
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 71 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
os: linux
language: go
go: 1.4.1

install: go get github.com/tools/godep
after_success: make build
script: make test-verbose
after_success: make package

deploy:
provider: releases
api_key:
secure: gE+O8G3DK0jhYc7lIIz9wlRLSZTXau7WdWz3dQqG/DAPvkJbjcGAjA3WmW81KcEs4Ob0ODIdChSGJY5+px/w0aFTdAAP27NvoPRwute2VcLRevpjCglroqz3girj3KUVJmACtya4h/nJZ3Eo20/6/lTJTE6y6W9dVhbwx1DpKlM=
file: t2
file:
- tnator-linux-amd64.tar.gz
- tnator-darwin-amd64.tar.gz
on:
repo: albertrdixon/tmplnator
tags: true
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
PROJECT = github.com/albertrdixon/tmplnator
EXECUTABLE = "t2"
LDFLAGS = "-X $(PROJECT)/version.Build $$(git rev-parse --short HEAD) -s"
BINARY = "cmd/t2/t2.go"
TEST_COMMAND = TNATOR_DIR=$(shell pwd)/fixtures godep go test
PLATFORM = "$$(echo "$$(uname)" | tr '[A-Z]' '[a-z]')"
VERSION = "$$(./t2 -v)"

.PHONY: dep-save dep-restore test test-verbose test-integration vet lint build install clean

Expand Down Expand Up @@ -29,10 +32,14 @@ dep-restore:
godep restore

test:
$(TEST_COMMAND) ./...
@echo "==> Running all tests"
@echo ""
@$(TEST_COMMAND) ./...

test-verbose:
$(TEST_COMMAND) -test.v ./...
@echo "==> Running all tests (verbose output)"
@ echo ""
@$(TEST_COMMAND) -test.v ./...

test-integration:
$(TEST_COMMAND) ./... -tags integration
Expand All @@ -44,10 +51,16 @@ lint:
golint ./...

build:
godep go build -ldflags $(LDFLAGS) $(BINARY)
@echo "==> Building $(EXECUTABLE) with ldflags '$(LDFLAGS)'"
@godep go build -ldflags $(LDFLAGS) $(BINARY)

install:
godep go install -ldflags $(LDFLAGS) $(BINARIES)
@echo "==> Installing $(EXECUTABLE) with ldflags $(LDFLAGS)"
@godep go install -ldflags $(LDFLAGS) $(BINARIES)

package: build
@echo "==> Tar'ing up the binary"
@test -f t2 && tar czf tnator-$(PLATFORM)-amd64-$(shell ./t2 -version).tar.gz t2

clean:
go clean ./...
Expand Down
130 changes: 65 additions & 65 deletions cmd/t2/t2.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
package main

import (
"flag"
"fmt"
"github.com/albertrdixon/tmplnator/template"
"github.com/albertrdixon/tmplnator/version"
"io/ioutil"
"os"
"sync"
"flag"
"fmt"
"github.com/albertrdixon/tmplnator/template"
"github.com/albertrdixon/tmplnator/version"
"io/ioutil"
"os"
"sync"
)

func exitErr(err error) {
fmt.Printf("There's been a problem: %v", err)
os.Exit(1)
fmt.Printf("There's been a problem: %v", err)
os.Exit(1)
}

func main() {
var (
V = flag.Bool("version", false, "Print version")
src = flag.String("td", "", "template dir to render")
del = flag.Bool("d", false, "Remove templates after processing")
pre = flag.String("P", "", "Dir prefix")
threads = flag.Int("T", 4, "Render threads")
)
var (
V = flag.Bool("v", false, "Print version")
src = flag.String("td", "", "template dir to render")
del = flag.Bool("d", false, "Remove templates after processing")
pre = flag.String("P", "", "Dir prefix")
threads = flag.Int("T", 4, "Render threads")
)

flag.Parse()
if *V {
fmt.Printf("Version: %s\n", version.RuntimeVersion(version.CodeVersion, version.Build))
os.Exit(0)
}
flag.Parse()
if *V {
fmt.Printf("%s\n", version.RuntimeVersion(version.CodeVersion, version.Build))
os.Exit(0)
}

if *src == "" {
fmt.Printf("Sorry, you need to provide the src dir.\n")
os.Exit(1)
}
if *threads < 1 {
*threads = 1
}
if *src == "" {
fmt.Printf("Sorry, you need to provide the src dir.\n")
os.Exit(1)
}
if *threads < 1 {
*threads = 1
}

s, p := *src, *pre
var err error
s, p := *src, *pre
var err error

if p == "tmp" {
if p, err = ioutil.TempDir("", "tnator"); err != nil {
fmt.Printf("Couldn't get tmp dir.")
exitErr(err)
}
fmt.Println(pre)
}
if p == "tmp" {
if p, err = ioutil.TempDir("", "tnator"); err != nil {
fmt.Printf("Couldn't get tmp dir.")
exitErr(err)
}
fmt.Println(pre)
}

fmt.Printf("==> Parsing Templates in %q\n", s)
tStack, err := template.ParseDirectory(s, p)
if err != nil {
fmt.Printf("Failed to parse templates.")
exitErr(err)
}
fmt.Printf("==> Parsing Templates in %q\n", s)
tStack, err := template.ParseDirectory(s, p)
if err != nil {
fmt.Printf("Failed to parse templates.")
exitErr(err)
}

var wg sync.WaitGroup
wg.Add(*threads)
for i := 0; i < *threads; i++ {
go func() {
defer wg.Done()
for tStack.Len() > 0 {
if t, ok := tStack.Pop().(*template.Template); ok {
if err := t.Write(); err == nil {
if *del {
os.Remove(t.Src)
}
} else {
fmt.Printf("Error writing template: %v", err)
}
} else {
fmt.Printf("Could not cast stack item as template: %v", t)
}
}
}()
}
var wg sync.WaitGroup
wg.Add(*threads)
for i := 0; i < *threads; i++ {
go func() {
defer wg.Done()
for tStack.Len() > 0 {
if t, ok := tStack.Pop().(*template.Template); ok {
if err := t.Write(); err == nil {
if *del {
os.Remove(t.Src)
}
} else {
fmt.Printf("Error writing template: %v", err)
}
} else {
fmt.Printf("Could not cast stack item as template: %v", t)
}
}
}()
}

wg.Wait()
os.Exit(0)
wg.Wait()
os.Exit(0)
}

0 comments on commit 6c1e831

Please sign in to comment.