Skip to content

Commit

Permalink
Add version flag
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Comellini <[email protected]>
  • Loading branch information
lucacome committed Apr 30, 2024
1 parent 89e5ed0 commit f50c4b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ refdocs

# goreleaser output
dist

gen-crd-api-reference-docs
4 changes: 4 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ builds:
goarch:
- amd64
- arm64
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}}

archives:
- files:
Expand Down
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"path/filepath"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
Expand All @@ -30,9 +32,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 (
Expand Down Expand Up @@ -82,6 +88,15 @@ func init() {
flag.Set("alsologtostderr", "true") // for klog
flag.Parse()

commitHash, commitTime, dirtyBuild := getBuildInfo()
arch := fmt.Sprintf("%v/%v", runtime.GOOS, runtime.GOARCH)

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())

if *flVersion {
os.Exit(0)
}

if *flConfig == "" {
panic("-config not specified")
}
Expand Down Expand Up @@ -702,3 +717,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
}

0 comments on commit f50c4b2

Please sign in to comment.