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:
@@ -39,10 +39,13 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// doctorFields enumerates the fields surfaced for `--json` discovery on
|
||||
// `doctor`. Items here refer to data.checks[*] entries (Check struct).
|
||||
var doctorFields = []string{"name", "status", "details", "hint"}
|
||||
|
||||
type Options struct {
|
||||
NoCache bool
|
||||
Offline bool
|
||||
JSONOut bool
|
||||
}
|
||||
|
||||
// Status is the per-check outcome on the wire (JSON Marshal still emits the
|
||||
@@ -100,13 +103,17 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
|
||||
Short: "Run 4 self-checks: base URL, auth, server version, credential storage",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(c *cobra.Command, _ []string) error {
|
||||
jopts, err := cmdutil.CheckJSONFlags(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
svc, err := buildServices(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cliVer, _, _ := build.Info()
|
||||
r := runChecks(c.Context(), opts, svc, cliVer)
|
||||
emit(opts, r)
|
||||
emit(jopts, r)
|
||||
// v0.2 exit-code policy: fail → exit 1; warn / ok / skip → exit 0.
|
||||
// SilentError suppresses both the human "error: ..." line and the
|
||||
// error envelope printer, so the data envelope already written by
|
||||
@@ -119,7 +126,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
|
||||
}
|
||||
cmd.Flags().BoolVar(&opts.NoCache, "no-cache", false, "Bypass server-info cache (located at $XDG_CACHE_HOME/weknora/server-info.yaml); force re-probe")
|
||||
cmd.Flags().BoolVar(&opts.Offline, "offline", false, "Skip network checks; only verify local keyring/file storage (credential_storage check still runs)")
|
||||
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
||||
cmdutil.AddJSONFlags(cmd, doctorFields)
|
||||
agent.SetAgentHelp(cmd, "Returns 4 health checks. AGENT short-circuit: read data.summary.all_passed; if false, inspect data.checks[].status (ok/warn/fail/skip). exit 1 only when any status=fail; warn does not change envelope.ok.")
|
||||
return cmd
|
||||
}
|
||||
@@ -329,12 +336,13 @@ func summarize(cs []Check) Summary {
|
||||
// rather than calling format.Success because envelope.ok must reflect "no
|
||||
// fail" — warn does not flip it (per package doc), but fail does. We can't
|
||||
// use format.Failure either, since that drops the data field.
|
||||
func emit(opts *Options, r Result) {
|
||||
if opts.JSONOut {
|
||||
_ = format.WriteEnvelope(iostreams.IO.Out, format.Envelope{
|
||||
OK: r.Summary.Failed == 0,
|
||||
Data: r,
|
||||
})
|
||||
func emit(jopts *cmdutil.JSONOptions, r Result) {
|
||||
if jopts.Enabled() {
|
||||
_ = format.WriteEnvelopeFiltered(
|
||||
iostreams.IO.Out,
|
||||
format.Envelope{OK: r.Summary.Failed == 0, Data: r},
|
||||
jopts.Fields, jopts.JQ,
|
||||
)
|
||||
return
|
||||
}
|
||||
for _, c := range r.Checks {
|
||||
|
||||
@@ -67,7 +67,7 @@ func TestDoctor_AllOK(t *testing.T) {
|
||||
systemInfo: &sdk.SystemInfo{Version: "1.0.0"},
|
||||
userResp: goodUserResp(),
|
||||
}
|
||||
r := runChecks(context.Background(), &Options{JSONOut: true}, svc, "1.0.0")
|
||||
r := runChecks(context.Background(), &Options{}, svc, "1.0.0")
|
||||
if !r.Summary.AllPassed {
|
||||
t.Errorf("expected all_passed, got summary %+v", r.Summary)
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func TestDoctor_AllOK(t *testing.T) {
|
||||
if r.Summary.Failed != 0 || r.Summary.Skipped != 0 {
|
||||
t.Errorf("expected 0 fail / 0 skip, got %+v", r.Summary)
|
||||
}
|
||||
emit(&Options{JSONOut: true}, r)
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
if !strings.Contains(out.String(), `"all_passed":true`) {
|
||||
t.Errorf("envelope should embed all_passed=true, got %q", out.String())
|
||||
}
|
||||
@@ -280,7 +280,7 @@ func TestDoctor_VersionSkewWarns(t *testing.T) {
|
||||
|
||||
// Envelope ok stays true (warn is non-blocking).
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
emit(&Options{JSONOut: true}, r)
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
if !strings.Contains(out.String(), `"ok":true`) {
|
||||
t.Errorf("envelope.ok must be true on warn-only run, got %q", out.String())
|
||||
}
|
||||
@@ -381,7 +381,7 @@ func TestDoctor_EmitEnvelope_OK_WhenWarnOnly(t *testing.T) {
|
||||
{Name: "credential_storage", Status: StatusOK},
|
||||
},
|
||||
}
|
||||
emit(&Options{JSONOut: true}, r)
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
got := out.String()
|
||||
if !strings.Contains(got, `"ok":true`) {
|
||||
t.Errorf("envelope.ok must be true on warn-only result, got %q", got)
|
||||
@@ -400,7 +400,7 @@ func TestDoctor_EmitEnvelope_NotOK_OnFail(t *testing.T) {
|
||||
{Name: "credential_storage", Status: StatusOK},
|
||||
},
|
||||
}
|
||||
emit(&Options{JSONOut: true}, r)
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
got := out.String()
|
||||
if !strings.Contains(got, `"ok":false`) {
|
||||
t.Errorf("envelope.ok must be false when any check fails, got %q", got)
|
||||
@@ -418,7 +418,7 @@ func TestDoctor_HumanMarker_Warn(t *testing.T) {
|
||||
{Name: "server_version", Status: StatusWarn, Details: "older"},
|
||||
},
|
||||
}
|
||||
emit(&Options{JSONOut: false}, r)
|
||||
emit(nil, r)
|
||||
got := out.String()
|
||||
if !strings.Contains(got, "⚠") {
|
||||
t.Errorf("human output should contain ⚠ glyph for warn, got %q", got)
|
||||
@@ -443,7 +443,7 @@ func TestDoctor_WarnedField_OmittedAtZero(t *testing.T) {
|
||||
{Name: "credential_storage", Status: StatusOK},
|
||||
},
|
||||
}
|
||||
emit(&Options{JSONOut: true}, r)
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
got := out.String()
|
||||
if strings.Contains(got, `"warned"`) {
|
||||
t.Errorf("envelope should omit `warned` field when zero, got %q", got)
|
||||
|
||||
Reference in New Issue
Block a user