refactor(cli,auth): consolidate auth/profile model (BREAKING)

Auth now acts on the active profile instead of per-command --host/--name:
register a target with `profile add <name> --host <url> --use`, then
`auth login`. `auth login --with-token` reads an API key from stdin
(non-interactive, agent default); password login is interactive-only.

`auth token` prints the raw credential by default (clean $(...) capture);
--format json emits the {token,mode,profile} envelope, --jq implies json.
Login mode label corrected (api-key vs bearer). profile use and the auth/
profile read commands gain --dry-run / agent-help where they were missing.
This commit is contained in:
nullkey
2026-05-29 20:06:19 +08:00
committed by lyingbug
parent 6a1d0834c4
commit a68cc59310
17 changed files with 493 additions and 156 deletions
+17 -8
View File
@@ -32,12 +32,14 @@ func TestNewCmdAuth_TreeShape(t *testing.T) {
func TestNewCmdLogin_FlagsRegistered(t *testing.T) {
cmd := NewCmdLogin(&cmdutil.Factory{}, nil)
// --format is a persistent root flag (v0.7); only per-command flags here.
for _, name := range []string{"host", "name", "with-token"} {
assert.NotNilf(t, cmd.Flags().Lookup(name), "flag %s missing", name)
}
assert.NotNil(t, cmd.Flags().Lookup("with-token"), "flag with-token missing")
// login authenticates the active profile; host/name come from config
// (created via `profile add`), so these flags are gone.
assert.Nil(t, cmd.Flags().Lookup("host"), "auth login must not declare --host (v0.9)")
assert.Nil(t, cmd.Flags().Lookup("name"), "auth login must not declare --name (v0.9)")
// --profile is the global persistent override; local registration would
// silently shadow it.
assert.Nil(t, cmd.Flags().Lookup("profile"), "auth login must not declare a local --profile flag (use --name)")
assert.Nil(t, cmd.Flags().Lookup("profile"), "auth login must not declare a local --profile flag (use the global --profile)")
}
func TestNewCmdLogin_InvokesRunF(t *testing.T) {
@@ -49,11 +51,12 @@ func TestNewCmdLogin_InvokesRunF(t *testing.T) {
}
cmd := NewCmdLogin(f, func(_ context.Context, opts *LoginOptions, _ *cmdutil.FormatOptions, _ *cmdutil.Factory, _ LoginService) error {
called = true
assert.Equal(t, "https://kb.example.com", opts.Host)
// host/profile resolve inside runLogin from the active profile;
// the runF seam just receives the parsed flags.
assert.True(t, opts.WithToken)
return nil
})
cmd.SetArgs([]string{"--host", "https://kb.example.com", "--with-token"})
cmd.SetArgs([]string{"--with-token"})
require.NoError(t, cmd.Execute())
assert.True(t, called)
}
@@ -72,6 +75,12 @@ func TestPersistAPIKey_WritesContext(t *testing.T) {
Prompter: func() prompt.Prompter { return prompt.AgentPrompter{} },
Secrets: func() (secrets.Store, error) { return store, nil },
}
// persistAPIKey MERGES into the existing active profile record (created
// via `profile add`) rather than creating it; seed it first.
require.NoError(t, config.Save(&config.Config{
CurrentProfile: "ci",
Profiles: map[string]config.Profile{"ci": {Host: "https://kb.example.com"}},
}))
opts := &LoginOptions{
Host: "https://kb.example.com",
Profile: "ci",
@@ -81,8 +90,8 @@ func TestPersistAPIKey_WritesContext(t *testing.T) {
v, _ := store.Get("ci", "api_key")
assert.Equal(t, "sk-zzz", v)
cfg, _ := f.Config()
assert.Equal(t, "ci", cfg.CurrentProfile)
assert.Equal(t, "https://kb.example.com", cfg.Profiles["ci"].Host)
assert.Equal(t, "ci", cfg.CurrentProfile, "active profile unchanged by login")
assert.Equal(t, "https://kb.example.com", cfg.Profiles["ci"].Host, "existing host preserved by merge")
// APIKeyRef should be the mem:// URI from the store's Ref method.
assert.Equal(t, "mem://ci/api_key", cfg.Profiles["ci"].APIKeyRef)
}