Last active
August 29, 2015 14:14
-
-
Save rbucker/2f362aeb0ddec24e8b8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2015, One Off Code | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"strings" | |
) | |
var ( | |
build bool | |
test bool | |
clean bool | |
) | |
func gopath(config string) string { | |
if b, err := ioutil.ReadFile(config); err == nil { | |
a := strings.Split(strings.TrimSpace(string(b)), "\n") | |
for i, v := range a { | |
a[i], err = filepath.Abs(v) | |
if err != nil { | |
log.Printf("ERROR %v\n", err) | |
return "" | |
} | |
} | |
return strings.Join(a, ":") | |
} | |
return "" | |
} | |
func getenv(varname, defval string) string { | |
if a := os.Getenv(varname); a != "" { | |
return a | |
} | |
return defval | |
} | |
func taskBuildHound() error { | |
log.Println("taskBuildHound") | |
names, err := filepath.Glob("src/hound/cmds/hound/*.go") | |
if err != nil { | |
return err | |
} | |
tgts := strings.Join(names, " ") | |
b, err := exec.Command("/bin/sh", "-c", `go build -o bin/hound -ldflags "-X main.defaultHost `+os.Getenv("HOST")+`" `+tgts).Output() | |
log.Printf("RUN: %v %v %v\n", "/bin/sh", "-c", `go build -o bin/hound -ldflags "-X main.defaultHost `+os.Getenv("HOST")+`" `+tgts) | |
log.Printf("OUT: %v\n", string(b)) | |
return err | |
} | |
func taskBuildHoundd() error { | |
log.Println("taskBuildHoundd") | |
names, err := filepath.Glob("src/hound/cmds/houndd/main.go") | |
if err != nil { | |
return err | |
} | |
tgts := strings.Join(names, " ") | |
b, err := exec.Command("/bin/sh", "-c", `go build -o bin/houndd -ldflags "-X main.defaultHost `+os.Getenv("HOST")+`" `+tgts).Output() | |
log.Printf("RUN: %v %v %v\n", "/bin/sh", "-c", `go build -o bin/houndd -ldflags "-X main.defaultHost `+os.Getenv("HOST")+`" `+tgts) | |
log.Printf("OUT: %v\n", string(b)) | |
return err | |
} | |
func taskTest() error { | |
log.Println("taskTest") | |
names, err := filepath.Glob("src/*/*_test.go") | |
if err != nil { | |
return err | |
} | |
for i, v := range names { | |
names[i] = filepath.Dir(v)[4:] | |
} | |
tests := strings.Join(names, " ") | |
b, err := exec.Command("/bin/sh", "-c", "go test "+tests).Output() | |
log.Printf("RUN: %v %v %v\n", "/bin/sh", "-c", "go test "+tests) | |
log.Printf("OUT: %v\n", string(b)) | |
return err | |
} | |
func taskClean() error { | |
log.Println("taskClean") | |
return os.RemoveAll("bin") | |
} | |
func main() { | |
var err error | |
flag.Parse() | |
os.Setenv("GOPATH", gopath(".gaan")) | |
os.Setenv("HOST", getenv("HOST", "localhost:6080")) | |
if build { | |
err = taskBuildHound() | |
if err != nil { | |
fmt.Println(err) | |
} | |
err = taskBuildHoundd() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
if test { | |
err = taskTest() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
if clean { | |
err = taskClean() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
} | |
func init() { | |
flag.BoolVar(&build, "build", false, "build the system") | |
flag.BoolVar(&test, "test", false, "test the system") | |
flag.BoolVar(&clean, "clean", false, "clean the system") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment