feat(tencent): add --format json to cost by-product and cost top#180
Merged
Conversation
Same pattern as the --format flag on `tencent list` (PR #179) — the existing table renderer is preserved by default, and JSON mode is opt-in via --format json. The JSON envelope is the canonical wire shape consumed by the clanker-cloud Tencent cost.Provider. Why: clanker-cloud Phase d wires Tencent into the Cost Explorer. Without --format json on these subcommands the backend would have to parse tabwriter output to extract per-service spend, which is brittle and loses the precision of the Tencent SDK's string-encoded decimals. Wire shapes: cost by-product --format json: { month, items: [{product, real_cost, cash, incentive, voucher, pct}], total } cost top --format json: { month, top, items: [{product, resource_id, name, region, pay_mode, action, cost}] } Internals refactor: both list functions now split into a buildXReport function that returns the typed struct and a writeXTable helper that preserves the historical text output. listBillByProduct/Top stays the single entry point and just routes by format. No external callers depend on the old signatures because both were only invoked from their respective cobra RunE. Tests: JSON round-trip pins both shapes against drift. go vet + gofmt -s clean; existing CLI behaviour unchanged.
rephapeng
added a commit
to rephapeng/clanker
that referenced
this pull request
May 28, 2026
Upstream merged PR bgdnvk#165 (Tencent provider) and added work on top: k8s SRE playbooks (bgdnvk#174), SRE agent fix (bgdnvk#177), tree-wide gofmt -s (bgdnvk#176), README (bgdnvk#175), and three Tencent CLI features the fork lacked — `list --format json` (bgdnvk#179), `cost --format json` (bgdnvk#180), and security-scan CLI subcommands (bgdnvk#181). Conflict resolution: all 16 conflicts resolved to upstream's side. 14 were pure gofmt whitespace from bgdnvk#176 (identical code); billing.go and static_commands.go were upstream supersets adding the JSON/security CLI surface with no fork-unique code lost. Fixed a duplicate tencent import in cmd/ask.go left by the auto-merge. Verified in Docker (golang:1.25, -mod=mod): gofmt clean, go build ./..., go vet ./..., and go test ./... all pass.
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
Mirrors PR #179 (which added
--format jsontotencent list) — adds the same flag totencent cost by-productandtencent cost top. Default behaviour is unchanged (tabwriter output); JSON mode is opt-in.Why
clanker-cloud is wiring Tencent into the Cost Explorer (Phase d). Without
--format jsonon these subcommands the cost.Provider implementation would have to parse tabwriter output, which is brittle and loses the precision of Tencent's string-encoded decimal amounts.Wire shapes
Internals
listBillByProduct/listBillResourceTopsplit intobuildXReport(typed struct) +writeXTable(preserved text output). Single entry point dispatches by--format.BillByProductItem/BillByProductReport/BillTopResource/BillTopResourceReportare exported so the cloud backend can consume them directly via JSON.Test plan
go test ./internal/tencent/ -run "BillByProduct|BillTopResource"— JSON round-trip tests for both shapesgo vet ./...,gofmt -s -l .clean