Skip to content

feat(tencent): add --format json to cost by-product and cost top#180

Merged
rafeegnash merged 1 commit into
masterfrom
feat/tencent-cost-json
May 28, 2026
Merged

feat(tencent): add --format json to cost by-product and cost top#180
rafeegnash merged 1 commit into
masterfrom
feat/tencent-cost-json

Conversation

@rafeegnash
Copy link
Copy Markdown
Collaborator

Summary

Mirrors PR #179 (which added --format json to tencent list) — adds the same flag to tencent cost by-product and tencent 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 json on 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

// `tencent cost by-product --format json`
{
  "month": "2026-05",
  "items": [
    {"product": "CVM", "real_cost": "12.50", "cash": "10.00", "incentive": "0.00", "voucher": "2.50", "pct": "62.5"}
  ],
  "total": 20.0
}
// `tencent cost top --format json`
{
  "month": "2026-05",
  "top": 5,
  "items": [
    {"product": "CVM", "resource_id": "ins-1", "name": "web-a", "region": "ap-singapore", "pay_mode": "PREPAID", "action": "renew", "cost": "12.50"}
  ]
}

Internals

  • listBillByProduct / listBillResourceTop split into buildXReport (typed struct) + writeXTable (preserved text output). Single entry point dispatches by --format.
  • New types BillByProductItem / BillByProductReport / BillTopResource / BillTopResourceReport are 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 shapes
  • go vet ./..., gofmt -s -l . clean
  • Help text shows the new flag on both subcommands
  • Live smoke with real Tencent creds — JSON output matches the table output byte-for-byte field-wise

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.
@rafeegnash rafeegnash merged commit 21c9c10 into master May 28, 2026
5 checks passed
@rafeegnash rafeegnash deleted the feat/tencent-cost-json branch May 28, 2026 06:29
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.
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