Skip to content

feat: add --json output mode to all func subcommands#3774

Open
Ankitsinghsisodya wants to merge 3 commits into
knative:mainfrom
Ankitsinghsisodya:feat/json-output-mode
Open

feat: add --json output mode to all func subcommands#3774
Ankitsinghsisodya wants to merge 3 commits into
knative:mainfrom
Ankitsinghsisodya:feat/json-output-mode

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

Summary

Implements structured JSON output for every func subcommand, as described in #3769.

All JSON is written to stdout using a versioned envelope:

{"apiVersion":"v1","status":"ok","data":{...}}
{"apiVersion":"v1","status":"error","error":{"category":"...","code":"...","retryable":false,"message":"...","hint":"..."}}

The flag is opt-in — all existing human-readable output is unchanged.

Changes

New files

  • cmd/json.go — shared JSONResponse/JSONError structs, WriteJSONSuccess, WriteJSONError, errorToJSONError (maps 14+ typed errors to category/code/retryable), and isJSONEnabled helper
  • cmd/json_test.go — 20 unit tests covering every error-type mapping, envelope shape, apiVersion presence, and round-trip validity

Global plumbing

  • cmd/root.go--json registered as a persistent flag on the root command (inheritable by all subcommands), bound to Viper
  • pkg/app/app.go — top-level error sink writes WriteJSONError to stdout when --json is set, instead of plain text to stderr

Per-command success payloads

Command data fields
build name, image
deploy name, namespace, url, image
create name, path, runtime, template
delete name, namespace
describe existing fn.Instance object (reuses Formatter.JSON())
list array of listItems (reuses Formatter.JSON())
run address, host, port (upgrades existing ad-hoc JSON to envelope)
invoke response (HTTP body)
version existing Version struct
languages array of runtime strings
templates map of runtime → template array
environment existing Environment struct
subscribe name, source, filters
repository list array of {name, url}
repository add/rename/remove operation result fields
logs explicitly unsupported — returns error if --json is set

Updated tests

  • cmd/languages_test.go — updated TestLanguages_JSON to assert envelope shape
  • cmd/templates_test.go — updated TestTemplates_JSON and TestTemplates_ByLanguage to assert envelope shape

Test plan

  • go test knative.dev/func/cmd -run "TestWriteJSON|TestAPIVersion|TestErrorToJSONError|TestRound|TestLanguages_JSON|TestTemplates_JSON|TestTemplates_ByLanguage|TestRepository_List" — all 26 pass
  • func create -l go myfunc --json → envelope with status: ok and data.name, data.runtime
  • func build --json → envelope with data.name, data.image
  • func deploy --json → envelope with data.name, data.namespace
  • func list --json → envelope with array in data
  • func logs --json → returns error: --json is not supported for streaming commands
  • Bad flag (e.g. missing registry) with --jsonstatus: error with category, code, retryable

- Added JSON output functionality to multiple commands including build, create, delete, deploy, describe, environment, invoke, languages, list, logs, repository, run, subscribe, templates, and version.
- Introduced a standardized JSON response structure for success and error messages, enhancing the user experience and consistency across the CLI.
- Updated existing commands to check for the --json flag and return structured JSON responses when enabled.
- Added tests to ensure the correctness of JSON output and error handling in various scenarios.

This enhancement improves the usability of the CLI by allowing users to easily parse command outputs programmatically.
Copilot AI review requested due to automatic review settings May 17, 2026 08:30
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 17, 2026 08:30
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 17, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ankitsinghsisodya
Once this PR has been reviewed and has the lgtm label, please assign dsimansk for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the size/XL 🤖 PR changes 500-999 lines, ignoring generated files. label May 17, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 17, 2026

Hi @Ankitsinghsisodya. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@knative-prow knative-prow Bot added the needs-ok-to-test 🤖 Needs an org member to approve testing label May 17, 2026
- Added JSON output support for various commands, ensuring structured responses when the --json flag is enabled.
- Updated error handling in commands like 'completion', 'mcp start', and 'tkn-tasks' to return appropriate error messages when JSON output is requested.
- Redirected non-error messages to stderr in commands such as 'deploy' and 'invoke' to prevent contamination of JSON output on stdout.
- Improved test cases to validate the new output behavior and error handling across commands.

These changes enhance the user experience by providing clearer error messages and maintaining output consistency across the CLI.
@knative-prow knative-prow Bot added size/XXL 🤖 PR changes 1000+ lines, ignoring generated files. and removed size/XL 🤖 PR changes 500-999 lines, ignoring generated files. labels May 17, 2026
- Updated the `invoke` command to return the response body directly as JSON when the `--json` flag is enabled, improving output consistency.
- Removed the internal alias for `writeJSONError` in the `json.go` file to streamline the codebase.
- Added documentation to multiple command reference files to include the `--json` option, ensuring users are aware of the new output format.

These changes enhance the user experience by providing structured JSON responses across various commands, facilitating easier parsing and integration.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 17, 2026

Codecov Report

❌ Patch coverage is 39.78102% with 330 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.14%. Comparing base (584c11e) to head (eadfb64).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
cmd/json.go 49.45% 165 Missing and 20 partials ⚠️
cmd/repository.go 12.90% 23 Missing and 4 partials ⚠️
cmd/deploy.go 30.43% 13 Missing and 3 partials ⚠️
cmd/build.go 7.69% 10 Missing and 2 partials ⚠️
pkg/app/app.go 0.00% 12 Missing ⚠️
cmd/delete.go 35.29% 8 Missing and 3 partials ⚠️
cmd/subscribe.go 9.09% 8 Missing and 2 partials ⚠️
cmd/invoke.go 0.00% 8 Missing and 1 partial ⚠️
cmd/create.go 11.11% 7 Missing and 1 partial ⚠️
cmd/describe.go 0.00% 6 Missing and 1 partial ⚠️
... and 9 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3774      +/-   ##
==========================================
- Coverage   57.03%   55.14%   -1.89%     
==========================================
  Files         182      183       +1     
  Lines       21376    21858     +482     
==========================================
- Hits        12191    12053     -138     
- Misses       7953     8558     +605     
- Partials     1232     1247      +15     
Flag Coverage Δ
e2e 22.08% <2.37%> (-13.73%) ⬇️
e2e go ?
e2e node ?
e2e python ?
e2e quarkus ?
e2e rust ?
e2e springboot ?
e2e typescript ?
e2e-config-ci 27.50% <2.46%> (-0.66%) ⬇️
integration 16.79% <0.00%> (-0.38%) ⬇️
unit macos-14 45.11% <41.03%> (+0.01%) ⬆️
unit macos-latest 45.11% <41.03%> (+0.01%) ⬆️
unit ubuntu-24.04-arm 45.29% <39.78%> (-0.05%) ⬇️
unit ubuntu-latest 46.03% <41.03%> (-0.01%) ⬇️
unit windows-latest 45.15% <41.03%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor Author

@lkingland Can you re run the failed test?

@lkingland
Copy link
Copy Markdown
Member

/ok-to-test

@knative-prow knative-prow Bot added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 18, 2026
@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor Author

/ok-to-test

Can you plz re run the failed CI sir?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/XXL 🤖 PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants