warren/cmd_build.go

52 lines
1.1 KiB
Go

package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)
var buildUsage = `
warren build - Runs go build and populates the following variables:
Build.GitBranch from git
Build.GitHash from git
Build.Date from system time
`
var buildCmd = &Command{
Name: "build",
Usage: "",
Summary: "build the go server in the current directory",
Help: buildUsage,
Run: buildRun,
}
func buildRun(cmd *Command, args ...string) {
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)
}
}