refactor(cli): drop --dry-run + introduce bare-JSON output path
Two intertwined mainstream-alignment moves bundled because they share
the migration target (every command's --json path):
1. Drop --dry-run entirely. Survey of comparable API-wrapper CLIs
(gh, aws, stripe, lark): none expose --dry-run. The mainstream that
does (kubectl/git/helm/ansible) operates on declarative manifests
or local state where the preview is materially different from the
executed action. WeKnora's CLI just echoed the same parameters
that would have gone on the wire — the preview added no real
signal over `--help` + reading the call site. Removes:
- root --dry-run persistent flag + cmdutil/dryrun.go
- DryRun fields + EmitDryRun calls in 12 write commands
- format.Envelope.DryRun field
- 8 corresponding *_test.go cases
- --dry-run mention from README.md and CHANGELOG.md
- "dry_run":false from 16 golden envelopes
2. Migrate every --json output to bare data:
- New format.WriteJSON / WriteJSONFiltered helpers
(cli/internal/format/bare.go) share filterArrayItems /
filterObjectKeys / writeJQ with the (still-live for now) envelope
filter helpers.
- Read commands (kb/doc/session list+view, search chunks/docs/
sessions/kb, auth list/status, agent list/view, context list,
doctor) emit bare arrays / objects on stdout.
- Write commands (kb create/edit/delete/pin/empty, doc upload/
upload_recursive/delete, session delete, auth login/logout/
refresh/token, link/unlink, context add/use/remove, agent
invoke, chat, api, version) emit bare result objects. Risk
classification dropped — the resource + exit code already
convey the action.
Per-command shape changes:
list / search → []T (was {ok, data:{items:[…]}})
view → T (was {ok, data:T, _meta:…})
create / edit → T
delete / pin / etc. → {id, …action result…}
doctor → {summary, checks}
api → {status, headers, body}
_meta dropped on the read path:
pagination (page/page_size/total/has_more) — agents iterate with
--all-pages or accept --limit (gh CLI parity);
kb_id / context echo — caller already knows what it asked for.
Acceptance contract goldens regenerated for the new bare shape.
Error envelope on stdout (PrintErrorEnvelope) stays live for now —
the envelope-infra deletion lands in the next commit.
This commit is contained in:
@@ -103,10 +103,9 @@ func TestUpload_Success_JSON(t *testing.T) {
|
||||
require.NoError(t, runUpload(context.Background(), opts, &cmdutil.JSONOptions{}, svc, "kb_xxx", path))
|
||||
|
||||
got := out.String()
|
||||
assert.True(t, strings.HasPrefix(got, `{"ok":true`), "envelope should start with ok:true; got %q", got)
|
||||
assert.Contains(t, got, `"id":"doc_77"`)
|
||||
assert.True(t, strings.HasPrefix(strings.TrimSpace(got), `{"id":"doc_77"`), "expected bare Knowledge object; got %q", got)
|
||||
assert.Contains(t, got, `"file_name":"a.md"`)
|
||||
assert.Contains(t, got, `"kb_id":"kb_xxx"`, "_meta.kb_id should carry the resolved kb id")
|
||||
assert.NotContains(t, got, `"ok":`)
|
||||
}
|
||||
|
||||
func TestUpload_HTTPError_500(t *testing.T) {
|
||||
@@ -193,27 +192,16 @@ func TestUploadFromURL_WithName_Passes_AsFileName(t *testing.T) {
|
||||
"--name must be forwarded as FileName (server uses it for file-vs-crawl mode hint)")
|
||||
}
|
||||
|
||||
func TestUploadFromURL_JSON_Envelope(t *testing.T) {
|
||||
func TestUploadFromURL_JSON_BareObject(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
svc := &fakeUploadSvc{urlResp: &sdk.Knowledge{ID: "doc_url_3", FileName: "ok.pdf"}}
|
||||
jopts := &cmdutil.JSONOptions{}
|
||||
require.NoError(t, runUploadFromURL(context.Background(),
|
||||
&UploadOptions{FromURL: "https://example.com/ok.pdf"}, jopts, svc, "kb_xxx"))
|
||||
assert.Contains(t, out.String(), `"ok":true`)
|
||||
assert.Contains(t, out.String(), `"id":"doc_url_3"`)
|
||||
assert.Contains(t, out.String(), `"risk":{"level":"write"`)
|
||||
}
|
||||
|
||||
func TestUploadFromURL_DryRun(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
svc := &fakeUploadSvc{} // must not be called
|
||||
jopts := &cmdutil.JSONOptions{}
|
||||
opts := &UploadOptions{FromURL: "https://example.com/x.pdf", DryRun: true}
|
||||
require.NoError(t, runUploadFromURL(context.Background(), opts, jopts, svc, "kb_xxx"))
|
||||
got := out.String()
|
||||
assert.Contains(t, got, `"dry_run":true`)
|
||||
assert.Contains(t, got, `"from_url"`)
|
||||
assert.Empty(t, svc.got.urlReq.URL, "SDK call must NOT fire on --dry-run")
|
||||
assert.Contains(t, got, `"id":"doc_url_3"`)
|
||||
assert.NotContains(t, got, `"ok":`)
|
||||
assert.NotContains(t, got, `"risk":`)
|
||||
}
|
||||
|
||||
func TestUploadFromURL_DuplicateURLMaps_resource_already_exists(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user