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" "log"
"os" "os"
"os/exec" "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{ var buildCmd = &Command{
Name: "build", Name: "build",
Usage: "", Usage: "",
Summary: "build the go server in the current directory", Summary: "build the go server in the current directory",
Help: `build extended help here...`, Help: buildUsage,
Run: buildRun, Run: buildRun,
} }
func buildRun(cmd *Command, args ...string) { 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)) if len(args) > 0 && args[0] == "help" {
fmt.Println(mycmd.Args) fmt.Print(cmd.Help)
fmt.Println(mycmd.Env) return
mycmd.Stderr = os.Stderr }
mycmd.Stdout = os.Stdout
err := mycmd.Run() 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }