Skip to content

fix: register and consume --json shorthand for custom-format shortcuts#1737

Open
zhaojunlin0405 wants to merge 9 commits into
mainfrom
fix/mail-triage-json-shorthand
Open

fix: register and consume --json shorthand for custom-format shortcuts#1737
zhaojunlin0405 wants to merge 9 commits into
mainfrom
fix/mail-triage-json-shorthand

Conversation

@zhaojunlin0405

@zhaojunlin0405 zhaojunlin0405 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

--json (shorthand for --format json) was only registered when the framework injected the default format flag, so shortcuts declaring their own format flag — mail +triage, mail +watch, base +record-list — rejected it with unknown flag: --json. In addition, nothing ever consumed the flag's value: it appeared to work on other commands only because their format already defaults to json, and --format table --json silently ignored the shorthand.

This change decouples shorthand registration from default-format injection and adds an explicit consumption step:

  • ensureJSONShorthand registers --json when a command has a format flag whose Enum contains json and the json flag name is not already taken.
  • applyJSONShorthand folds the shorthand into the format flag itself before it is consumed, so both the cached format and per-command reads observe json. An explicitly passed --format always wins over the shorthand.
  • Commands declaring their own --json (event +subscribe's pretty-print switch, base +record-search / +record-get's request-body payload) are untouched on both the registration and consumption paths.

Behavior change: invalid --format values on the five affected commands now fail enum validation (exit 2, listing allowed values) instead of silently falling back to the default format (mail +triage) or erroring later inside Execute (the others).

Changes

  • shortcuts/common/runner.go: add shortcutDeclaresJSONFlag / shortcutFormatSupportsJSON / ensureJSONShorthand / applyJSONShorthand; decouple the --json registration from the default-format injection block; apply normalization in newRuntimeContext before the format value is cached
  • shortcuts/mail/mail_triage.go, shortcuts/mail/mail_watch.go, shortcuts/base/record_list.go: add Enum metadata to the existing format flag declarations (one line each; defaults unchanged)
  • skills/lark-mail/references/lark-mail-triage.md, lark-mail-watch.md, skills/lark-base/references/lark-base-data-query.md: document the shorthand, the explicit-format precedence, and JSON-output guidance for machine-consumption scenarios
  • New tests: shortcuts/common/runner_json_shorthand_test.go (registration matrix + normalization precedence), shortcuts/mail/mail_json_shorthand_test.go, shortcuts/base/record_json_shorthand_test.go

Test Plan

  • go test ./shortcuts/...: registration matrix (5 cases), normalization precedence (6 cases), mail behavior (help / dry-run equivalence / explicit-table precedence / enum error, 6 cases), base registration + request-body regression (3 cases) — all green
  • go build ./... and incremental golangci-lint clean
  • Sandbox E2E: 11/11 scenarios pass, covering dry-run byte equivalence between --json and --format json, explicit --format table --json precedence via the jq mutual-exclusion probe, enum validation errors on all five commands, and record-search / record-get request-body --json regression anchors

Related Issues

  • Fixes unknown flag: --json on mail +triage; the same registration gap on mail +watch and base +record-list is fixed by the shared rule
  • Follow-up (tracked separately, pre-existing and unchanged by this PR): mail +triage --jq is accepted but silently ignored because the command's custom output path bypasses the common jq filter

Summary by CodeRabbit

  • New Features
    • Added --json shorthand support for relevant --format-based commands (e.g., record list/search/get and mail commands), documented as an alias for --format json.
    • Tightened --format options for +record-list, mail +triage, and mail +watch with explicit allowed values.
  • Bug Fixes
    • Ensured --json correctly sets output to JSON when appropriate, while preserving commands where --json represents request-body data.
    • Enforced precedence: explicit --format overrides --json, and validation reports allowed values clearly.
  • Documentation
    • Updated reference pages to clarify --json vs --format behavior and request-body vs output formatting distinctions.

@zhaojunlin0405 zhaojunlin0405 added bug Something isn't working domain/base PR touches the base domain domain/mail PR touches the mail domain labels Jul 3, 2026
@CLAassistant

CLAassistant commented Jul 3, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the size/L Large or sensitive change across domains or core paths label Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0fbe1829-571f-40fa-a732-1f3e07a12944

📥 Commits

Reviewing files that changed from the base of the PR and between eb56913 and 4add473.

📒 Files selected for processing (1)
  • shortcuts/mail/mail_json_shorthand_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • shortcuts/mail/mail_json_shorthand_test.go

📝 Walkthrough

Walkthrough

This PR adds shared --json shorthand handling for shortcut commands, restricts several --format flags to explicit enum values, and updates tests and reference docs to match the new behavior.

Changes

JSON shorthand and format validation

Layer / File(s) Summary
Runner shorthand wiring
shortcuts/common/runner.go
Adds helpers that decide when --json can be registered, applies shorthand parsing before runtime format caching, and skips shortcuts that already define their own --json.
Runner shorthand tests
shortcuts/common/runner_json_shorthand_test.go
Adds tests for registration rules, precedence between --json and --format, and preservation of shortcut-defined --json semantics.
Base record format and shortcut tests
shortcuts/base/record_list.go, shortcuts/base/record_json_shorthand_test.go, skills/lark-base/references/lark-base-data-query.md
Constrains record-list format values to markdown and json, verifies --json behavior for base record shortcuts, and updates the base reference note.
Mail format constraints and CLI tests
shortcuts/mail/mail_triage.go, shortcuts/mail/mail_watch.go, shortcuts/mail/mail_json_shorthand_test.go, skills/lark-mail/references/lark-mail-triage.md, skills/lark-mail/references/lark-mail-watch.md
Constrains mail triage/watch format values, adds CLI tests for help text, output behavior, precedence, and enum validation, and updates the mail reference docs.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

  • larksuite/cli#301: Both changes touch mail +triage output formatting and JSON-related command behavior.
  • larksuite/cli#328: This PR’s base record tests specifically cover shortcuts whose own --json flag should remain unchanged.
  • larksuite/cli#1104: Both PRs modify shortcuts/common/runner.go around --json shorthand registration and conflict avoidance.

