github.com/fern4lvarez/piladb@v0.2.0-alpha.20180407/pkg/version/version.go (about) 1 // Package version provides ways to get the version of the 2 // program. 3 package version 4 5 import ( 6 "os/exec" 7 "strings" 8 ) 9 10 // VERSION defines piladb version. 11 // It must be set when creating a new release, 12 // otherwise people building the project from 13 // source will get an inaccurate version. 14 const VERSION = "" 15 16 // Version returns piladb version given a v version. If v is empty, 17 // defaults to CommitHash. 18 func Version(v string) string { 19 if v == "" { 20 return CommitHash() 21 } 22 23 return v 24 } 25 26 // CommitHash returns the commit hash of the repository. 27 func CommitHash() string { 28 cmd := exec.Command("git", []string{"rev-parse", "HEAD"}...) 29 30 b, err := cmd.Output() 31 if err != nil { 32 return "undefined" 33 } 34 return strings.Replace(string(b), "\n", "", -1) 35 }