2020-10-12 20:42:55 +00:00
|
|
|
//go:generate go run -mod=vendor git.rootprojects.org/root/go-gitver/v2
|
2019-06-21 04:55:24 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
2019-07-01 08:32:19 +00:00
|
|
|
|
2020-10-12 20:42:55 +00:00
|
|
|
"git.rootprojects.org/root/go-gitver/v2/gitver"
|
2019-06-21 04:55:24 +00:00
|
|
|
)
|
|
|
|
|
2020-10-10 22:13:09 +00:00
|
|
|
var commit, version, date string
|
2019-06-21 04:55:24 +00:00
|
|
|
var exitCode int
|
2019-06-21 06:01:02 +00:00
|
|
|
var verFile = "xversion.go"
|
2019-06-21 04:55:24 +00:00
|
|
|
|
|
|
|
func main() {
|
2019-06-21 05:27:41 +00:00
|
|
|
pkg := "main"
|
|
|
|
|
2019-06-21 04:55:24 +00:00
|
|
|
args := os.Args[1:]
|
|
|
|
for i := range args {
|
|
|
|
arg := args[i]
|
|
|
|
if "-f" == arg || "--fail" == arg {
|
|
|
|
exitCode = 1
|
2019-06-21 06:01:02 +00:00
|
|
|
} else if ("--outfile" == arg || "-o" == arg) && len(args) > i+1 {
|
|
|
|
verFile = args[i+1]
|
|
|
|
args[i+1] = ""
|
2019-06-21 05:27:41 +00:00
|
|
|
} else if "--package" == arg && len(args) > i+1 {
|
|
|
|
pkg = args[i+1]
|
|
|
|
args[i+1] = ""
|
2019-06-21 04:55:24 +00:00
|
|
|
} else if "-V" == arg || "version" == arg || "-version" == arg || "--version" == arg {
|
2020-10-10 22:13:09 +00:00
|
|
|
fmt.Println(commit)
|
|
|
|
fmt.Println(version)
|
|
|
|
fmt.Println(date)
|
2019-06-21 04:55:24 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if "" != os.Getenv("GITVER_FAIL") && "false" != os.Getenv("GITVER_FAIL") {
|
|
|
|
exitCode = 1
|
|
|
|
}
|
|
|
|
|
2019-07-01 08:32:19 +00:00
|
|
|
v, err := gitver.ExecAndParse()
|
2019-06-21 04:55:24 +00:00
|
|
|
if nil != err {
|
2019-07-08 20:36:22 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to get git version: %s\n", err)
|
|
|
|
if exitCode > 0 {
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
v = &gitver.Versions{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
}
|
2019-06-21 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 08:32:19 +00:00
|
|
|
// Create or overwrite the go file from template
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := versionTpl.Execute(&buf, struct {
|
2019-06-21 05:27:41 +00:00
|
|
|
Package string
|
2019-06-21 04:55:24 +00:00
|
|
|
Timestamp string
|
|
|
|
Version string
|
2020-10-10 22:13:09 +00:00
|
|
|
Commit string
|
2019-06-21 04:55:24 +00:00
|
|
|
}{
|
2019-06-21 05:27:41 +00:00
|
|
|
Package: pkg,
|
2019-07-01 08:32:19 +00:00
|
|
|
Timestamp: v.Timestamp.Format(time.RFC3339),
|
|
|
|
Version: v.Version,
|
2020-10-10 22:13:09 +00:00
|
|
|
Commit: v.Rev,
|
2019-07-01 08:32:19 +00:00
|
|
|
}); nil != err {
|
2019-06-21 04:55:24 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format
|
|
|
|
src, err := format.Source(buf.Bytes())
|
|
|
|
if nil != err {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to disk (in the Current Working Directory)
|
|
|
|
f, err := os.Create(verFile)
|
|
|
|
if nil != err {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := f.Write(src); nil != err {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); nil != err {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var versionTpl = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
|
2019-06-21 05:27:41 +00:00
|
|
|
package {{ .Package }}
|
2019-06-21 04:55:24 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-10-10 22:13:09 +00:00
|
|
|
{{ if .Commit -}}
|
|
|
|
commit = "{{ .Commit }}"
|
2019-07-08 20:36:22 +00:00
|
|
|
{{ end -}}
|
|
|
|
{{ if .Version -}}
|
2020-10-10 22:13:09 +00:00
|
|
|
version = "{{ .Version }}"
|
2019-06-21 06:18:07 +00:00
|
|
|
{{ end -}}
|
2020-10-10 22:13:09 +00:00
|
|
|
date = "{{ .Timestamp }}"
|
2019-06-21 04:55:24 +00:00
|
|
|
}
|
|
|
|
`))
|