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
+28 -2
View File
@@ -55,11 +55,37 @@ to see which mode each profile uses, and construct the matching HTTP header:
if err != nil {
return err
}
fopts.ResolveDefault(iostreams.IO.IsStdoutTTY())
// `auth token` is a scalar scripting helper (WEKNORA_TOKEN=$(...)),
// so it defaults to the raw token — overriding the global json
// default (gh auth token does the same; cf. doc download streaming
// raw bytes). Explicit --format json / WEKNORA_FORMAT=json still
// emit the {token,mode,profile} envelope.
fopts.FromEnv()
if fopts.Mode == "" {
// --jq implies JSON (it filters the envelope); without it,
// default to the raw token for clean $(...) capture.
if fopts.JQ != "" {
fopts.Mode = cmdutil.FormatJSON
} else {
fopts.Mode = cmdutil.FormatText
}
}
return runToken(f, fopts)
},
}
cmdutil.AddFormatFlag(cmd, authTokenFields...)
cmdutil.SetAgentHelp(cmd, cmdutil.AgentHelp{
UsedFor: "print the active profile's raw credential to stdout for shell capture",
Examples: []string{
"WEKNORA_TOKEN=$(weknora auth token)",
"weknora auth token --profile staging",
"weknora auth token --format json",
},
Output: "raw token on stdout (no envelope, no trailing newline) by default; --format json emits {token, mode, profile}",
Warnings: []string{
"default output is the bare token, NOT the JSON envelope — it is a scripting helper (cf. `gh auth token`)",
},
})
return cmd
}
@@ -74,7 +100,7 @@ func runToken(f *cmdutil.Factory, fopts *cmdutil.FormatOptions) error {
}
if profileName == "" {
return cmdutil.NewError(cmdutil.CodeAuthUnauthenticated,
"no current profile configured")
"no active profile configured")
}
ctx, ok := cfg.Profiles[profileName]
if !ok {