Skip to content

add version command with build-time version injection (CHAITINCLI-13, close #80) - #82

Merged
jerrynbc merged 2 commits into
mainfrom
agent/CHAITINCLI-13-version-command
Sep 22, 2026
Merged

jerrynbc merged 2 commits into
mainfrom
agent/CHAITINCLI-13-version-command

Conversation

@jerrynbc

Copy link
Copy Markdown
Collaborator

Summary

chaitin-cli 之前没有任何方式查看当前版本(GitHub issue #80 / DevBoard CHAITINCLI-13)。本 PR 增加版本输出能力:

  • 新增 chaitin-cli version 子命令,直接输出纯版本号(脚本友好)。
  • root 命令接入 cobra 的 --version,输出与 version 子命令完全一致。
  • 新增 main.version 注入点:Taskfile.yml 的 build / build:target / package 统一通过 -ldflags "-X main.version=..." 注入,默认取 git describe --tags --always --dirty,可用 VERSION= 覆盖。
  • CI 打包 job 把 tag(GITHUB_REF_NAME)通过 VERSION= 传给 task package,release 二进制从此带上真实版本号。
  • 未注入版本时(go run .、普通 go build)回退到 debug.ReadBuildInfo():优先模块版本,其次 vcs.revision 短 hash(dev+<hash>),都没有则 dev。
  • README / README.en.md 补充命令用法与版本注入说明。

行为示例:

$ task build VERSION=v0.0.0-test && ./bin/chaitin-cli version
v0.0.0-test
$ ./bin/chaitin-cli --version
v0.0.0-test
$ go run . version
dev

Tests

在 golang:1.25 Docker 环境(Go 1.25,本地 Go 1.18 不满足 go.mod)执行仓库既有 CI 步骤:

  • task fmt:check — exit 0
  • task lint(go vet ./...)— exit 0
  • task test(go test ./...)— 全部 PASS
  • task build VERSION=v0.0.0-test → ./bin/chaitin-cli version 输出 v0.0.0-test,--version 一致
  • task package GOOS=linux GOARCH=amd64 VERSION=v9.9.9 ... → bin/linux_amd64/chaitin-cli version 输出 v9.9.9,dist/ 正常产出 tar.gz
  • go run . version → dev(无 git/vcs 信息环境的最终回退路径)
  • chaitin-cli version extra → 退出码 1,报 unknown command

新增单测(version_test.go,表驱动):

  • 注入版本优先
  • build info 模块版本回退
  • (devel) + vcs.revision → dev+<12位hash>
  • 无 build info → dev
  • --version 与 version 输出一致

Risk

  • --version 输出格式从 cobra 默认的 chaitin-cli version vX.Y.Z 被改成纯版本号,以便和 version 子命令保持一致、便于脚本解析;如果希望保留 cobra 默认格式,去掉 SetVersionTemplate 一行即可。
  • commit message 中写了 close #80,PR 合并后 GitHub 会自动关闭该 issue;若希望人工关闭,请在合并前修改 commit message 或合并后手动重开。
  • 版本号只在 task/CI 构建路径注入;用户自行 go build 且环境无 git 时显示 dev,这是有意设计,但意味着非官方构建产物无法显示 tag。
  • 未改动任何产品子命令,未做自动升级/版本检查,非目标范围内。

chaitin-cli version prints the CLI version, and --version is kept in
sync with it. The version is injected via -ldflags from Taskfile/CI and
falls back to Go build info (module version, then vcs revision) or dev.

Refs CHAITINCLI-13, close #80

Co-authored-by: multica-agent <github@multica.ai>
A build without an injected version could still read as a release version:
plain `go build` in a checkout and `go install <module>@latest` both carry the
Go pseudo-version the toolchain stamps from the commit (for example
v0.0.0-20260920052045-2d4b5d8fd518), because Main.Version was preferred over
the revision. Prefer the recorded VCS revision, and treat a pseudo-version as
an untagged build even when no VCS settings are recorded, so both paths report
dev+<commit>. Main.Version is now only used when it names a real release.

README documents the actual fallback order, and the version tests cover the
two pseudo-version inputs (taken from real `go build` / `go install` binaries)
and assert that output stays on stdout.

Refs CHAITINCLI-13

Co-Authored-By: Claude Code <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
@jerrynbc
jerrynbc merged commit fd8b4c8 into main Sep 22, 2026
3 checks passed
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.

1 participant