Skip to content

Instantly share code, notes, and snippets.

@disq
Created December 1, 2016 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disq/27aa2558afedafa8925861f959cdd86c to your computer and use it in GitHub Desktop.
Save disq/27aa2558afedafa8925861f959cdd86c to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
)
func mustRunGetResult(cmd string, arg ...string) string {
var buf bytes.Buffer
c := exec.Command(cmd)
c.Args = append(c.Args, arg...)
c.Stdout = &buf
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
log.Fatal(err)
return ""
}
return strings.Trim(buf.String(), "\n\r ")
}
func commandToConst(name, command string, args []string) string {
data := mustRunGetResult(command, args...)
ret := "\n// " + name + " is the output of \"" + command + " " + strings.Join(args, " ") + "\"\n"
ret += "const " + name + ` = "` + data + `"` + "\n"
return ret
}
const destinationFile = "version/version.go"
func main() {
summary := commandToConst("GitSummary", "git", strings.Split("describe --tags --dirty --always", " "))
branch := commandToConst("GitBranch", "git", strings.Split("symbolic-ref -q --short HEAD", " "))
timestamp := time.Now().Format(time.UnixDate)
b := bytes.NewBuffer(nil)
fmt.Fprint(b, `// This package is auto-generated using version/cmd/generate.go
package version
// AUTO-GENERATED. DO NOT EDIT
// `+timestamp+"\n"+summary+branch+"\n")
log.Printf("Writing %s...\n", destinationFile)
if err := ioutil.WriteFile(destinationFile, b.Bytes(), 0644); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment