Skip to content

Commit 389a322

Browse files
fix: enhance CLI version detection with build info fallback and fix SBOM generation (#26)
1 parent 5e2328d commit 389a322

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

.github/workflows/release.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ jobs:
3030
- name: Run tests
3131
run: mise run test
3232

33+
- name: Install syft for SBOM generation
34+
run: |
35+
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
36+
3337
- name: Run GoReleaser
3438
uses: goreleaser/goreleaser-action@v6
3539
with:

cmd/openapi/main.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
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"
@@ -16,6 +18,45 @@ var (
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+
1960
var 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

76117
func 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("\nBuild: " + currentCommit)
130+
}
131+
132+
if currentDate != "unknown" && currentDate != "" {
133+
versionTemplate.WriteString("\nBuilt: " + currentDate)
84134
}
85135

136+
rootCmd.SetVersionTemplate(versionTemplate.String())
137+
86138
// Add OpenAPI spec validation command
87139
openapiCmd.Apply(openapiCmds)
88140

0 commit comments

Comments
 (0)