Repository navigation
add version command with build-time version injection (CHAITINCLI-13, close #80) - #82
Merged
Merged
Conversation
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>
Closed
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
chaitin-cli之前没有任何方式查看当前版本(GitHub issue #80 / DevBoard CHAITINCLI-13)。本 PR 增加版本输出能力:chaitin-cli version子命令,直接输出纯版本号(脚本友好)。--version,输出与version子命令完全一致。main.version注入点:Taskfile.yml的build/build:target/package统一通过-ldflags "-X main.version=..."注入,默认取git describe --tags --always --dirty,可用VERSION=覆盖。GITHUB_REF_NAME)通过VERSION=传给task package,release 二进制从此带上真实版本号。go run .、普通go build)回退到debug.ReadBuildInfo():优先模块版本,其次vcs.revision短 hash(dev+<hash>),都没有则dev。行为示例:
Tests
在
golang:1.25Docker 环境(Go 1.25,本地 Go 1.18 不满足go.mod)执行仓库既有 CI 步骤:task fmt:check— exit 0task lint(go vet ./...)— exit 0task test(go test ./...)— 全部 PASStask 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.gzgo run . version→dev(无 git/vcs 信息环境的最终回退路径)chaitin-cli version extra→ 退出码 1,报 unknown command新增单测(
version_test.go,表驱动):(devel)+vcs.revision→dev+<12位hash>dev--version与version输出一致Risk
--version输出格式从 cobra 默认的chaitin-cli version vX.Y.Z被改成纯版本号,以便和version子命令保持一致、便于脚本解析;如果希望保留 cobra 默认格式,去掉SetVersionTemplate一行即可。close #80,PR 合并后 GitHub 会自动关闭该 issue;若希望人工关闭,请在合并前修改 commit message 或合并后手动重开。task/CI 构建路径注入;用户自行go build且环境无 git 时显示dev,这是有意设计,但意味着非官方构建产物无法显示 tag。