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
+23 -12
View File
@@ -14,9 +14,14 @@ import (
)
type AddOptions struct {
Host string
User string
JSONOut bool
Host string
User string
}
// contextAddFields enumerates the fields surfaced for `--json` discovery on
// `context add`. The result describes the newly-registered context.
var contextAddFields = []string{
"name", "host", "user", "current",
}
// addResult is the typed payload emitted under data on success.
@@ -49,18 +54,22 @@ adds leave the current context untouched.`,
weknora context add prod --host https://prod.example.com --user alice@example.com`,
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
return runAdd(opts, args[0])
jopts, err := cmdutil.CheckJSONFlags(c)
if err != nil {
return err
}
return runAdd(opts, jopts, args[0])
},
}
cmd.Flags().StringVar(&opts.Host, "host", "", "Server base URL, e.g. https://kb.example.com (required)")
cmd.Flags().StringVar(&opts.User, "user", "", "Account email shown in 'context list' (optional, cosmetic only)")
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
cmdutil.AddJSONFlags(cmd, contextAddFields)
_ = cmd.MarkFlagRequired("host")
agent.SetAgentHelp(cmd, "Adds a context. First context added auto-becomes current. Errors with resource.already_exists if name collides; input.invalid_argument if --host is not an absolute http(s) URL.")
return cmd
}
func runAdd(opts *AddOptions, name string) error {
func runAdd(opts *AddOptions, jopts *cmdutil.JSONOptions, name string) error {
if err := validateName(name); err != nil {
return err
}
@@ -93,12 +102,14 @@ func runAdd(opts *AddOptions, name string) error {
}
risk := &format.Risk{Level: format.RiskWrite, Action: fmt.Sprintf("add context %s", name)}
if opts.JSONOut {
return format.WriteEnvelope(iostreams.IO.Out, format.SuccessWithRisk(
addResult{Name: name, Host: host, User: opts.User, Current: wasFirst},
&format.Meta{Context: cfg.CurrentContext},
risk,
))
if jopts.Enabled() {
return format.WriteEnvelopeFiltered(iostreams.IO.Out,
format.SuccessWithRisk(
addResult{Name: name, Host: host, User: opts.User, Current: wasFirst},
&format.Meta{Context: cfg.CurrentContext},
risk,
),
jopts.Fields, jopts.JQ)
}
if wasFirst {
fmt.Fprintf(iostreams.IO.Out, "✓ Added context %s (now current). Run `weknora auth login --name %s` to attach credentials.\n", name, name)