fix(cli): drop link --context flag (shadowed global --context)

The `link` subcommand declared a local `--context` StringVar that
shadowed the root-level persistent `--context` flag at the cobra layer.
Two different semantics under one name:

  - root global `--context <name>`: "override the active context for
    THIS invocation only, no disk write" (single-shot connection
    override, applied via Factory.ContextOverride).
  - link local `--context <name>`: "the context name to record in
    .weknora/project.yaml" (persisted state, written to disk).

The shadow meant `weknora --context staging link` (intent: link runs
against staging) silently did NOT propagate the override into link's
runtime; instead link's local "" beat the global. `weknora link
--context staging` (intent: record staging in the file) did work, but
shared a name with the unrelated global behavior, which is a usability
trap.

Resolution: drop the local flag entirely. The active context at link
time is what gets recorded; users who want to bind under a different
context use the global `--context X link --kb my-kb` form, which now
propagates correctly (no local shadow). This matches the bind-command
patterns surveyed across mainstream CLIs:

  - lark-cli `config bind` — uses domain-specific flags (--source /
    --app-id / --identity); the global --profile is named distinctly.
  - gh `repo set-default` — uses a positional for the bind target;
    the global -R/--repo is the only flag-form path.
  - netlify `link` — uses --id/--name for the bind target; no
    --site global flag at all (env var only).
  - vercel `link` — reuses --project for both global and link, but
    only works because vercel ships a custom parser that merges
    flag/env/file precedence; cobra's persistent-flag shadowing is
    silent-override, not graceful merge.

The lark-cli / gh / netlify pattern of "bind command's target flag
must not share a name with the global override flag" is the cobra-
friendly choice; dropping the flag is the simplest form of that.

No behavior change for the common path (`weknora link --kb my-kb`
without --context still records the active context). The "record
under a specific non-current context" use case is now expressed via
the global flag, which is what it was designed for.

link_test.go untouched (no test referenced the dropped flag).
This commit is contained in:
nullkey
2026-05-13 15:07:14 +08:00
committed by lyingbug
parent c9b837dfce
commit 13cce78332
+7 -8
View File
@@ -22,7 +22,6 @@ import (
)
type Options struct {
Context string // --context: record a specific context instead of the active one
KB string // --kb: KB UUID or name; empty triggers interactive prompt on TTY
JSONOut bool // --json
}
@@ -60,7 +59,6 @@ user explicitly asked to bind this directory; don't run it as a side effect.`,
return runLink(c.Context(), opts, f)
},
}
cmd.Flags().StringVar(&opts.Context, "context", "", "Context to record in the link (defaults to active context)")
cmd.Flags().StringVar(&opts.KB, "kb", "", "Knowledge base UUID or name; omit on a TTY for interactive prompt")
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
agent.SetAgentHelp(cmd, "Writes .weknora/project.yaml binding cwd to a KB. Pass --kb (id or name) for non-interactive use. Always overwrites.")
@@ -74,7 +72,7 @@ func runLink(ctx context.Context, opts *Options, f *cmdutil.Factory) error {
}
linkPath := filepath.Join(cwd, projectlink.DirName, projectlink.FileName)
ctxName, err := resolveContext(opts, f)
ctxName, err := resolveContext(f)
if err != nil {
return err
}
@@ -113,11 +111,12 @@ func runLink(ctx context.Context, opts *Options, f *cmdutil.Factory) error {
return nil
}
// resolveContext picks the auth context to record in the link.
func resolveContext(opts *Options, f *cmdutil.Factory) (string, error) {
if opts.Context != "" {
return opts.Context, nil
}
// resolveContext picks the auth context to record in the link. There is no
// per-invocation override flag on `weknora link` itself — to record under a
// different context, use the global persistent flag (`weknora --context
// staging link --kb my-kb`); the active context at link time is what gets
// written.
func resolveContext(f *cmdutil.Factory) (string, error) {
cfg, err := f.Config()
if err != nil {
return "", err