From 819de227e5fe85ee70022e71191d7838847e075a Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Sat, 27 Jul 2024 07:34:55 -0700 Subject: [PATCH] Add version flag (#76) --- .github/workflows/ci.yml | 4 ++++ .gitignore | 2 ++ .goreleaser.yml | 4 ++++ main.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 23945b1..36aeea7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,3 +46,7 @@ jobs: args: ${{ github.ref_type == 'tag' && 'release' || 'build --snapshot' }} --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Print version + run: ./dist/gen-crd-api-reference-docs_linux_amd64_v1/gen-crd-api-reference-docs -version + continue-on-error: true diff --git a/.gitignore b/.gitignore index a4d184e..dcea91a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ refdocs # goreleaser output dist + +gen-crd-api-reference-docs diff --git a/.goreleaser.yml b/.goreleaser.yml index 40ecfa4..ac18240 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -8,6 +8,10 @@ builds: goarch: - amd64 - arm64 + flags: + - -trimpath + ldflags: + - -s -w -X main.version={{.Version}} archives: - files: diff --git a/main.go b/main.go index 762df37..d3f8d40 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "path/filepath" "reflect" "regexp" + "runtime" + "runtime/debug" "sort" "strconv" "strings" @@ -31,9 +33,13 @@ var ( flConfig = flag.String("config", "", "path to config file") flAPIDir = flag.String("api-dir", "", "api directory (or import path), point this to pkg/apis") flTemplateDir = flag.String("template-dir", "template", "path to template/ dir") + flVersion = flag.Bool("version", false, "print version and exit") flHTTPAddr = flag.String("http-addr", "", "start an HTTP server on specified addr to view the result (e.g. :8080)") flOutFile = flag.String("out-file", "", "path to output file to save the result") + + // set by go build + version string ) const ( @@ -83,6 +89,14 @@ func init() { flag.Set("alsologtostderr", "true") // for klog flag.Parse() + commitHash, commitTime, dirtyBuild := getBuildInfo() + arch := fmt.Sprintf("%v/%v", runtime.GOOS, runtime.GOARCH) + + if *flVersion { + fmt.Printf("gen-crd-api-reference-docs version=%s commit=%s date=%s dirty=%v arch=%s go=%v\n", version, commitHash, commitTime, dirtyBuild, arch, runtime.Version()) + os.Exit(0) + } + if *flConfig == "" { panic("-config not specified") } @@ -707,3 +721,23 @@ func render(w io.Writer, pkgs []*apiPackage, config generatorConfig) error { return nil } + +func getBuildInfo() (string, string, bool) { + var commitHash, commitTime string + var dirtyBuild bool + info, ok := debug.ReadBuildInfo() + if !ok { + return "", "", false + } + for _, kv := range info.Settings { + switch kv.Key { + case "vcs.revision": + commitHash = kv.Value + case "vcs.time": + commitTime = kv.Value + case "vcs.modified": + dirtyBuild = kv.Value == "true" + } + } + return commitHash, commitTime, dirtyBuild +}