warren build populates variables

This commit is contained in:
Dan Ballard 2014-12-09 22:43:10 -08:00
parent 4d9d215c43
commit 07a08f1795
1 changed files with 30 additions and 7 deletions

View File

@ -5,23 +5,46 @@ import (
"log"
"os"
"os/exec"
"strings"
"time"
)
var buildUsage = `
warren build - Runs go build and populates the following variables:
Build.GitBranch
Build.GitHash
Build.Date
`
var buildCmd = &Command{
Name: "build",
Usage: "",
Summary: "build the go server in the current directory",
Help: `build extended help here...`,
Help: buildUsage,
Run: buildRun,
}
func buildRun(cmd *Command, args ...string) {
mycmd := exec.Command("go", "build", "-ldflags", fmt.Sprintf("-X main.Build.Date %s -X main.Build.GitHash %s -X main.Build.Num %d\"", "2014-12-08", "a1b2c3d4", 107))
fmt.Println(mycmd.Args)
fmt.Println(mycmd.Env)
mycmd.Stderr = os.Stderr
mycmd.Stdout = os.Stdout
err := mycmd.Run()
if len(args) > 0 && args[0] == "help" {
fmt.Print(cmd.Help)
return
}
githash, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
log.Fatal(err)
}
gitbranchb, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output()
if err != nil {
log.Fatal(err)
}
gitbranch := strings.TrimSpace(string(gitbranchb))
date := time.Now().Format(time.RFC3339)
gobuild := exec.Command("go", "build", "-ldflags", fmt.Sprintf("-X main.Build.Date %s -X main.Build.GitHash %s -X main.Build.GitBranch %s\"", date, githash, gitbranch))
gobuild.Stderr = os.Stderr
gobuild.Stdout = os.Stdout
err = gobuild.Run()
if err != nil {
log.Fatal(err)
}