feat(cli): --json field-select, --jq, auth token, doc --from-url

Output ergonomics:
- `--json` accepts a comma-separated field list (gh-parity); selects
  named keys from the per-command payload. Bare `--json` keeps the
  full shape.
- `--jq <expr>` evaluates a gojq expression over the JSON; pairs with
  `--json field-list` so projection runs before jq.
- `--version` is a global cobra flag in addition to the `version`
  subcommand; both render the same line.
- Per-command `--help` now renders the available JSON field list under
  "JSON fields available via `--json id,name,...`" (field-discovery
  parity with gh / kubectl `-o jsonpath`).

New commands:
- `auth token` — print the active context's credential to stdout for
  shell command substitution (`WEKNORA_TOKEN=$(weknora auth token)`).
  Default: raw secret, no trailing newline. `--json` emits
  `{token, mode, context}`.
- `doc upload --from-url <URL>` — ingest a remote URL via the SDK
  `CreateKnowledgeFromURL`. `--name` forwarded as `FileName` so the
  server's known-extension heuristic upgrades crawl-mode to
  file-download-mode where appropriate.

Includes the simplify post-review polish pass (field-filter unit
tests, --json/--jq compose check, agent_help copy fixes).
This commit is contained in:
nullkey
2026-05-14 23:28:14 +08:00
committed by lyingbug
parent 4fb089d4d7
commit 1b20b06f5e
81 changed files with 2351 additions and 642 deletions
+18 -11
View File
@@ -13,10 +13,13 @@ import (
"github.com/Tencent/WeKnora/cli/internal/prompt"
)
// docDeleteFields enumerates the fields surfaced for `--json` discovery on
// `doc delete`. The result payload is a small {id, deleted} object.
var docDeleteFields = []string{"id", "deleted"}
type DeleteOptions struct {
Yes bool // sourced from the global -y/--yes persistent flag (see cli/cmd/root.go)
JSONOut bool
DryRun bool
Yes bool // sourced from the global -y/--yes persistent flag (see cli/cmd/root.go)
DryRun bool
}
// DeleteService is the narrow SDK surface this command depends on.
@@ -50,31 +53,35 @@ without the user's explicit go-ahead.`,
weknora doc delete doc_abc -y --json # envelope output`,
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
jopts, err := cmdutil.CheckJSONFlags(c)
if err != nil {
return err
}
opts.Yes, _ = c.Flags().GetBool("yes")
opts.DryRun = cmdutil.IsDryRun(c)
if opts.DryRun {
return runDelete(c.Context(), opts, nil, f.Prompter(), args[0])
return runDelete(c.Context(), opts, jopts, nil, f.Prompter(), args[0])
}
cli, err := f.Client()
if err != nil {
return err
}
return runDelete(c.Context(), opts, cli, f.Prompter(), args[0])
return runDelete(c.Context(), opts, jopts, cli, f.Prompter(), args[0])
},
}
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
cmdutil.AddJSONFlags(cmd, docDeleteFields)
agent.SetAgentHelp(cmd, "Destructively deletes one document by id. ALWAYS pass -y/--yes in agent mode (no TTY ⇒ confirm prompt fails). Returns data: {id, deleted:true}.")
return cmd
}
func runDelete(ctx context.Context, opts *DeleteOptions, svc DeleteService, p prompt.Prompter, id string) error {
func runDelete(ctx context.Context, opts *DeleteOptions, jopts *cmdutil.JSONOptions, svc DeleteService, p prompt.Prompter, id string) error {
if opts.DryRun {
return cmdutil.EmitDryRun(opts.JSONOut,
return cmdutil.EmitDryRun(jopts.Enabled(),
deleteResult{ID: id, Deleted: false}, nil,
&format.Risk{Level: format.RiskHighRiskWrite, Action: fmt.Sprintf("delete document %s", id)})
}
if err := cmdutil.ConfirmDestructive(p, opts.Yes, opts.JSONOut, "document", id); err != nil {
if err := cmdutil.ConfirmDestructive(p, opts.Yes, jopts.Enabled(), "document", id); err != nil {
return err
}
@@ -82,9 +89,9 @@ func runDelete(ctx context.Context, opts *DeleteOptions, svc DeleteService, p pr
return cmdutil.WrapHTTP(err, "delete document %s", id)
}
if opts.JSONOut {
if jopts.Enabled() {
risk := &format.Risk{Level: format.RiskHighRiskWrite, Action: fmt.Sprintf("deleted document %s", id)}
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(deleteResult{ID: id, Deleted: true}, nil, risk))
return format.WriteEnvelopeFiltered(iostreams.IO.Out, format.SuccessWithRisk(deleteResult{ID: id, Deleted: true}, nil, risk), jopts.Fields, jopts.JQ)
}
fmt.Fprintf(iostreams.IO.Out, "✓ Deleted document %s\n", id)
return nil
+9 -9
View File
@@ -49,7 +49,7 @@ func TestDelete_Success_WithForce(t *testing.T) {
opts := &DeleteOptions{Yes: true}
// Force=true short-circuits the confirm path; the prompter must not be
// consulted, so any value works.
require.NoError(t, runDelete(context.Background(), opts, svc, scriptedConfirm{confirmReturn: false}, "doc_abc"))
require.NoError(t, runDelete(context.Background(), opts, nil, svc, scriptedConfirm{confirmReturn: false}, "doc_abc"))
assert.Equal(t, "doc_abc", svc.got)
assert.Equal(t, 1, svc.calls)
@@ -60,8 +60,8 @@ func TestDelete_Success_WithForce(t *testing.T) {
func TestDelete_Success_JSON(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeDeleteSvc{}
opts := &DeleteOptions{Yes: true, JSONOut: true}
require.NoError(t, runDelete(context.Background(), opts, svc, scriptedConfirm{confirmReturn: true}, "doc_abc"))
opts := &DeleteOptions{Yes: true}
require.NoError(t, runDelete(context.Background(), opts, &cmdutil.JSONOptions{}, svc, scriptedConfirm{confirmReturn: true}, "doc_abc"))
got := out.String()
assert.True(t, strings.HasPrefix(got, `{"ok":true`), "envelope should start with ok:true; got %q", got)
@@ -72,7 +72,7 @@ func TestDelete_Success_JSON(t *testing.T) {
func TestDelete_NotFound_404(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeDeleteSvc{err: errors.New("HTTP error 404: not found")}
err := runDelete(context.Background(), &DeleteOptions{Yes: true}, svc, scriptedConfirm{}, "doc_missing")
err := runDelete(context.Background(), &DeleteOptions{Yes: true}, nil, svc, scriptedConfirm{}, "doc_missing")
require.Error(t, err)
var typed *cmdutil.Error
@@ -83,7 +83,7 @@ func TestDelete_NotFound_404(t *testing.T) {
func TestDelete_HTTPError_500(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeDeleteSvc{err: errors.New("HTTP error 500: internal")}
err := runDelete(context.Background(), &DeleteOptions{Yes: true}, svc, scriptedConfirm{}, "doc_x")
err := runDelete(context.Background(), &DeleteOptions{Yes: true}, nil, svc, scriptedConfirm{}, "doc_x")
require.Error(t, err)
var typed *cmdutil.Error
@@ -94,7 +94,7 @@ func TestDelete_HTTPError_500(t *testing.T) {
func TestDelete_ConfirmYes(t *testing.T) {
out, _ := iostreams.SetForTestWithTTY(t)
svc := &fakeDeleteSvc{}
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, svc, scriptedConfirm{confirmReturn: true}, "doc_abc")
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, nil, svc, scriptedConfirm{confirmReturn: true}, "doc_abc")
require.NoError(t, err)
assert.Equal(t, 1, svc.calls, "user said yes ⇒ delete proceeds")
assert.Contains(t, out.String(), "✓")
@@ -103,7 +103,7 @@ func TestDelete_ConfirmYes(t *testing.T) {
func TestDelete_ConfirmNo(t *testing.T) {
_, errBuf := iostreams.SetForTestWithTTY(t)
svc := &fakeDeleteSvc{}
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, svc, scriptedConfirm{confirmReturn: false}, "doc_abc")
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, nil, svc, scriptedConfirm{confirmReturn: false}, "doc_abc")
require.Error(t, err)
assert.Equal(t, 0, svc.calls, "user said no ⇒ SDK must NOT be called")
@@ -119,7 +119,7 @@ func TestDelete_ConfirmNo(t *testing.T) {
func TestDelete_AgentPrompterErrors(t *testing.T) {
_, _ = iostreams.SetForTestWithTTY(t)
svc := &fakeDeleteSvc{}
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, svc, errPrompter{}, "doc_abc")
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, nil, svc, errPrompter{}, "doc_abc")
require.Error(t, err)
assert.Equal(t, 0, svc.calls)
@@ -135,7 +135,7 @@ func TestDelete_AgentPrompterErrors(t *testing.T) {
func TestDelete_NoYes_NonTTY_RequiresConfirmation(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeDeleteSvc{}
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, svc, errPrompter{}, "doc_abc")
err := runDelete(context.Background(), &DeleteOptions{Yes: false}, nil, svc, errPrompter{}, "doc_abc")
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
+20 -6
View File
@@ -17,10 +17,20 @@ import (
sdk "github.com/Tencent/WeKnora/client"
)
// docListFields enumerates the fields surfaced for `--json` discovery on
// `doc list`. These are the per-item (Knowledge) fields, not the envelope
// wrappers (items/page/total/kb_id) — filtering applies to data.items[*].
var docListFields = []string{
"id", "knowledge_base_id", "tag_id", "type", "title", "description",
"source", "channel", "parse_status", "summary_status", "enable_status",
"embedding_model_id", "file_name", "file_type", "file_size", "file_hash",
"file_path", "storage_size",
"created_at", "updated_at", "processed_at", "error_message",
}
type ListOptions struct {
Page int
PageSize int
JSONOut bool
}
// ListService is the narrow SDK surface this command depends on.
@@ -61,6 +71,10 @@ backend storage order is not guaranteed and varies between deployments.`,
weknora doc list --page 2 --json # paginated envelope output`,
Args: cobra.NoArgs,
RunE: func(c *cobra.Command, _ []string) error {
jopts, err := cmdutil.CheckJSONFlags(c)
if err != nil {
return err
}
kbID, err := f.ResolveKB(c)
if err != nil {
return err
@@ -69,7 +83,7 @@ backend storage order is not guaranteed and varies between deployments.`,
if err != nil {
return err
}
return runList(c.Context(), opts, cli, kbID)
return runList(c.Context(), opts, jopts, cli, kbID)
},
}
// --kb is read by Factory.ResolveKB; declare it here so cobra parses the
@@ -77,12 +91,12 @@ backend storage order is not guaranteed and varies between deployments.`,
cmd.Flags().String("kb", "", "Knowledge base UUID or name (overrides env / project link)")
cmd.Flags().IntVar(&opts.Page, "page", 1, "Page number (1-based)")
cmd.Flags().IntVar(&opts.PageSize, "page-size", 20, "Items per page (1..1000)")
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
cmdutil.AddJSONFlags(cmd, docListFields)
agent.SetAgentHelp(cmd, "Lists docs in the resolved KB. Returns data: {items, page, page_size, total, kb_id}; pass --kb when not running inside a project.")
return cmd
}
func runList(ctx context.Context, opts *ListOptions, svc ListService, kbID string) error {
func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions, svc ListService, kbID string) error {
if opts.Page < 1 {
return &cmdutil.Error{
Code: cmdutil.CodeInputInvalidArgument,
@@ -116,8 +130,8 @@ func runList(ctx context.Context, opts *ListOptions, svc ListService, kbID strin
Total: total,
KBID: kbID,
}
if opts.JSONOut {
return format.WriteEnvelope(iostreams.IO.Out, format.Success(r, &format.Meta{KBID: kbID}))
if jopts.Enabled() {
return format.WriteEnvelopeFiltered(iostreams.IO.Out, format.Success(r, &format.Meta{KBID: kbID}), jopts.Fields, jopts.JQ)
}
if len(items) == 0 {
+8 -8
View File
@@ -56,7 +56,7 @@ func TestList_Success_Human(t *testing.T) {
}
svc := &fakeListSvc{items: items, total: 2}
opts := &ListOptions{Page: 1, PageSize: 20}
require.NoError(t, runList(context.Background(), opts, svc, "kb_xxx"))
require.NoError(t, runList(context.Background(), opts, nil, svc, "kb_xxx"))
assert.Equal(t, "kb_xxx", svc.got.kbID)
assert.Equal(t, 1, svc.got.page)
@@ -74,8 +74,8 @@ func TestList_Success_Human(t *testing.T) {
func TestList_Success_JSON(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeListSvc{items: []sdk.Knowledge{{ID: "doc1", FileName: "a.pdf"}}, total: 1}
opts := &ListOptions{Page: 1, PageSize: 20, JSONOut: true}
require.NoError(t, runList(context.Background(), opts, svc, "kb_xxx"))
opts := &ListOptions{Page: 1, PageSize: 20}
require.NoError(t, runList(context.Background(), opts, &cmdutil.JSONOptions{}, svc, "kb_xxx"))
got := out.String()
assert.True(t, strings.HasPrefix(got, `{"ok":true`), "envelope should start with ok:true; got %q", got)
@@ -91,15 +91,15 @@ func TestList_Empty_Human(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeListSvc{items: nil, total: 0}
opts := &ListOptions{Page: 1, PageSize: 20}
require.NoError(t, runList(context.Background(), opts, svc, "kb_xxx"))
require.NoError(t, runList(context.Background(), opts, nil, svc, "kb_xxx"))
assert.Contains(t, out.String(), "(no documents)")
}
func TestList_Empty_JSON(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeListSvc{items: nil, total: 0}
opts := &ListOptions{Page: 1, PageSize: 20, JSONOut: true}
require.NoError(t, runList(context.Background(), opts, svc, "kb_xxx"))
opts := &ListOptions{Page: 1, PageSize: 20}
require.NoError(t, runList(context.Background(), opts, &cmdutil.JSONOptions{}, svc, "kb_xxx"))
got := out.String()
assert.Contains(t, got, `"items":[]`, "items must serialize as [] not null")
@@ -110,7 +110,7 @@ func TestList_HTTPError_500(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeListSvc{err: errors.New("HTTP error 500: internal")}
opts := &ListOptions{Page: 1, PageSize: 20}
err := runList(context.Background(), opts, svc, "kb_xxx")
err := runList(context.Background(), opts, nil, svc, "kb_xxx")
require.Error(t, err)
var typed *cmdutil.Error
@@ -188,7 +188,7 @@ func TestList_SortByUpdatedDesc(t *testing.T) {
{ID: "new", FileName: "new.pdf", UpdatedAt: now.Add(-1 * time.Hour)},
}
svc := &fakeListSvc{items: items, total: 2}
require.NoError(t, runList(context.Background(), &ListOptions{Page: 1, PageSize: 20}, svc, "kb_xxx"))
require.NoError(t, runList(context.Background(), &ListOptions{Page: 1, PageSize: 20}, nil, svc, "kb_xxx"))
got := out.String()
newIdx := strings.Index(got, "new.pdf")
+133 -38
View File
@@ -2,6 +2,7 @@ package doc
import (
"context"
"errors"
"fmt"
"os"
@@ -19,11 +20,22 @@ import (
// and "wechat" (mini-program). The server uses this only for analytics.
const uploadChannel = "api"
// docUploadFields enumerates the fields surfaced for `--json` discovery on
// `doc upload`. The single-file upload result is the full Knowledge struct;
// these are its top-level json tags.
var docUploadFields = []string{
"id", "knowledge_base_id", "tag_id", "type", "title", "description",
"source", "channel", "parse_status", "summary_status", "enable_status",
"embedding_model_id", "file_name", "file_type", "file_size", "file_hash",
"file_path", "storage_size",
"created_at", "updated_at", "processed_at", "error_message",
}
type UploadOptions struct {
Name string
Recursive bool // --recursive: positional arg is a directory; walk + upload each match
Glob string // --glob: filename pattern under --recursive (default "*")
JSONOut bool
FromURL string // --from-url: ingest a remote URL via SDK CreateKnowledgeFromURL
DryRun bool
}
@@ -37,6 +49,11 @@ type UploadService interface {
enableMultimodel *bool,
customFileName, channel string,
) (*sdk.Knowledge, error)
CreateKnowledgeFromURL(
ctx context.Context,
kbID string,
req sdk.CreateKnowledgeFromURLRequest,
) (*sdk.Knowledge, error)
}
// NewCmdUpload builds `weknora doc upload <file>`.
@@ -59,47 +76,138 @@ Use --recursive --glob to upload a directory tree (see Examples).`,
weknora doc upload notes.md --kb a32a63ff-fb36-4874-bcaa-30f48570a694
weknora doc upload notes.md --kb my-kb
weknora doc upload q3.pdf --name "Q3 Marketing Report.pdf"
weknora doc upload ./docs --recursive --glob '*.pdf'`,
Args: cobra.ExactArgs(1),
weknora doc upload ./docs --recursive --glob '*.pdf'
weknora doc upload --from-url https://example.com/whitepaper.pdf
weknora doc upload --from-url https://example.com/article.html --name "Q3 Article"`,
Args: cobra.MaximumNArgs(1),
RunE: func(c *cobra.Command, args []string) error {
path := args[0]
jopts, err := cmdutil.CheckJSONFlags(c)
if err != nil {
return err
}
opts.DryRun = cmdutil.IsDryRun(c)
if err := validateUploadFlags(opts, args); err != nil {
return err
}
kbID, err := f.ResolveKB(c)
if err != nil {
return err
}
if opts.Recursive {
if opts.DryRun {
return runUploadRecursive(c.Context(), opts, nil, kbID, path)
}
cli, err := f.Client()
// Resolve the SDK client once; dry-run paths take nil and never
// dereference it. Hoisting avoids three near-identical
// `cli, err := f.Client()` blocks across the dispatch branches.
var cli UploadService
if !opts.DryRun {
sdkCli, err := f.Client()
if err != nil {
return err
}
return runUploadRecursive(c.Context(), opts, cli, kbID, path)
cli = sdkCli
}
if err := validateUploadPath(path); err != nil {
return err
switch {
case opts.FromURL != "":
return runUploadFromURL(c.Context(), opts, jopts, cli, kbID)
case opts.Recursive:
return runUploadRecursive(c.Context(), opts, jopts, cli, kbID, args[0])
default:
if err := validateUploadPath(args[0]); err != nil {
return err
}
return runUpload(c.Context(), opts, jopts, cli, kbID, args[0])
}
if opts.DryRun {
return runUpload(c.Context(), opts, nil, kbID, path)
}
cli, err := f.Client()
if err != nil {
return err
}
return runUpload(c.Context(), opts, cli, kbID, path)
},
}
cmd.Flags().String("kb", "", "Knowledge base UUID or name (overrides env / project link)")
cmd.Flags().StringVar(&opts.Name, "name", "", "Custom file name to record (defaults to base name)")
cmd.Flags().BoolVar(&opts.Recursive, "recursive", false, "Treat the positional argument as a directory to walk")
cmd.Flags().StringVar(&opts.Glob, "glob", "*", "Filename pattern to filter when --recursive (e.g. '*.pdf')")
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
agent.SetAgentHelp(cmd, "Uploads to the resolved KB. Default: one regular file (refuses dir/non-regular). With --recursive: walks the directory, filters by --glob, uploads each match sequentially with per-file OK/FAIL output. Exit 1 on any per-file failure, exit 0 only on full success. Returns data: single Knowledge object (default) or aggregate report (recursive).")
cmd.Flags().StringVar(&opts.FromURL, "from-url", "", "Ingest a remote `URL` (HTTP/HTTPS) instead of a local file")
cmdutil.AddJSONFlags(cmd, docUploadFields)
agent.SetAgentHelp(cmd, "Uploads to the resolved KB. Default: one local regular file. With --recursive: walks the directory, filters by --glob, uploads each match. With --from-url: ingests a remote URL (mutually exclusive with the positional file / --recursive / --glob). Returns data: single Knowledge object (file or URL) or aggregate report (recursive). Errors: resource.already_exists when the URL was previously ingested.")
return cmd
}
// validateUploadFlags enforces mutual exclusion between the three input
// modes (positional file path / --recursive directory walk / --from-url
// remote ingest) and validates the URL when --from-url is set.
func validateUploadFlags(opts *UploadOptions, args []string) error {
hasPath := len(args) == 1
hasURL := opts.FromURL != ""
if hasURL {
if hasPath {
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
"cannot pass a file path with --from-url; choose one input mode")
}
if opts.Recursive {
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
"--recursive cannot be combined with --from-url")
}
return cmdutil.ValidateHTTPURL("--from-url", opts.FromURL)
}
if !hasPath {
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
"a file path is required (or pass --from-url)")
}
return nil
}
// runUploadFromURL ingests a remote URL via SDK CreateKnowledgeFromURL.
// `--name` becomes the FileName hint so the server's "known file extension"
// detection upgrades crawl-mode to file-download-mode when appropriate.
func runUploadFromURL(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID string) error {
if opts.DryRun {
return cmdutil.EmitDryRun(jopts.Enabled(),
map[string]string{"from_url": opts.FromURL, "kb_id": kbID, "name": opts.Name},
&format.Meta{KBID: kbID},
&format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("ingest URL %s into kb %s", opts.FromURL, kbID)})
}
req := sdk.CreateKnowledgeFromURLRequest{
URL: opts.FromURL,
FileName: opts.Name,
Channel: uploadChannel,
}
k, err := svc.CreateKnowledgeFromURL(ctx, kbID, req)
if err != nil {
if errors.Is(err, sdk.ErrDuplicateURL) {
// Server returns 409 with the existing knowledge entry's data.
// Surface as resource.already_exists; the data payload (if any)
// is observable via err's wrap chain — but the typed code is
// what agents branch on.
return cmdutil.Wrapf(cmdutil.CodeResourceAlreadyExists, err,
"URL already ingested into this knowledge base")
}
return cmdutil.WrapHTTP(err, "ingest URL %s", opts.FromURL)
}
return printUploadSuccess(k, jopts, kbID, "ingested", "Ingested", opts.FromURL, opts.Name, opts.FromURL)
}
// printUploadSuccess emits the post-upload result envelope (--json path)
// or the human checkmark line. Shared by single-file upload and URL ingest;
// the verb (upload/ingest), risk-action verb (uploaded/ingested), and the
// fallback display source (local path vs source URL) are the only varying
// pieces.
func printUploadSuccess(k *sdk.Knowledge, jopts *cmdutil.JSONOptions, kbID, riskVerb, humanVerb, source, customName, fallbackDisplay string) error {
if jopts.Enabled() {
risk := &format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("%s %s", riskVerb, source)}
return format.WriteEnvelopeFiltered(iostreams.IO.Out,
format.SuccessWithRisk(k, &format.Meta{KBID: kbID}, risk),
jopts.Fields, jopts.JQ)
}
displayed := customName
if displayed == "" {
displayed = k.FileName
}
if displayed == "" {
displayed = fallbackDisplay
}
fmt.Fprintf(iostreams.IO.Out, "✓ %s %q (id: %s)\n", humanVerb, displayed, k.ID)
return nil
}
// validateUploadPath checks that path exists and refers to a regular file.
// Symlinks and directories are rejected up-front so users get a typed error
// instead of an opaque SDK failure mid-upload. os.Stat (not Lstat) is used
@@ -121,9 +229,9 @@ func validateUploadPath(path string) error {
return nil
}
func runUpload(ctx context.Context, opts *UploadOptions, svc UploadService, kbID, path string) error {
func runUpload(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID, path string) error {
if opts.DryRun {
return cmdutil.EmitDryRun(opts.JSONOut,
return cmdutil.EmitDryRun(jopts.Enabled(),
map[string]string{"file": path, "kb_id": kbID, "name": opts.Name},
&format.Meta{KBID: kbID},
&format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("upload %s to kb %s", path, kbID)})
@@ -133,18 +241,5 @@ func runUpload(ctx context.Context, opts *UploadOptions, svc UploadService, kbID
if err != nil {
return cmdutil.WrapHTTP(err, "upload %s", path)
}
if opts.JSONOut {
risk := &format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("uploaded %s", path)}
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(k, &format.Meta{KBID: kbID}, risk))
}
displayed := opts.Name
if displayed == "" {
displayed = k.FileName
}
if displayed == "" {
displayed = path
}
fmt.Fprintf(iostreams.IO.Out, "✓ Uploaded %q (id: %s)\n", displayed, k.ID)
return nil
return printUploadSuccess(k, jopts, kbID, "uploaded", "Uploaded", path, opts.Name, path)
}
+11 -11
View File
@@ -25,7 +25,7 @@ type uploadOutcome struct {
// in one run. Exit semantics: nil error on full success, a typed *cmdutil.Error
// when ≥1 file failed (the typed code mirrors the first failure's
// classification so callers can still branch).
func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadService, kbID, dir string) error {
func runUploadRecursive(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID, dir string) error {
if opts.Name != "" {
return &cmdutil.Error{
Code: cmdutil.CodeInputInvalidArgument,
@@ -62,9 +62,9 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
return cmdutil.Wrapf(cmdutil.CodeLocalFileIO, err, "walk %s", dir)
}
if len(matches) == 0 {
if opts.JSONOut {
return format.WriteEnvelope(iostreams.IO.Out, format.Success(
recursiveResult{KBID: kbID}, &format.Meta{KBID: kbID}))
if jopts.Enabled() {
return format.WriteEnvelopeFiltered(iostreams.IO.Out, format.Success(
recursiveResult{KBID: kbID}, &format.Meta{KBID: kbID}), jopts.Fields, jopts.JQ)
}
fmt.Fprintf(iostreams.IO.Out, "(no files matched %q under %s)\n", opts.Glob, dir)
return nil
@@ -75,7 +75,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
for _, m := range matches {
previews = append(previews, uploadOutcome{Path: m})
}
return cmdutil.EmitDryRun(opts.JSONOut,
return cmdutil.EmitDryRun(jopts.Enabled(),
recursiveResult{KBID: kbID, Uploaded: previews},
&format.Meta{KBID: kbID},
&format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("upload %d file(s) to kb %s", len(matches), kbID)})
@@ -93,7 +93,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
failed = append(failed, uploadOutcome{Path: p, Error: err.Error()})
// Per-file progress lines are human progress signal; suppress
// under --json so they don't precede the envelope on stdout.
if !opts.JSONOut {
if !jopts.Enabled() {
fmt.Fprintf(iostreams.IO.Out, "FAIL %s: %v\n", filepath.Base(p), err)
}
continue
@@ -103,16 +103,16 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
id = k.ID
}
uploaded = append(uploaded, uploadOutcome{Path: p, ID: id})
if !opts.JSONOut {
if !jopts.Enabled() {
fmt.Fprintf(iostreams.IO.Out, "OK %s (id: %s)\n", filepath.Base(p), id)
}
}
if opts.JSONOut {
if jopts.Enabled() {
result := recursiveResult{KBID: kbID, Uploaded: uploaded, Failed: failed}
risk := &format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("upload %d file(s) to kb %s", len(matches), kbID)}
if err := format.WriteEnvelope(iostreams.IO.Out,
format.SuccessWithRisk(result, &format.Meta{KBID: kbID}, risk)); err != nil {
if err := format.WriteEnvelopeFiltered(iostreams.IO.Out,
format.SuccessWithRisk(result, &format.Meta{KBID: kbID}, risk), jopts.Fields, jopts.JQ); err != nil {
return err
}
} else {
@@ -128,7 +128,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, svc UploadServ
return &cmdutil.Error{
Code: firstFailCode,
Message: fmt.Sprintf("%d of %d uploads failed", len(failed), len(matches)),
Silent: opts.JSONOut,
Silent: jopts.Enabled(),
}
}
return nil
+19 -9
View File
@@ -42,6 +42,16 @@ func (s *scriptedUploadSvc) CreateKnowledgeFromFile(
return r.k, r.err
}
// CreateKnowledgeFromURL satisfies UploadService but is unused by the
// recursive-walk path. Recursive upload only goes through CreateKnowledgeFromFile.
func (s *scriptedUploadSvc) CreateKnowledgeFromURL(
_ context.Context,
_ string,
_ sdk.CreateKnowledgeFromURLRequest,
) (*sdk.Knowledge, error) {
return nil, nil
}
func mkTree(t *testing.T, base string, names ...string) {
t.Helper()
for _, n := range names {
@@ -58,7 +68,7 @@ func TestUploadRecursive_WalksAllFiles(t *testing.T) {
svc := &scriptedUploadSvc{}
opts := &UploadOptions{Recursive: true, Glob: "*"}
require.NoError(t, runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir))
require.NoError(t, runUploadRecursive(context.Background(), opts, nil, svc, "kb_xxx", dir))
sort.Strings(svc.called)
assert.Equal(t, []string{"a.pdf", "b.pdf", "c.pdf"}, svc.called)
@@ -75,7 +85,7 @@ func TestUploadRecursive_GlobFilter(t *testing.T) {
svc := &scriptedUploadSvc{}
opts := &UploadOptions{Recursive: true, Glob: "*.pdf"}
require.NoError(t, runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir))
require.NoError(t, runUploadRecursive(context.Background(), opts, nil, svc, "kb_xxx", dir))
sort.Strings(svc.called)
assert.Equal(t, []string{"doc.pdf", "keep.pdf"}, svc.called)
@@ -93,7 +103,7 @@ func TestUploadRecursive_PartialFailure_Exits1(t *testing.T) {
"bad.pdf": {err: errors.New("HTTP error 500: internal")},
}}
opts := &UploadOptions{Recursive: true, Glob: "*"}
err := runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir)
err := runUploadRecursive(context.Background(), opts, nil, svc, "kb_xxx", dir)
require.Error(t, err)
var typed *cmdutil.Error
@@ -116,7 +126,7 @@ func TestUploadRecursive_NoMatches(t *testing.T) {
svc := &scriptedUploadSvc{}
opts := &UploadOptions{Recursive: true, Glob: "*.pdf"}
require.NoError(t, runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir))
require.NoError(t, runUploadRecursive(context.Background(), opts, nil, svc, "kb_xxx", dir))
assert.Len(t, svc.called, 0)
assert.Contains(t, strings.ToLower(out.String()), "no files matched")
}
@@ -125,7 +135,7 @@ func TestUploadRecursive_NotADirectory(t *testing.T) {
_, _ = iostreams.SetForTest(t)
path := writeTempFile(t, "single.pdf")
svc := &scriptedUploadSvc{}
err := runUploadRecursive(context.Background(), &UploadOptions{Recursive: true, Glob: "*"}, svc, "kb_xxx", path)
err := runUploadRecursive(context.Background(), &UploadOptions{Recursive: true, Glob: "*"}, nil, svc, "kb_xxx", path)
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
@@ -139,7 +149,7 @@ func TestUploadRecursive_RejectsNameFlag(t *testing.T) {
mkTree(t, dir, "a.pdf")
svc := &scriptedUploadSvc{}
opts := &UploadOptions{Recursive: true, Glob: "*", Name: "single-name.pdf"}
err := runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir)
err := runUploadRecursive(context.Background(), opts, nil, svc, "kb_xxx", dir)
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
@@ -158,8 +168,8 @@ func TestUploadRecursive_JSON_Envelope(t *testing.T) {
}{
"bad.pdf": {err: errors.New("HTTP error 500: internal")},
}}
opts := &UploadOptions{Recursive: true, Glob: "*", JSONOut: true}
err := runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir)
opts := &UploadOptions{Recursive: true, Glob: "*"}
err := runUploadRecursive(context.Background(), opts, &cmdutil.JSONOptions{}, svc, "kb_xxx", dir)
require.Error(t, err) // partial failure → typed error
body := out.String()
@@ -188,7 +198,7 @@ func TestUploadRecursive_DryRun(t *testing.T) {
mkTree(t, dir, "a.pdf", "b.pdf")
svc := &scriptedUploadSvc{}
opts := &UploadOptions{Recursive: true, Glob: "*", DryRun: true}
require.NoError(t, runUploadRecursive(context.Background(), opts, svc, "kb_xxx", dir))
require.NoError(t, runUploadRecursive(context.Background(), opts, nil, svc, "kb_xxx", dir))
assert.Len(t, svc.called, 0, "dry-run must not call SDK")
got := out.String()
assert.Contains(t, got, "would upload 2")
+124 -9
View File
@@ -18,12 +18,15 @@ import (
// fakeUploadSvc captures call arguments and returns canned responses.
type fakeUploadSvc struct {
resp *sdk.Knowledge
err error
got struct {
resp *sdk.Knowledge
err error
urlResp *sdk.Knowledge
urlErr error
got struct {
kbID, filePath, customName, channel string
metadata map[string]string
enableMultimodel *bool
urlReq sdk.CreateKnowledgeFromURLRequest
}
}
@@ -43,6 +46,16 @@ func (f *fakeUploadSvc) CreateKnowledgeFromFile(
return f.resp, f.err
}
func (f *fakeUploadSvc) CreateKnowledgeFromURL(
_ context.Context,
kbID string,
req sdk.CreateKnowledgeFromURLRequest,
) (*sdk.Knowledge, error) {
f.got.kbID = kbID
f.got.urlReq = req
return f.urlResp, f.urlErr
}
// writeTempFile creates a regular file under t.TempDir() with sample content.
func writeTempFile(t *testing.T, name string) string {
t.Helper()
@@ -56,7 +69,7 @@ func TestUpload_Success_Human(t *testing.T) {
path := writeTempFile(t, "report.pdf")
svc := &fakeUploadSvc{resp: &sdk.Knowledge{ID: "doc_99", FileName: "report.pdf"}}
opts := &UploadOptions{}
require.NoError(t, runUpload(context.Background(), opts, svc, "kb_xxx", path))
require.NoError(t, runUpload(context.Background(), opts, nil, svc, "kb_xxx", path))
assert.Equal(t, "kb_xxx", svc.got.kbID)
assert.Equal(t, path, svc.got.filePath)
@@ -78,7 +91,7 @@ func TestUpload_Success_CustomName(t *testing.T) {
path := writeTempFile(t, "q3.pdf")
svc := &fakeUploadSvc{resp: &sdk.Knowledge{ID: "doc_88", FileName: "q3.pdf"}}
opts := &UploadOptions{Name: "Q3 Marketing Report.pdf"}
require.NoError(t, runUpload(context.Background(), opts, svc, "kb_xxx", path))
require.NoError(t, runUpload(context.Background(), opts, nil, svc, "kb_xxx", path))
assert.Equal(t, "Q3 Marketing Report.pdf", svc.got.customName)
}
@@ -86,8 +99,8 @@ func TestUpload_Success_JSON(t *testing.T) {
out, _ := iostreams.SetForTest(t)
path := writeTempFile(t, "a.md")
svc := &fakeUploadSvc{resp: &sdk.Knowledge{ID: "doc_77", FileName: "a.md"}}
opts := &UploadOptions{JSONOut: true}
require.NoError(t, runUpload(context.Background(), opts, svc, "kb_xxx", path))
opts := &UploadOptions{}
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)
@@ -100,7 +113,7 @@ func TestUpload_HTTPError_500(t *testing.T) {
_, _ = iostreams.SetForTest(t)
path := writeTempFile(t, "x.txt")
svc := &fakeUploadSvc{err: errors.New("HTTP error 500: internal")}
err := runUpload(context.Background(), &UploadOptions{}, svc, "kb_xxx", path)
err := runUpload(context.Background(), &UploadOptions{}, nil, svc, "kb_xxx", path)
require.Error(t, err)
var typed *cmdutil.Error
@@ -112,7 +125,7 @@ func TestUpload_HTTPError_409Conflict(t *testing.T) {
_, _ = iostreams.SetForTest(t)
path := writeTempFile(t, "dup.pdf")
svc := &fakeUploadSvc{err: errors.New("HTTP error 409: file exists")}
err := runUpload(context.Background(), &UploadOptions{}, svc, "kb_xxx", path)
err := runUpload(context.Background(), &UploadOptions{}, nil, svc, "kb_xxx", path)
require.Error(t, err)
var typed *cmdutil.Error
@@ -155,3 +168,105 @@ func TestValidateUploadPath_SymlinkToFileAccepted(t *testing.T) {
// os.Stat (not Lstat) should follow the symlink and report regular file.
require.NoError(t, validateUploadPath(link))
}
// --from-url tests (4-N1).
func TestUploadFromURL_Success_Human(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeUploadSvc{urlResp: &sdk.Knowledge{ID: "doc_url_1", FileName: "whitepaper.pdf"}}
opts := &UploadOptions{FromURL: "https://example.com/whitepaper.pdf"}
require.NoError(t, runUploadFromURL(context.Background(), opts, nil, svc, "kb_xxx"))
assert.Equal(t, "kb_xxx", svc.got.kbID)
assert.Equal(t, "https://example.com/whitepaper.pdf", svc.got.urlReq.URL)
assert.Equal(t, "api", svc.got.urlReq.Channel)
assert.Contains(t, out.String(), "Ingested")
assert.Contains(t, out.String(), "doc_url_1")
}
func TestUploadFromURL_WithName_Passes_AsFileName(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeUploadSvc{urlResp: &sdk.Knowledge{ID: "doc_url_2"}}
opts := &UploadOptions{FromURL: "https://example.com/article.html", Name: "Q3 Article"}
require.NoError(t, runUploadFromURL(context.Background(), opts, nil, svc, "kb_xxx"))
assert.Equal(t, "Q3 Article", svc.got.urlReq.FileName,
"--name must be forwarded as FileName (server uses it for file-vs-crawl mode hint)")
}
func TestUploadFromURL_JSON_Envelope(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")
}
func TestUploadFromURL_DuplicateURLMaps_resource_already_exists(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeUploadSvc{
urlResp: &sdk.Knowledge{ID: "doc_existing"},
urlErr: sdk.ErrDuplicateURL,
}
err := runUploadFromURL(context.Background(),
&UploadOptions{FromURL: "https://example.com/dup.pdf"}, nil, svc, "kb_xxx")
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
assert.Equal(t, cmdutil.CodeResourceAlreadyExists, typed.Code)
}
func TestValidateUploadFlags_FromURL_OK(t *testing.T) {
require.NoError(t, validateUploadFlags(&UploadOptions{FromURL: "https://example.com/x.pdf"}, nil))
}
func TestValidateUploadFlags_FromURL_WithPositional_Rejected(t *testing.T) {
err := validateUploadFlags(&UploadOptions{FromURL: "https://example.com/x.pdf"}, []string{"/tmp/x.pdf"})
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
}
func TestValidateUploadFlags_FromURL_WithRecursive_Rejected(t *testing.T) {
err := validateUploadFlags(&UploadOptions{FromURL: "https://example.com/x.pdf", Recursive: true}, nil)
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
}
func TestValidateUploadFlags_FromURL_BadScheme(t *testing.T) {
err := validateUploadFlags(&UploadOptions{FromURL: "file:///etc/passwd"}, nil)
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
}
func TestValidateUploadFlags_FromURL_NoHost(t *testing.T) {
err := validateUploadFlags(&UploadOptions{FromURL: "https://"}, nil)
require.Error(t, err)
}
func TestValidateUploadFlags_NoPathOrURL_Rejected(t *testing.T) {
err := validateUploadFlags(&UploadOptions{}, nil)
require.Error(t, err)
var typed *cmdutil.Error
require.ErrorAs(t, err, &typed)
assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
}
+19 -7
View File
@@ -14,10 +14,18 @@ import (
sdk "github.com/Tencent/WeKnora/client"
)
type ViewOptions struct {
JSONOut bool
// docViewFields enumerates the fields surfaced for `--json` discovery on
// `doc view`. Lists the Knowledge struct top-level json tags.
var docViewFields = []string{
"id", "knowledge_base_id", "tag_id", "type", "title", "description",
"source", "channel", "parse_status", "summary_status", "enable_status",
"embedding_model_id", "file_name", "file_type", "file_size", "file_hash",
"file_path", "storage_size",
"created_at", "updated_at", "processed_at", "error_message",
}
type ViewOptions struct{}
// ViewService is the narrow SDK surface this command depends on.
type ViewService interface {
GetKnowledge(ctx context.Context, id string) (*sdk.Knowledge, error)
@@ -33,25 +41,29 @@ func NewCmdView(f *cmdutil.Factory) *cobra.Command {
weknora doc view doc_abc --json`,
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
jopts, err := cmdutil.CheckJSONFlags(c)
if err != nil {
return err
}
cli, err := f.Client()
if err != nil {
return err
}
return runView(c.Context(), opts, cli, args[0])
return runView(c.Context(), opts, jopts, cli, args[0])
},
}
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
cmdutil.AddJSONFlags(cmd, docViewFields)
agent.SetAgentHelp(cmd, "Returns the full Knowledge object for one document: id, kb_id, file metadata, parse_status, processed_at, error_message. Use this instead of paging through `doc list` when you have the doc id.")
return cmd
}
func runView(ctx context.Context, opts *ViewOptions, svc ViewService, id string) error {
func runView(ctx context.Context, opts *ViewOptions, jopts *cmdutil.JSONOptions, svc ViewService, id string) error {
doc, err := svc.GetKnowledge(ctx, id)
if err != nil {
return cmdutil.WrapHTTP(err, "get document %q", id)
}
if opts.JSONOut {
return format.WriteEnvelope(iostreams.IO.Out, format.Success(doc, &format.Meta{KBID: doc.KnowledgeBaseID}))
if jopts.Enabled() {
return format.WriteEnvelopeFiltered(iostreams.IO.Out, format.Success(doc, &format.Meta{KBID: doc.KnowledgeBaseID}), jopts.Fields, jopts.JQ)
}
w := iostreams.IO.Out
fmt.Fprintf(w, "ID: %s\n", doc.ID)
+5 -5
View File
@@ -39,7 +39,7 @@ func TestView_Human_RendersExpectedFields(t *testing.T) {
UpdatedAt: time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC),
ProcessedAt: &processed,
}}
if err := runView(context.Background(), &ViewOptions{}, svc, "doc_abc"); err != nil {
if err := runView(context.Background(), &ViewOptions{}, nil, svc, "doc_abc"); err != nil {
t.Fatalf("runView: %v", err)
}
got := out.String()
@@ -59,7 +59,7 @@ func TestView_Human_RendersExpectedFields(t *testing.T) {
func TestView_Human_TitleFallback(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeViewSvc{doc: &sdk.Knowledge{ID: "doc_url", Title: "Pasted article", FileName: ""}}
if err := runView(context.Background(), &ViewOptions{}, svc, "doc_url"); err != nil {
if err := runView(context.Background(), &ViewOptions{}, nil, svc, "doc_url"); err != nil {
t.Fatalf("runView: %v", err)
}
got := out.String()
@@ -77,7 +77,7 @@ func TestView_Human_OmitsEmptyFields(t *testing.T) {
ID: "doc_abc",
FileName: "x.txt",
}}
if err := runView(context.Background(), &ViewOptions{}, svc, "doc_abc"); err != nil {
if err := runView(context.Background(), &ViewOptions{}, nil, svc, "doc_abc"); err != nil {
t.Fatalf("runView: %v", err)
}
// Line-prefix match (not substring): "ERROR:" as a substring could
@@ -97,7 +97,7 @@ func TestView_Human_OmitsEmptyFields(t *testing.T) {
func TestView_JSON_EmitsEnvelope(t *testing.T) {
out, _ := iostreams.SetForTest(t)
svc := &fakeViewSvc{doc: &sdk.Knowledge{ID: "doc_abc", FileName: "x.txt", KnowledgeBaseID: "kb1"}}
if err := runView(context.Background(), &ViewOptions{JSONOut: true}, svc, "doc_abc"); err != nil {
if err := runView(context.Background(), &ViewOptions{}, &cmdutil.JSONOptions{}, svc, "doc_abc"); err != nil {
t.Fatalf("runView: %v", err)
}
got := out.String()
@@ -111,7 +111,7 @@ func TestView_JSON_EmitsEnvelope(t *testing.T) {
func TestView_NotFound_ClassifiedAs404(t *testing.T) {
_, _ = iostreams.SetForTest(t)
svc := &fakeViewSvc{err: errors.New("HTTP error 404: not found")}
err := runView(context.Background(), &ViewOptions{}, svc, "missing")
err := runView(context.Background(), &ViewOptions{}, nil, svc, "missing")
if err == nil {
t.Fatal("expected error")
}