@@ -3,6 +3,8 @@ package main
33import (
44 "fmt"
55 "os"
6+ "runtime/debug"
7+ "strings"
68
79 arazzoCmd "github.com/speakeasy-api/openapi/arazzo/cmd"
810 openapiCmd "github.com/speakeasy-api/openapi/openapi/cmd"
1618 date = "unknown"
1719)
1820
21+ // getVersionInfo returns version information, prioritizing ldflags values over build info
22+ func getVersionInfo () (string , string , string ) {
23+ // If version/commit/date were set via ldflags (GoReleaser), use those
24+ if version != "dev" || commit != "none" || date != "unknown" {
25+ return version , commit , date
26+ }
27+
28+ // Otherwise, try to get info from build info
29+ buildInfo , ok := debug .ReadBuildInfo ()
30+ if ! ok {
31+ return version , commit , date
32+ }
33+
34+ // Use module version if available, otherwise fallback to "dev"
35+ moduleVersion := version
36+ if buildInfo .Main .Version != "" && buildInfo .Main .Version != "(devel)" {
37+ moduleVersion = buildInfo .Main .Version
38+ }
39+
40+ // Extract VCS information
41+ vcsCommit := commit
42+ vcsTime := date
43+
44+ for _ , setting := range buildInfo .Settings {
45+ switch setting .Key {
46+ case "vcs.revision" :
47+ if len (setting .Value ) >= 7 {
48+ vcsCommit = setting .Value [:7 ] // Short commit hash
49+ } else {
50+ vcsCommit = setting .Value
51+ }
52+ case "vcs.time" :
53+ vcsTime = setting .Value
54+ }
55+ }
56+
57+ return moduleVersion , vcsCommit , vcsTime
58+ }
59+
1960var rootCmd = & cobra.Command {
2061 Use : "openapi" ,
2162 Short : "OpenAPI toolkit for working with OpenAPI specifications, overlays, and Arazzo workflows" ,
@@ -74,15 +115,26 @@ These commands help you validate and work with Arazzo documents.`,
74115}
75116
76117func init () {
118+ // Get version information (prioritizes ldflags, falls back to build info)
119+ currentVersion , currentCommit , currentDate := getVersionInfo ()
120+
121+ // Update root command version
122+ rootCmd .Version = currentVersion
123+
77124 // Set version template with build info
78- if commit != "none" && date != "unknown" {
79- rootCmd .SetVersionTemplate (`{{printf "%s" .Version}}
80- Build: ` + commit + `
81- Built: ` + date )
82- } else {
83- rootCmd .SetVersionTemplate (`{{printf "%s" .Version}}` )
125+ var versionTemplate strings.Builder
126+ versionTemplate .WriteString (`{{printf "%s" .Version}}` )
127+
128+ if currentCommit != "none" && currentCommit != "" {
129+ versionTemplate .WriteString ("\n Build: " + currentCommit )
130+ }
131+
132+ if currentDate != "unknown" && currentDate != "" {
133+ versionTemplate .WriteString ("\n Built: " + currentDate )
84134 }
85135
136+ rootCmd .SetVersionTemplate (versionTemplate .String ())
137+
86138 // Add OpenAPI spec validation command
87139 openapiCmd .Apply (openapiCmds )
88140
0 commit comments