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
+20 -5
View File
@@ -81,13 +81,18 @@ func TestRefresh_Happy(t *testing.T) {
assert.Equal(t, "new-refresh", gotRefresh)
}
func TestRefresh_NamedContext(t *testing.T) {
// TestRefresh_ActiveProfileViaOverride exercises refreshing a non-default
// profile. Production resolves this via the global --profile flag (which
// rewrites cfg.CurrentProfile in Factory.Config); here we set
// CurrentProfile=staging directly, since runRefresh's target is the active
// profile.
func TestRefresh_ActiveProfileViaOverride(t *testing.T) {
iostreams.SetForTest(t)
store := secrets.NewMemStore()
require.NoError(t, store.Set("staging", "refresh", "stg-refresh"))
cfg := &config.Config{
CurrentProfile: "prod",
CurrentProfile: "staging", // global --profile staging resolves to this
Profiles: map[string]config.Profile{
"prod": {Host: "https://prod", TokenRef: "mem://prod/access", RefreshRef: "mem://prod/refresh"},
"staging": {Host: "https://stg", TokenRef: "mem://staging/access", RefreshRef: "mem://staging/refresh"},
@@ -97,15 +102,25 @@ func TestRefresh_NamedContext(t *testing.T) {
svc := &fakeRefreshService{resp: &sdk.RefreshTokenResponse{
Success: true, AccessToken: "new-stg-access", RefreshToken: "new-stg-refresh",
}}
require.NoError(t, runRefresh(context.Background(), &RefreshOptions{Name: "staging"}, &cmdutil.FormatOptions{Mode: cmdutil.FormatText}, f, stubSvc(svc)))
require.NoError(t, runRefresh(context.Background(), &RefreshOptions{}, &cmdutil.FormatOptions{Mode: cmdutil.FormatText}, f, stubSvc(svc)))
assert.Equal(t, "stg-refresh", svc.gotTok, "--name=staging must refresh the staging profile, not current")
// current is untouched
assert.Equal(t, "stg-refresh", svc.gotTok, "active profile staging must be refreshed")
// prod (non-active) is untouched
if v, _ := store.Get("prod", "access"); v != "" {
t.Errorf("prod must not have been touched, got %q", v)
}
}
// TestRefresh_NoNameFlag asserts the --name flag is gone; refresh targets the
// active profile (override via the global --profile).
func TestRefresh_NoNameFlag(t *testing.T) {
iostreams.SetForTest(t)
cfg := &config.Config{Profiles: map[string]config.Profile{"a": {Host: "https://a"}}}
f := newRefreshFactory(t, cfg, secrets.NewMemStore())
cmd := NewCmdRefresh(f)
assert.Nil(t, cmd.Flags().Lookup("name"), "--name flag must be removed")
}
func TestRefresh_NoCurrentProfile(t *testing.T) {
iostreams.SetForTest(t)
f := newRefreshFactory(t, &config.Config{}, secrets.NewMemStore())