From 35971c9a80879728a7bec38494a1edc1ecbf740f Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Tue, 16 Jun 2020 22:14:35 +0200 Subject: [feat] include git version in generated variable --- build.sh | 1 + pkg/config/version/.gitignore | 1 + pkg/config/version/LICENSE | 19 +++++++++ pkg/config/version/gen.go | 3 ++ pkg/config/version/main.go | 95 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 pkg/config/version/.gitignore create mode 100644 pkg/config/version/LICENSE create mode 100644 pkg/config/version/gen.go create mode 100644 pkg/config/version/main.go diff --git a/build.sh b/build.sh index 402525f..4ce1e77 100755 --- a/build.sh +++ b/build.sh @@ -28,6 +28,7 @@ function init { function buildGoLib { echo "[+] Using go in" $GO "[`go version`]" + $GO generate ./pkg/config/version/gen.go if [ "$XBUILD" == "no" ] then echo "[+] Building Go library with standard Go compiler" diff --git a/pkg/config/version/.gitignore b/pkg/config/version/.gitignore new file mode 100644 index 0000000..f53f6d3 --- /dev/null +++ b/pkg/config/version/.gitignore @@ -0,0 +1 @@ +version.go diff --git a/pkg/config/version/LICENSE b/pkg/config/version/LICENSE new file mode 100644 index 0000000..8ba5491 --- /dev/null +++ b/pkg/config/version/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015 Vincent Batts, Raleigh, NC, USA + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/pkg/config/version/gen.go b/pkg/config/version/gen.go new file mode 100644 index 0000000..334ed50 --- /dev/null +++ b/pkg/config/version/gen.go @@ -0,0 +1,3 @@ +package main + +//go:generate go run . -output version.go diff --git a/pkg/config/version/main.go b/pkg/config/version/main.go new file mode 100644 index 0000000..a86b619 --- /dev/null +++ b/pkg/config/version/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "flag" + "io" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + "text/template" + "time" +) + +var ( + flPackageName = flag.String("package", "main", "name for the generated golang package") + flVariableName = flag.String("variable", "VERSION", "variable name in the generated golang package") + flOutputFile = flag.String("output", "", "output filename (default stdout)") +) + +func main() { + flag.Parse() + dir := "." + if flag.NArg() > 0 { + dir = flag.Args()[0] + } + dir, err := filepath.Abs(dir) + if err != nil { + log.Fatal(err) + } + + var output io.Writer + if len(*flOutputFile) > 0 { + fh, err := os.Create(*flOutputFile) + if err != nil { + log.Fatal(err) + } + defer fh.Close() + output = fh + } else { + output = os.Stdout + } + + vers, err := GitDescribe(dir) + if err != nil { + log.Fatal(err) + } + vp := VersionPackage{ + Name: *flPackageName, + Path: dir, + Date: time.Now(), + Variable: *flVariableName, + Version: vers, + } + + packageTemplate.Execute(output, vp) +} + +// VersionPackage is the needed information to template a version package +type VersionPackage struct { + Name string + Path string + Date time.Time + Variable string + Version string +} + +var packageTemplate = template.Must(template.New("default").Parse(packageLayout)) +var packageLayout = `package {{.Name}} +// AUTO-GENERATED. DO NOT EDIT +// {{.Date}} + +// {{.Variable}} is generated by git-describe from gen.go +var {{.Variable}} = "{{.Version}}" + ` + +// GitDescribe calls `git describe` in the provided path +func GitDescribe(path string) (string, error) { + cwd, err := os.Getwd() + if err != nil { + return "", err + } + // TODO check if this is a directory + if err := os.Chdir(path); err != nil { + return "", err + } + defer os.Chdir(cwd) + + buf, err := exec.Command("git", "describe").CombinedOutput() + if err != nil { + return "", err + } + + return strings.TrimSpace(string(buf)), nil +} -- cgit v1.2.3