Skip to content

Add --json to remaining delete / utility commands#69

Merged
austin-denoble merged 2 commits intomainfrom
adenoble/add-json-delete-utility-commands
Mar 18, 2026
Merged

Add --json to remaining delete / utility commands#69
austin-denoble merged 2 commits intomainfrom
adenoble/add-json-delete-utility-commands

Conversation

@austin-denoble
Copy link
Collaborator

@austin-denoble austin-denoble commented Mar 16, 2026

Problem

There are a number of commands that currently either don't support --json, or the flag is used improperly, and there's still non-JSON output going through stdout. This PR tackles adding --json to existing commands where it's not supported, and constructing coherent JSON payloads for the output in these situations.

In some cases like delete and login commands where there's no explicit returned struct from the SDK, we fabricate a minimal response struct inline to allow for returning structured, relevant data from the command. This is important for agentic contexts where informative output from commands is essential.

Solution

  • Added -j / --json flag to all remaining delete commands: pc index delete, pc collection delete, pc backup delete, pc index namespace delete, and pc apiKey delete. Each emits a consistent delete envelope to stdout: {"deleted": true, <identity fields>}, where identity fields are the natural key(s) for that resource (name, id, namespace/index, etc.).
  • Added -j / --json flag to utility commands: pc logout / pc auth logout emit {"status": "logged_out"}; pc whoami / pc auth whoami emit {"email": "...", "organization_id": "..."} from parsed JWT claims (raw token is never exposed); pc version emits {"version": "...", "sha": "...", "built": "..."}.
  • pc apiKey delete --json automatically skips the interactive confirmation prompt, since agents cannot respond to stdin prompts.
  • pc index delete and pc collection delete were refactored to extract a run*Cmd function (matching the pattern used by backup, namespace, and other commands), making them independently testable without going through Cobra.
  • JSON output paths in pc backup create, pc backup describe, and pc backup list were switched from pcio.Println to fmt.Println directly, consistent with the new delete commands and the direction of SDK-700. This also unblocked testing those paths, since pcio is silenced by TestMain via testutils.SilenceOutput().
  • backup_test.go was split into focused per-command test files (create_test.go, describe_test.go, list_test.go, delete_test.go), with backup_test.go retaining only the shared mock and TestMain.
  • testutils.CaptureStdout was added as a shared test helper (alongside the existing SilenceOutput) to avoid duplicating the os.Pipe stdout-capture pattern across every package.
  • All SucceedsJSON tests capture actual stdout output and assert on it: assert.JSONEq for our own delete envelope shapes (exact match, key-order independent); assert.Contains for SDK-typed responses where the full serialization shape is owned externally.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

For testing, the specific focus should be around using the --json flag with commands and validating the output.

Key things to test:

# Make sure confirmation prompt is skipped, and output is well-formed.
$ pc api-key delete --json

# confirm email and organization_id appear, make sure we're not emitting the raw token
$ pc whoami --json

# confirm credentials cleared and the JSON emits correctly
$ pc logout --json

# version data properly emitted
$ pc version --json

# any delete commands with JSON should now return output
$ pc index delete --json --name index-name

Local testing output:

./dist/pc_darwin_all/pc version --json
{
    "version": "0.3.1-SNAPSHOT-86b9e4d",
    "sha": "86b9e4d61a3af778130cf19e133e12cb43f2061c",
    "built": "2026-03-16T21:30:49Z"
}
./dist/pc_darwin_all/pc whoami --json
{
    "email": "austin.d@pinecone.io",
    "organization_id": "-NcToA8ZVfmGbuUWQe9L"
}
./dist/pc_darwin_all/pc api-key delete --id 5720ec6e-4153-43ef-843f-aab3c865b1d2 --json
{
    "deleted": true,
    "name": "test-cli-2",
    "id": "5720ec6e-4153-43ef-843f-aab3c865b1d2"
}

Note

Low Risk
Low risk additive CLI flags and output modes; main risk is minor behavior/output changes for scripts if they relied on exact stdout text or quiet-mode pcio behavior.

Overview
Adds -j/--json output to the remaining delete/utility commands (api-key delete, backup delete, collection delete, index delete, index namespace delete, logout, whoami, version), emitting small structured payloads (e.g., {"deleted":true,...} / {"status":"logged_out"}).

Refactors index delete and collection delete to route through testable run* helpers, and updates several backup commands’ JSON paths to print via fmt.Println so JSON reliably reaches stdout even when pcio is silenced.

Splits backup command tests into per-command files and adds testutils.CaptureStdout to assert JSON output in tests.

Written by Cursor Bugbot for commit 7cc662c. This will update automatically on new commits. Configure here.

…gents consuming CLI output had no to way to programmatically confirm the outcome of delete operations or identity/version queries

fmt.Printf("Version: %s\n", build.Version)
fmt.Printf("SHA: %s\n", build.Commit)
fmt.Printf("Built: %s\n", build.Date)
Copy link

Choose a reason for hiding this comment

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

Version non-JSON output bypasses quiet mode

Medium Severity

The non-JSON output path was changed from pcio.Printf to fmt.Printf, which means pc version --quiet (or -q) no longer suppresses output. The global --quiet flag works by calling pcio.SetQuiet(true) in the root command's PersistentPreRun, and only pcio.* functions honor that setting. Other commands in the codebase (e.g., apiKey/list.go, target/target.go) still use pcio.Printf for non-JSON stdout output. The JSON path correctly uses fmt.Println (explicit opt-in output), but the plain-text fallback needs to remain pcio.Printf to preserve quiet-mode behavior.

Fix in Cursor Fix in Web

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

if options.json {
fmt.Println(text.IndentJSON(struct {
Email string `json:"email"`
OrgId string `json:"organization_id"`
Copy link

Choose a reason for hiding this comment

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

JSON field name doesn't match documented spec

Medium Severity

The inline struct uses json:"organization_id" but the PR specification and local testing output both show the field name as org_id. The actual JSON output will contain "organization_id" instead of "org_id", breaking the documented contract that agentic consumers would rely on. Both whoami/whoami.go and auth/whoami.go have the same mismatch.

Additional Locations (1)
Fix in Cursor Fix in Web

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated to "organization_id" intentionally. I've updated the PR description, testing, and examples.

@austin-denoble austin-denoble merged commit e3ae144 into main Mar 18, 2026
8 checks passed
@austin-denoble austin-denoble deleted the adenoble/add-json-delete-utility-commands branch March 18, 2026 14:46
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