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
+22 -12
View File
@@ -15,9 +15,14 @@ import (
)
type RemoveOptions struct {
Yes bool // sourced from the global -y/--yes persistent flag (matches `kb delete`)
JSONOut bool
DryRun bool
Yes bool // sourced from the global -y/--yes persistent flag (matches `kb delete`)
DryRun bool
}
// contextRemoveFields enumerates the fields surfaced for `--json` discovery on
// `context remove`. The result reports the disposition of the removed entry.
var contextRemoveFields = []string{
"name", "removed", "was_current",
}
// removeResult is the typed payload emitted under data on success.
@@ -49,21 +54,25 @@ in scripted / --json invocations (exit code 10; see cli/AGENTS.md).`,
weknora context remove production -y # remove current → confirm`,
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)
store, err := f.Secrets()
if err != nil {
return err
}
return runRemove(opts, args[0], store, f.Prompter())
return runRemove(opts, jopts, args[0], store, f.Prompter())
},
}
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
cmdutil.AddJSONFlags(cmd, contextRemoveFields)
agent.SetAgentHelp(cmd, "Removes a context entry. Removing the current context requires -y in non-TTY/--json (exit-10). Always best-effort clears keyring refs.")
return cmd
}
func runRemove(opts *RemoveOptions, name string, store secrets.Store, p prompt.Prompter) error {
func runRemove(opts *RemoveOptions, jopts *cmdutil.JSONOptions, name string, store secrets.Store, p prompt.Prompter) error {
cfg, err := config.Load()
if err != nil {
return err
@@ -75,8 +84,9 @@ func runRemove(opts *RemoveOptions, name string, store secrets.Store, p prompt.P
wasCurrent := name == cfg.CurrentContext
risk := riskForRemove(name, wasCurrent)
jsonOut := jopts.Enabled()
if opts.DryRun {
return cmdutil.EmitDryRun(opts.JSONOut,
return cmdutil.EmitDryRun(jsonOut,
removeResult{Name: name, Removed: false, WasCurrent: wasCurrent},
&format.Meta{Context: cfg.CurrentContext},
risk)
@@ -84,7 +94,7 @@ func runRemove(opts *RemoveOptions, name string, store secrets.Store, p prompt.P
// Confirmation only fires for removing the current context — non-current
// remove uses the same low-friction policy as `auth logout`.
if wasCurrent {
if err := cmdutil.ConfirmDestructive(p, opts.Yes, opts.JSONOut, "current context", name); err != nil {
if err := cmdutil.ConfirmDestructive(p, opts.Yes, jsonOut, "current context", name); err != nil {
return err
}
}
@@ -101,10 +111,10 @@ func runRemove(opts *RemoveOptions, name string, store secrets.Store, p prompt.P
clearContextSecrets(store, ctx, name)
result := removeResult{Name: name, Removed: true, WasCurrent: wasCurrent}
if opts.JSONOut {
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(
result, &format.Meta{Context: cfg.CurrentContext}, risk,
))
if jsonOut {
return format.WriteEnvelopeFiltered(iostreams.IO.Out,
format.SuccessWithRisk(result, &format.Meta{Context: cfg.CurrentContext}, risk),
jopts.Fields, jopts.JQ)
}
if wasCurrent {
fmt.Fprintf(iostreams.IO.Out, "✓ Removed context %s (current context cleared — run `weknora context use <name>` to pick another)\n", name)