feat: add --json output mode to all func subcommands#3774
feat: add --json output mode to all func subcommands#3774Ankitsinghsisodya wants to merge 3 commits into
Conversation
- 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.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Ankitsinghsisodya The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
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 Tip We noticed you've done this a few times! Consider joining the org to skip this step and gain Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
- 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.
- 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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@lkingland Can you re run the failed test? |
|
/ok-to-test |
Can you plz re run the failed CI sir? |
Summary
Implements structured JSON output for every
funcsubcommand, 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— sharedJSONResponse/JSONErrorstructs,WriteJSONSuccess,WriteJSONError,errorToJSONError(maps 14+ typed errors to category/code/retryable), andisJSONEnabledhelpercmd/json_test.go— 20 unit tests covering every error-type mapping, envelope shape,apiVersionpresence, and round-trip validityGlobal plumbing
cmd/root.go—--jsonregistered as a persistent flag on the root command (inheritable by all subcommands), bound to Viperpkg/app/app.go— top-level error sink writesWriteJSONErrorto stdout when--jsonis set, instead of plain text to stderrPer-command success payloads
datafieldsbuildname,imagedeployname,namespace,url,imagecreatename,path,runtime,templatedeletename,namespacedescribefn.Instanceobject (reusesFormatter.JSON())listlistItems(reusesFormatter.JSON())runaddress,host,port(upgrades existing ad-hoc JSON to envelope)invokeresponse(HTTP body)versionVersionstructlanguagestemplatesenvironmentEnvironmentstructsubscribename,source,filtersrepository list{name, url}repository add/rename/removelogs--jsonis setUpdated tests
cmd/languages_test.go— updatedTestLanguages_JSONto assert envelope shapecmd/templates_test.go— updatedTestTemplates_JSONandTestTemplates_ByLanguageto assert envelope shapeTest plan
go test knative.dev/func/cmd -run "TestWriteJSON|TestAPIVersion|TestErrorToJSONError|TestRound|TestLanguages_JSON|TestTemplates_JSON|TestTemplates_ByLanguage|TestRepository_List"— all 26 passfunc create -l go myfunc --json→ envelope withstatus: okanddata.name,data.runtimefunc build --json→ envelope withdata.name,data.imagefunc deploy --json→ envelope withdata.name,data.namespacefunc list --json→ envelope with array indatafunc logs --json→ returns error: --json is not supported for streaming commands--json→status: errorwithcategory,code,retryable