Suggested labels: feature

Suggested reviewers: kongenpei, chanthuang, liangshuo-1

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding and consuming a --json shorthand for custom-format shortcuts.
Description check ✅ Passed The description covers the required Summary, Changes, Test Plan, and Related Issues sections with relevant detail.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mail-triage-json-shorthand

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@4add473509c1a087361d82feb07c608d2439eb9b

🧩 Skill update

npx skills add larksuite/cli#fix/mail-triage-json-shorthand -y -g

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.42%. Comparing base (d0cde9a) to head (4add473).
⚠️ Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
shortcuts/common/runner.go 86.20% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1737      +/-   ##
==========================================
- Coverage   74.51%   74.42%   -0.09%     
==========================================
  Files         852      854       +2     
  Lines       87655    88485     +830     
==========================================
+ Hits        65312    65859     +547     
- Misses      17315    17555     +240     
- Partials     5028     5071      +43     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
shortcuts/common/runner_json_shorthand_test.go (1)

29-30: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Comment language consistency.

Test comments are in Chinese while the rest of the codebase (e.g. runner.go) documents in English, which may hinder readability for the broader contributor base.

Also applies to: 49-49, 61-61, 73-73, 110-110, 120-120, 130-130, 140-140, 150-150, 170-170, 187-187

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@shortcuts/common/runner_json_shorthand_test.go` around lines 29 - 30, The
test comments in runner_json_shorthand_test.go are written in Chinese while the
surrounding codebase documentation is in English, so update the affected comment
blocks to English for consistency and readability. Keep the existing test names
and behavior unchanged, and revise the inline comments near the JSON shorthand
tests so they clearly describe the cases in English across all referenced
occurrences.
shortcuts/mail/mail_watch.go (1)

102-102: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Manual format validation now redundant after adding Enum.

With Enum: []string{"json", "data"} on the format flag (Line 102), the framework will reject any other value before Execute runs — confirmed by TestMailTriageEnumRejectsUnknownFormat's dry-run test rejecting invalid values without reaching business logic. The switch/default error branch at Lines 188-192 is therefore dead code for real invocations.

Consider removing it (or documenting why it's kept as defense-in-depth) to avoid confusing future maintainers about where validation actually happens.

♻️ Proposed cleanup
 		outFormat := runtime.Str("format")
-		switch outFormat {
-		case "json", "data", "":
-		default:
-			return mailValidationParamError("--format", "invalid --format %q: must be json or data", outFormat)
-		}
 		msgFormat := runtime.Str("msg-format")

Also applies to: 188-192

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@shortcuts/mail/mail_watch.go` at line 102, The format validation in MailWatch
is now duplicated because the format flag already has an Enum on the flags
definition, so the framework rejects invalid values before execution reaches
business logic. Remove the unreachable default/error branch in MailWatch.Execute
that handles unknown format values, or keep it only if you add an explicit
comment explaining it as defense-in-depth; use the format flag definition and
the Execute switch on format as the symbols to locate the cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@shortcuts/mail/mail_json_shorthand_test.go`:
- Around line 80-92: The test in TestMailTriageEnumRejectsUnknownFormat
currently verifies only error message substrings; update it to assert typed
error metadata instead. Use errs.ProblemOf on the error returned by
runMountedMailShortcut to check the expected category/subtype/param for the
invalid --format value, and also verify cause preservation rather than relying
on strings.Contains checks on err.Error().

---

Nitpick comments:
In `@shortcuts/common/runner_json_shorthand_test.go`:
- Around line 29-30: The test comments in runner_json_shorthand_test.go are
written in Chinese while the surrounding codebase documentation is in English,
so update the affected comment blocks to English for consistency and
readability. Keep the existing test names and behavior unchanged, and revise the
inline comments near the JSON shorthand tests so they clearly describe the cases
in English across all referenced occurrences.

In `@shortcuts/mail/mail_watch.go`:
- Line 102: The format validation in MailWatch is now duplicated because the
format flag already has an Enum on the flags definition, so the framework
rejects invalid values before execution reaches business logic. Remove the
unreachable default/error branch in MailWatch.Execute that handles unknown
format values, or keep it only if you add an explicit comment explaining it as
defense-in-depth; use the format flag definition and the Execute switch on
format as the symbols to locate the cleanup.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 49f57623-1d3b-40eb-b5ca-f7fdf145065e

📥 Commits

Reviewing files that changed from the base of the PR and between a1506cd and eb56913.

📒 Files selected for processing (10)
  • shortcuts/base/record_json_shorthand_test.go
  • shortcuts/base/record_list.go
  • shortcuts/common/runner.go
  • shortcuts/common/runner_json_shorthand_test.go
  • shortcuts/mail/mail_json_shorthand_test.go
  • shortcuts/mail/mail_triage.go
  • shortcuts/mail/mail_watch.go
  • skills/lark-base/references/lark-base-data-query.md
  • skills/lark-mail/references/lark-mail-triage.md
  • skills/lark-mail/references/lark-mail-watch.md

Comment thread shortcuts/mail/mail_json_shorthand_test.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working domain/base PR touches the base domain domain/mail PR touches the mail domain size/L Large or sensitive change across domains or core paths

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants