Skip to content

Report OpenAPI spec version and last-check date in --version#19

Merged
ndenny merged 5 commits into
developfrom
report-spec-version-in-version-cmd
Jul 24, 2026
Merged

Report OpenAPI spec version and last-check date in --version#19
ndenny merged 5 commits into
developfrom
report-spec-version-in-version-cmd

Conversation

@ndenny

@ndenny ndenny commented Jul 21, 2026

Copy link
Copy Markdown
Member

Summary

apimetrics --version now reports the OpenAPI document version (info.version) and the date/time the spec was last fetched from the server, to aid customer support and debugging:

apimetrics version <cli-version>
API spec version: <info.version>
Spec last updated:  <date/time>

Background

The CLI already auto-refreshes the cached OpenAPI spec on a 24-hour TTL (cacheAPI in cli/api.go) — that cadence is unchanged here. The value added is visibility: previously the spec's own version was parsed by libopenapi and then discarded, and there was no way to see when the spec was last refreshed.

Changes

  • cli/api.go — Add SpecVersion to the cached API struct (CBOR-persisted), carry it through Merge, and record a <name>.checked timestamp in cacheAPI. Add versionExtraInfo() which reads the loaded API or falls back to the on-disk .cbor cache.
  • openapi/openapi.go — Capture model.Info.Version into the built cli.API.
  • cli/cli.go — Register a versionExtra template func + custom cobra version template, and make --version skip the network load (reads cached state from disk).
  • openapi/testdata/*/output.yaml — Fixtures updated to include spec_version.

Resilience

--version is used precisely when things are broken, so it must not depend on the network. It now reads the cached spec state from disk and skips the load entirely — no network call, no auth prompt, no panic when offline. Verified:

Scenario Result
Cold cache + offline unknown / never, clean exit
Warm cache, online spec version + timestamp, no network call
Warm cache, offline identical — reads from disk

After a binary upgrade, existing caches repopulate automatically (a CLI version change already invalidates the cache).

Testing

  • go test ./... — all pass
  • go vet ./... — clean
  • Manual verification of the four scenarios above

🤖 Generated with Claude Code

`apimetrics --version` now prints the OpenAPI document version
(info.version) and the date/time the spec was last fetched from the
server, to aid customer support and debugging.

- Persist the spec version in the cached API struct (SpecVersion),
  captured from libopenapi's model.Info.Version (previously discarded).
- Record a `<name>.checked` timestamp in cacheAPI (24h refresh TTL
  unchanged).
- Add a custom cobra version template that appends the two lines.
- Make --version a pure reporting command: it reads the cached spec
  state from disk and skips the network load, so it works offline and
  never triggers an auth prompt.

Fixtures updated: testdata/*/output.yaml now include spec_version.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 21, 2026 14:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances apimetrics --version output to include the cached OpenAPI document’s info.version plus a “last checked” timestamp, while ensuring --version avoids any network/API loading (so it remains usable when offline).

Changes:

  • Persist OpenAPI info.version into the cached cli.API model (SpecVersion) and expose it via the Cobra version template.
  • Record a per-API “checked” timestamp when the API spec cache is written, and display it in --version.
  • Update OpenAPI test fixtures to include spec_version in expected YAML output.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
cli/api.go Adds SpecVersion to cached API state, records <name>.checked, and implements versionExtraInfo() to read cached spec version + timestamp.
cli/cli.go Customizes Cobra version template and skips network loading when --version is requested.
openapi/openapi.go Captures model.Info.Version into cli.API.SpecVersion during OpenAPI load.
openapi/testdata/request/output.yaml Updates expected fixture output to include spec_version.
openapi/testdata/petstore/output.yaml Updates expected fixture output to include spec_version.
openapi/testdata/long_example/output.yaml Updates expected fixture output to include spec_version.
openapi/testdata/group-resp/output.yaml Updates expected fixture output to include spec_version.
openapi/testdata/extensions/output.yaml Updates expected fixture output to include spec_version.
openapi/testdata/auto_config/output.yaml Updates expected fixture output to include spec_version.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cli/api.go
Address PR review: versionExtraInfo now only trusts the on-disk cache
when its CLIVersion matches the running binary, mirroring Load's
validity check, so --version won't report a stale spec version/timestamp
after a binary upgrade (the CLI would refetch that cache anyway). Also
skip the cache lookup entirely when api-name is empty.

Add TestVersionExtraInfo covering the no-cache, valid-cache, and
stale-cache (mismatched CLIVersion) paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ndenny

ndenny commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Thanks — both points addressed in 38e4ca3.

versionExtraInfo now gates the on-disk cache read on cached.CLIVersion == Root.Version, mirroring the validity check in Load, so after a binary upgrade --version reports unknown / never rather than a stale spec version/timestamp that the CLI would refetch anyway. The <name>.checked lookup (and the whole cache read) is now also skipped when api-name is empty. Added TestVersionExtraInfo covering the no-cache, valid-cache, and stale-cache (mismatched CLIVersion) paths.

Loading the OpenAPI spec (the entrypoint and /openapi.json) is done
against public endpoints, but MakeRequest was still applying the
profile's auth, which triggered an interactive login on any invocation
that hit a cache miss -- including `apimetrics`, `apimetrics --help`,
completion, and config commands that never call an API operation.

Add an IgnoreAuth() request option and use it for the two spec-loading
requests in Load, so building the command tree never prompts for login.
Auth is still applied for real API operations (which don't pass the
option).

Add TestIgnoreAuth verifying the auth hook is skipped.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Comment thread cli/cli.go
Comment thread cli/api.go Outdated
ndenny and others added 2 commits July 21, 2026 13:03
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Address PR review: the offline/no-auth skip for --version matched only
the literal "--version" arg, so `--version=true` would still trigger the
network load. Register a version bool on GlobalFlags and read it via the
eager parse, which handles every bool form (--version, --version=true,
--version=false) consistently with Cobra.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ndenny
ndenny merged commit 85b5175 into develop Jul 24, 2026
1 check passed
@ndenny
ndenny deleted the report-spec-version-in-version-cmd branch July 24, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants