57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
const warning = "//WARNING! DO NOT EDIT THIS FILE BY HAND. IT IS AUTO GENERATED.\n"
|
|
|
|
const header = "package main\nconst gitVersion = \""
|
|
|
|
const code = `
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
func init() {
|
|
pflag.ErrHelp = nil
|
|
versionFlagSet := pflag.NewFlagSet("version", pflag.ContinueOnError)
|
|
versionFlagSet.ParseErrorsWhitelist.UnknownFlags = true
|
|
versionFlagSet.SetOutput(nil)
|
|
|
|
versionIsAsked := versionFlagSet.BoolP("version", "v", false, "Prints version of this application.")
|
|
versionFlagSet.BoolP("help", "h", false, "") // This line is needed to supress (redefine) built-in help flag.
|
|
|
|
err := versionFlagSet.Parse(os.Args)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *versionIsAsked {
|
|
fmt.Println(gitVersion)
|
|
os.Exit(0)
|
|
}
|
|
}
|
|
|
|
`
|
|
|
|
func main() {
|
|
cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(0)
|
|
}
|
|
version := string(out)
|
|
version = version[:len(version)-1]
|
|
content := warning + header + version + "\"" + code
|
|
|
|
ioutil.WriteFile("version.go", []byte(content), 0644)
|
|
}
|