Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jobs:
- name: Run tests
run: mise run test

- name: Install syft for SBOM generation
run: |
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
Expand Down
64 changes: 58 additions & 6 deletions cmd/openapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"
"runtime/debug"
"strings"

arazzoCmd "github.com/speakeasy-api/openapi/arazzo/cmd"
openapiCmd "github.com/speakeasy-api/openapi/openapi/cmd"
Expand All @@ -16,6 +18,45 @@ var (
date = "unknown"
)

// getVersionInfo returns version information, prioritizing ldflags values over build info
func getVersionInfo() (string, string, string) {
// If version/commit/date were set via ldflags (GoReleaser), use those
if version != "dev" || commit != "none" || date != "unknown" {
return version, commit, date
}

// Otherwise, try to get info from build info
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return version, commit, date
}

// Use module version if available, otherwise fallback to "dev"
moduleVersion := version
if buildInfo.Main.Version != "" && buildInfo.Main.Version != "(devel)" {
moduleVersion = buildInfo.Main.Version
}

// Extract VCS information
vcsCommit := commit
vcsTime := date

for _, setting := range buildInfo.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) >= 7 {
vcsCommit = setting.Value[:7] // Short commit hash
} else {
vcsCommit = setting.Value
}
case "vcs.time":
vcsTime = setting.Value
}
}

return moduleVersion, vcsCommit, vcsTime
}

var rootCmd = &cobra.Command{
Use: "openapi",
Short: "OpenAPI toolkit for working with OpenAPI specifications, overlays, and Arazzo workflows",
Expand Down Expand Up @@ -74,15 +115,26 @@ These commands help you validate and work with Arazzo documents.`,
}

func init() {
// Get version information (prioritizes ldflags, falls back to build info)
currentVersion, currentCommit, currentDate := getVersionInfo()

// Update root command version
rootCmd.Version = currentVersion

// Set version template with build info
if commit != "none" && date != "unknown" {
rootCmd.SetVersionTemplate(`{{printf "%s" .Version}}
Build: ` + commit + `
Built: ` + date)
} else {
rootCmd.SetVersionTemplate(`{{printf "%s" .Version}}`)
var versionTemplate strings.Builder
versionTemplate.WriteString(`{{printf "%s" .Version}}`)

if currentCommit != "none" && currentCommit != "" {
versionTemplate.WriteString("\nBuild: " + currentCommit)
}

if currentDate != "unknown" && currentDate != "" {
versionTemplate.WriteString("\nBuilt: " + currentDate)
}

rootCmd.SetVersionTemplate(versionTemplate.String())

// Add OpenAPI spec validation command
openapiCmd.Apply(openapiCmds)

Expand Down
Loading