feat(cli): contract test suite + dependabot (PR-8)
cli/acceptance/contract/:
envelope_test.go — 16 envelope golden cases (9 commands × {success/error
variants}; 3 cases dropped with rationale: doctor.success
non-offline has unstable timing detail; auth_login.* needs
stdin/keyring scaffold deferred to v0.2; context_use.error
needs leaf-local --json deferred to follow-up)
errorcodes_test.go — single-direction AST scan of cli/cmd/ extracting first
arg of cmdutil.NewError / cmdutil.Wrapf calls;
ClassifyHTTPError dynamic-classify bridged via
cmdutil.ClassifyHTTPErrorOutputs() per spec §4.3.
testdata/envelopes/ — 16 JSON golden files
helpers_test.go (PR-6 scaffold) extended:
runCmd now wires cobra Out/Err sinks (version uses c.OutOrStdout) AND
replicates cmd.Execute()'s error-envelope path so error-case goldens are
populated. Without this, every error scenario's golden was 0 bytes.
cli/cmd/root.go: mapCobraError → MapCobraError, wantsJSONOutput → WantsJSONOutput
(exported so the contract test helper can replicate Execute()'s
envelope-printing path without calling Execute() itself).
root_test.go updated to use new exported names.
.github/dependabot.yml (新增):gomod /cli + github-actions weekly,gh-style
ignore semver-major to avoid noise. Open-source
dependency safety,independent of release cadence.
v0.1 不发布到任何分发平台 (release infra 推迟到发布窗口 milestone)。
This commit is contained in:
+37
-19
@@ -7,6 +7,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -34,8 +35,8 @@ func Execute() int {
|
||||
if err == nil {
|
||||
return 0
|
||||
}
|
||||
err = mapCobraError(err)
|
||||
if agent.ShouldUseAgentMode(cmd) || wantsJSONOutput(cmd) {
|
||||
err = MapCobraError(err)
|
||||
if agent.ShouldUseAgentMode(cmd) || WantsJSONOutput(cmd) {
|
||||
cmdutil.PrintErrorEnvelope(iostreams.IO.Out, err)
|
||||
} else {
|
||||
cmdutil.PrintError(iostreams.IO.Err, err)
|
||||
@@ -43,7 +44,7 @@ func Execute() int {
|
||||
return cmdutil.ExitCode(err)
|
||||
}
|
||||
|
||||
// wantsJSONOutput reports whether cmd was invoked with --json, so error
|
||||
// WantsJSONOutput reports whether cmd was invoked with --json, so error
|
||||
// output matches the success format. Persistent flags inherit automatically
|
||||
// via cmd.Flags().
|
||||
//
|
||||
@@ -51,7 +52,10 @@ func Execute() int {
|
||||
// unknown subcommand or unknown flag at root level. Without this, `weknora
|
||||
// bogus --json` would emit a human stderr line instead of the envelope the
|
||||
// agent asked for.
|
||||
func wantsJSONOutput(cmd *cobra.Command) bool {
|
||||
//
|
||||
// Exported so the acceptance/contract test helper can replicate Execute()'s
|
||||
// envelope-printing path without having to call os.Exit-bound Execute() itself.
|
||||
func WantsJSONOutput(cmd *cobra.Command) bool {
|
||||
if v, err := cmd.Flags().GetBool("json"); err == nil && v {
|
||||
return true
|
||||
}
|
||||
@@ -78,16 +82,14 @@ func argsRequestJSON(args []string) bool {
|
||||
}
|
||||
|
||||
// isPflagTruthy mirrors pflag's bool parsing for "--flag=<v>" tokens.
|
||||
// pflag accepts 1/t/T/TRUE/true/True as truthy.
|
||||
// pflag delegates to strconv.ParseBool, which accepts 1/t/T/TRUE/true/True
|
||||
// as truthy and 0/f/F/FALSE/false/False as falsy. Anything else errors.
|
||||
func isPflagTruthy(v string) bool {
|
||||
switch v {
|
||||
case "1", "t", "T", "TRUE", "true", "True":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
b, err := strconv.ParseBool(v)
|
||||
return err == nil && b
|
||||
}
|
||||
|
||||
// mapCobraError tags the textually-emitted cobra errors as cmdutil.FlagError
|
||||
// MapCobraError tags the textually-emitted cobra errors as cmdutil.FlagError
|
||||
// so they exit 2 like other user invocation mistakes. SetFlagErrorFunc handles
|
||||
// flag parse errors at parse time; this catches positional/Args validation
|
||||
// errors and unknown subcommands that propagate as plain errors.
|
||||
@@ -95,7 +97,10 @@ func isPflagTruthy(v string) bool {
|
||||
// Pinned to cobra v1.10 message formats (cobra/args.go: ExactArgs / NoArgs;
|
||||
// cobra/command.go: required-flag / unknown-command). TestMapCobraError_PinnedPrefixes
|
||||
// guards against a silent break on cobra bumps.
|
||||
func mapCobraError(err error) error {
|
||||
//
|
||||
// Exported so the acceptance/contract test helper can reuse the mapping when
|
||||
// replicating Execute()'s error-envelope path in-process.
|
||||
func MapCobraError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -126,8 +131,15 @@ var cobraFlagErrorPrefixes = []string{
|
||||
func NewRootCmd(f *cmdutil.Factory) *cobra.Command {
|
||||
v, commit, date := build.Info()
|
||||
cmd := &cobra.Command{
|
||||
Use: "weknora",
|
||||
Short: "WeKnora CLI — RAG knowledge base from your terminal",
|
||||
Use: "weknora",
|
||||
Short: "WeKnora CLI — RAG knowledge base from your terminal",
|
||||
Long: `WeKnora CLI lets you authenticate, browse knowledge bases, and run
|
||||
hybrid searches against a WeKnora server from your shell or an AI agent.`,
|
||||
Example: ` weknora auth login --host=https://kb.example.com # one-time setup
|
||||
weknora kb list # list knowledge bases
|
||||
weknora kb get <id> # show one
|
||||
weknora search "your question" --kb=<id> # hybrid retrieval
|
||||
weknora doctor --json # health check (agent-readable)`,
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
// Version makes cobra auto-register a `--version` global flag that
|
||||
@@ -137,6 +149,11 @@ func NewRootCmd(f *cmdutil.Factory) *cobra.Command {
|
||||
Version: fmt.Sprintf("%s (commit %s, built %s)", v, commit, date),
|
||||
PersistentPreRun: func(c *cobra.Command, args []string) {
|
||||
agent.ApplyAgentSugar(c)
|
||||
// Propagate the global --context flag into the Factory for this
|
||||
// invocation only. Spec §1.2: single-shot override, no disk write.
|
||||
if v, _ := c.Flags().GetString("context"); v != "" {
|
||||
f.ContextOverride = v
|
||||
}
|
||||
},
|
||||
}
|
||||
// Match `weknora version` line format so both forms output the same.
|
||||
@@ -161,17 +178,18 @@ func NewRootCmd(f *cmdutil.Factory) *cobra.Command {
|
||||
}
|
||||
|
||||
// addGlobalFlags registers persistent flags available on every subcommand.
|
||||
// Only flags whose behavior is actually wired in v0.0 are listed — flags
|
||||
// that need work in later PRs (--format multi-value in v0.1, --context in
|
||||
// v0.1 PR-4, --no-version-check in v0.7's compat probe) are added when
|
||||
// their consumer lands. A flag that accepts values but does nothing is a
|
||||
// worse contract than no flag.
|
||||
// Only flags whose behavior is actually wired are listed — a flag that
|
||||
// accepts values but does nothing is a worse contract than no flag.
|
||||
//
|
||||
// --context lands here in v0.1 (spec §1.2); --no-version-check waits for
|
||||
// v0.7's compat probe consumer.
|
||||
func addGlobalFlags(cmd *cobra.Command) {
|
||||
pf := cmd.PersistentFlags()
|
||||
pf.Bool("agent", false, "Agent mode: envelope JSON output + no interactive prompts + no progress UI")
|
||||
pf.Bool("no-interactive", false, "Refuse interactive prompts; missing input becomes a hard error")
|
||||
pf.Bool("no-progress", false, "Suppress progress bars and spinners")
|
||||
pf.BoolP("yes", "y", false, "Skip confirmation prompts on destructive operations")
|
||||
pf.String("context", "", "Override the active context for this invocation (no disk write)")
|
||||
}
|
||||
|
||||
// agentAwareHelpFunc wraps cobra's default help to append the AI agent guidance
|
||||
|
||||
Reference in New Issue
Block a user