feat(cli): --log-level + kb/agent status & check + cross-cutting refactor
Operability surface and the bulk of the jopts→fopts migration: * --log-level error|warn|info|debug + WEKNORA_LOG_LEVEL env, wired to the SDK via client.SetDebugLevel. Invalid --log-level returns FlagError (exit 2). * kb status <kb-id> / kb check <kb-id> verb split (1 HTTP vs 1+N for failed_count aggregation). * agent status <agent-id> / agent check <agent-id> verb split (probes kb_scope_all_reachable via 1+N HTTP). * kb create <name> positional (matches agent create). * Positional id help strings namespaced (<kb-id> / <agent-id>). * All auth / context / link / doctor / kb / agent CRUD commands migrated to the FormatOptions API. * root.go Execute(ctx) takes a context so signal-cancellation propagates via cmd.Context() into long-running commands. * Pagination termination uses len(accum) >= total (not page*pageSize) so server-capped page sizes do not truncate aggregations.
This commit is contained in:
@@ -36,7 +36,7 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// doctorFields enumerates the fields surfaced for `--json` discovery on
|
||||
// doctorFields enumerates the fields surfaced for `--format json` discovery on
|
||||
// `doctor`. Items here refer to data.checks[*] entries (Check struct).
|
||||
var doctorFields = []string{"name", "status", "details", "hint"}
|
||||
|
||||
@@ -100,17 +100,18 @@ 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)
|
||||
fopts, err := cmdutil.CheckFormatFlag(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fopts.ResolveDefault(iostreams.IO.IsStdoutTTY())
|
||||
svc, err := buildServices(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cliVer, _, _ := build.Info()
|
||||
r := runChecks(c.Context(), opts, svc, cliVer)
|
||||
emit(jopts, r)
|
||||
emit(fopts, r)
|
||||
// Exit-code policy: fail → exit 1; warn / ok / skip → exit 0.
|
||||
// SilentError suppresses both the human "error: ..." line and
|
||||
// the stderr error formatter, so the JSON already written by
|
||||
@@ -123,7 +124,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)")
|
||||
cmdutil.AddJSONFlags(cmd, doctorFields)
|
||||
cmdutil.AddFormatFlag(cmd, doctorFields...)
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -331,9 +332,9 @@ func summarize(cs []Check) Summary {
|
||||
// emit renders the doctor result. The JSON path emits the Result directly;
|
||||
// pass/fail signaling is conveyed by summary.failed (and the process exit
|
||||
// code, set by the caller).
|
||||
func emit(jopts *cmdutil.JSONOptions, r Result) {
|
||||
if jopts.Enabled() {
|
||||
_ = jopts.Emit(iostreams.IO.Out, r)
|
||||
func emit(fopts *cmdutil.FormatOptions, r Result) {
|
||||
if fopts.WantsJSON() {
|
||||
_ = fopts.Emit(iostreams.IO.Out, r)
|
||||
return
|
||||
}
|
||||
for _, c := range r.Checks {
|
||||
@@ -357,7 +358,7 @@ func emit(jopts *cmdutil.JSONOptions, r Result) {
|
||||
//
|
||||
// Agent / JSON consumers read the stable status string from
|
||||
// data.checks[].status; the glyphs are presentation-only and never appear
|
||||
// in --json output.
|
||||
// in --format json output.
|
||||
func marker(s Status) string {
|
||||
switch s {
|
||||
case StatusFail:
|
||||
|
||||
@@ -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(&cmdutil.JSONOptions{}, r)
|
||||
emit(&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, r)
|
||||
if !strings.Contains(out.String(), `"all_passed":true`) {
|
||||
t.Errorf("bare output should embed all_passed=true, got %q", out.String())
|
||||
}
|
||||
@@ -282,7 +282,7 @@ func TestDoctor_VersionSkewWarns(t *testing.T) {
|
||||
// Wire shape: warn-only run still has summary.failed=0 (bare data
|
||||
// carries the signal; exit code stays 0).
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
emit(&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, r)
|
||||
body := out.String()
|
||||
if strings.Contains(body, `"ok":`) {
|
||||
t.Errorf("bare doctor output must not carry envelope keys, got %q", body)
|
||||
@@ -387,7 +387,7 @@ func TestDoctor_BareJSON_WarnDoesNotSignalFail(t *testing.T) {
|
||||
{Name: "credential_storage", Status: StatusOK},
|
||||
},
|
||||
}
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
emit(&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, r)
|
||||
got := out.String()
|
||||
if !strings.Contains(got, `"failed":0`) {
|
||||
t.Errorf("warn-only result must have summary.failed=0 (exit-0 signal), got %q", got)
|
||||
@@ -410,7 +410,7 @@ func TestDoctor_BareJSON_FailRaisesSummary(t *testing.T) {
|
||||
{Name: "credential_storage", Status: StatusOK},
|
||||
},
|
||||
}
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
emit(&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, r)
|
||||
got := out.String()
|
||||
if !strings.Contains(got, `"failed":1`) {
|
||||
t.Errorf("fail must surface in summary.failed, got %q", got)
|
||||
@@ -428,7 +428,7 @@ func TestDoctor_HumanMarker_Warn(t *testing.T) {
|
||||
{Name: "server_version", Status: StatusWarn, Details: "older"},
|
||||
},
|
||||
}
|
||||
emit(nil, r)
|
||||
emit(&cmdutil.FormatOptions{Mode: cmdutil.FormatText}, r)
|
||||
got := out.String()
|
||||
if !strings.Contains(got, "⚠") {
|
||||
t.Errorf("human output should contain ⚠ glyph for warn, got %q", got)
|
||||
@@ -453,7 +453,7 @@ func TestDoctor_WarnedField_OmittedAtZero(t *testing.T) {
|
||||
{Name: "credential_storage", Status: StatusOK},
|
||||
},
|
||||
}
|
||||
emit(&cmdutil.JSONOptions{}, r)
|
||||
emit(&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, r)
|
||||
got := out.String()
|
||||
if strings.Contains(got, `"warned"`) {
|
||||
t.Errorf("output should omit `warned` field when zero, got %q", got)
|
||||
@@ -479,7 +479,7 @@ func TestDoctor_RunE_FailReturnsSilentError(t *testing.T) {
|
||||
cmd := NewCmd(f)
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SetArgs([]string{"--json"})
|
||||
cmd.SetArgs([]string{"--format", "json"})
|
||||
cmd.SetContext(context.Background())
|
||||
err := cmd.Execute()
|
||||
if !errors.Is(err, cmdutil.SilentError) {
|
||||
|
||||
Reference in New Issue
Block a user