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:
@@ -15,7 +15,8 @@ import (
|
||||
)
|
||||
|
||||
// chunksFields enumerates the fields surfaced for `--json` discovery on
|
||||
// `search chunks`. Lists data.items[*] (SearchResult) fields.
|
||||
// `search chunks`. Filter applies to each SearchResult object in the bare
|
||||
// array.
|
||||
var chunksFields = []string{
|
||||
"id", "content", "knowledge_id", "chunk_index", "knowledge_title",
|
||||
"start_at", "end_at", "seq", "score", "match_type", "chunk_type",
|
||||
@@ -34,11 +35,6 @@ type ChunksOptions struct {
|
||||
NoKeyword bool
|
||||
}
|
||||
|
||||
// chunksResult is the typed payload emitted under data.items.
|
||||
type chunksResult struct {
|
||||
Items []*sdk.SearchResult `json:"items"`
|
||||
}
|
||||
|
||||
// ChunksService is the narrow SDK surface used by runChunks. *sdk.Client
|
||||
// satisfies it; tests inject fakes via Factory.Client.
|
||||
type ChunksService interface {
|
||||
@@ -145,11 +141,10 @@ func runChunks(ctx context.Context, opts *ChunksOptions, jopts *cmdutil.JSONOpti
|
||||
}
|
||||
|
||||
if jopts.Enabled() {
|
||||
return format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Success(chunksResult{Items: results}, &format.Meta{KBID: opts.KBID}),
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
if results == nil {
|
||||
results = []*sdk.SearchResult{}
|
||||
}
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, results, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
return renderChunkResults(results, opts.KBID)
|
||||
}
|
||||
|
||||
@@ -61,8 +61,10 @@ func TestRunSearch_JSONOutput(t *testing.T) {
|
||||
svc := &fakeChunksSvc{results: []*sdk.SearchResult{{Score: 0.9, Content: "x"}}}
|
||||
opts := &ChunksOptions{Query: "q", KBID: "kb1", Limit: 1}
|
||||
require.NoError(t, runChunks(context.Background(), opts, &cmdutil.JSONOptions{}, svc))
|
||||
assert.True(t, strings.HasPrefix(out.String(), `{"ok":true`), "got: %q", out.String())
|
||||
assert.Contains(t, out.String(), `"kb_id":"kb1"`)
|
||||
got := out.String()
|
||||
assert.True(t, strings.HasPrefix(strings.TrimSpace(got), "["), "expected bare JSON array, got: %q", got)
|
||||
assert.NotContains(t, got, `"ok":`)
|
||||
assert.Contains(t, got, `"score":0.9`)
|
||||
}
|
||||
|
||||
func TestRunSearch_EmptyResults(t *testing.T) {
|
||||
|
||||
+1
-10
@@ -39,11 +39,6 @@ type DocsSearchOptions struct {
|
||||
Limit int
|
||||
}
|
||||
|
||||
// docsResult is the typed payload emitted under data.items.
|
||||
type docsResult struct {
|
||||
Items []sdk.Knowledge `json:"items"`
|
||||
}
|
||||
|
||||
// DocsSearchService is the narrow SDK surface this command depends on.
|
||||
// Server has no fuzzy-document-name endpoint, so the CLI pages through
|
||||
// ListKnowledge and filters by Title / FileName client-side.
|
||||
@@ -125,11 +120,7 @@ done:
|
||||
if matches == nil {
|
||||
matches = []sdk.Knowledge{}
|
||||
}
|
||||
return format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Success(docsResult{Items: matches}, &format.Meta{KBID: opts.KBID}),
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, matches, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
fmt.Fprintln(iostreams.IO.Out, "(no matches)")
|
||||
|
||||
@@ -2,8 +2,8 @@ package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
||||
"github.com/Tencent/WeKnora/cli/internal/format"
|
||||
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
@@ -97,10 +96,10 @@ func TestDocsSearch_JSON(t *testing.T) {
|
||||
total: 1,
|
||||
}
|
||||
require.NoError(t, runDocsSearch(context.Background(), &DocsSearchOptions{Query: "match", KBID: "kb1", Limit: 20}, &cmdutil.JSONOptions{}, svc))
|
||||
var env format.Envelope
|
||||
require.NoError(t, json.Unmarshal(out.Bytes(), &env))
|
||||
require.True(t, env.OK)
|
||||
assert.Contains(t, out.String(), `"id":"d1"`)
|
||||
got := out.String()
|
||||
assert.True(t, strings.HasPrefix(strings.TrimSpace(got), "["), "expected bare JSON array, got: %q", got)
|
||||
assert.Contains(t, got, `"id":"d1"`)
|
||||
assert.NotContains(t, got, `"ok":`)
|
||||
}
|
||||
|
||||
func TestDocsSearch_NetworkError(t *testing.T) {
|
||||
|
||||
+1
-10
@@ -33,11 +33,6 @@ type KBSearchOptions struct {
|
||||
Limit int
|
||||
}
|
||||
|
||||
// kbSearchResult is the typed payload emitted under data.items.
|
||||
type kbSearchResult struct {
|
||||
Items []sdk.KnowledgeBase `json:"items"`
|
||||
}
|
||||
|
||||
// KBSearchService is the narrow SDK surface this command depends on.
|
||||
// Server has no fuzzy-KB-name endpoint; the CLI filters ListKnowledgeBases
|
||||
// client-side. Acceptable because tenants typically have ≪ 1000 KBs.
|
||||
@@ -96,11 +91,7 @@ func runKBSearch(ctx context.Context, opts *KBSearchOptions, jopts *cmdutil.JSON
|
||||
if matches == nil {
|
||||
matches = []sdk.KnowledgeBase{}
|
||||
}
|
||||
return format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Success(kbSearchResult{Items: matches}, nil),
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, matches, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
fmt.Fprintln(iostreams.IO.Out, "(no matches)")
|
||||
|
||||
@@ -2,7 +2,6 @@ package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -11,7 +10,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
||||
"github.com/Tencent/WeKnora/cli/internal/format"
|
||||
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
@@ -103,10 +101,10 @@ func TestKBSearch_JSON(t *testing.T) {
|
||||
svc := &fakeKBSearchSvc{items: []sdk.KnowledgeBase{{ID: "kb1", Name: "marketing"}}}
|
||||
require.NoError(t, runKBSearch(context.Background(), &KBSearchOptions{Query: "marketing", Limit: 20}, &cmdutil.JSONOptions{}, svc))
|
||||
|
||||
var env format.Envelope
|
||||
require.NoError(t, json.Unmarshal(out.Bytes(), &env))
|
||||
require.True(t, env.OK)
|
||||
assert.Contains(t, out.String(), `"id":"kb1"`)
|
||||
got := out.String()
|
||||
assert.True(t, strings.HasPrefix(strings.TrimSpace(got), "["), "expected bare JSON array, got: %q", got)
|
||||
assert.Contains(t, got, `"id":"kb1"`)
|
||||
assert.NotContains(t, got, `"ok":`)
|
||||
}
|
||||
|
||||
func TestKBSearch_NetworkError(t *testing.T) {
|
||||
|
||||
@@ -30,11 +30,6 @@ type SessionsSearchOptions struct {
|
||||
Limit int
|
||||
}
|
||||
|
||||
// sessionsSearchResult is the typed payload emitted under data.items.
|
||||
type sessionsSearchResult struct {
|
||||
Items []sdk.Session `json:"items"`
|
||||
}
|
||||
|
||||
// SessionsSearchService is the narrow SDK surface this command depends on.
|
||||
// Server has no session-search endpoint; CLI pages through and filters by
|
||||
// Title / Description client-side.
|
||||
@@ -105,11 +100,7 @@ done:
|
||||
if matches == nil {
|
||||
matches = []sdk.Session{}
|
||||
}
|
||||
return format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Success(sessionsSearchResult{Items: matches}, nil),
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
return format.WriteJSONFiltered(iostreams.IO.Out, matches, jopts.Fields, jopts.JQ)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
fmt.Fprintln(iostreams.IO.Out, "(no matches)")
|
||||
|
||||
Reference in New Issue
Block a user