feat(cli): context CRUD

New v0.3 P0 entry 3-4: kubectl-style context-management subtree using
gh's `<noun> <verb>` surface convention consistent with the rest of
this CLI.

- context list: tabwriter rendering + --json envelope; reads config.yaml
  only.
- context add <name> --host <url> [--user]: validates http(s) URL, first
  context auto-becomes current, rejects duplicates with did-you-mean.
- context remove <name>: best-effort keyring cleanup like `auth logout`.
  Removing the current context triggers exit-10 confirmation (lark-cli
  skill protocol) — subsequent commands would lose their default
  --context.

(`context use` predates v0.3; the subtree was previously use-only.)

Bugs caught and fixed inline by the post-commit reviewer round:
- auth login was accepting `http://` (empty host portion) because the
  old validateHost only checked the scheme. New cmdutil.NormalizeHost
  (shared by both login and context add) requires u.Host != "".
- context add's validateName claimed `..` was rejected but only denied
  / \\ space. Switched to positive allowlist [A-Za-z0-9._-] plus
  explicit ./../path-separator rejection.

Helper consolidation:
- cli/internal/cmdutil/host.go: NormalizeHost (trim, scheme, host
  non-empty) — both auth login and context add share it.
- cli/internal/format/dash.go: DashIfEmpty — promoted from copies in
  cmd/auth/list.go and cmd/context/list.go.
- recordingStore test stub dropped in favor of secrets.NewMemStore;
  contextKeyList test helper replaced by the existing contextKeys.

14 unit tests; 13 e2e branches verified.

Roadmap: 3-4.
This commit is contained in:
nullkey
2026-05-12 23:33:44 +08:00
committed by lyingbug
parent a7d30f73ca
commit 41a98b5743
11 changed files with 969 additions and 19 deletions
+1 -8
View File
@@ -76,7 +76,7 @@ func runList(opts *ListOptions, f *cmdutil.Factory) error {
if e.Current {
marker = "* "
}
fmt.Fprintf(tw, "%s%s\t%s\t%s\t%s\n", marker, e.Name, e.Host, dashIfEmpty(e.User), e.Mode)
fmt.Fprintf(tw, "%s%s\t%s\t%s\t%s\n", marker, e.Name, e.Host, format.DashIfEmpty(e.User), e.Mode)
}
return tw.Flush()
}
@@ -95,10 +95,3 @@ func inferMode(apiKeyRef, tokenRef string) string {
return "unknown"
}
}
func dashIfEmpty(s string) string {
if s == "" {
return "-"
}
return s
}
+2 -9
View File
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"net/url"
"strings"
"github.com/spf13/cobra"
@@ -217,14 +216,8 @@ func saveContextRef(opts *LoginOptions, f *cmdutil.Factory, ctx *config.Context,
// validateHost rejects empty / non-http URLs early so we surface a clean
// flag error instead of failing inside the SDK transport.
func validateHost(host string) error {
if host == "" {
return cmdutil.NewError(cmdutil.CodeInputMissingFlag, "--host is required")
}
u, err := url.Parse(host)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument, fmt.Sprintf("--host must be http(s) URL, got %q", host))
}
return nil
_, err := cmdutil.NormalizeHost(host)
return err
}
// readStdinTrimmed reads all of r and returns the result with surrounding