c490f71042
Saved accounts live under <CLAUDE_CONFIG_DIR>/pipelines-accounts/<slug>/ (0700 dir, 0600 files). Switching changes what /login changes: the claudeAiOauth entry of .credentials.json and oauthAccount of .claude.json (atomic temp+rename, mode and other keys kept) and the org caches /login drops, under Claude Code's own refresh, storage and config locks, so running CLIs continue on the new account without a restart. The active login's rotated tokens are written back to its slot first. Server: claude_accounts / claude_account_save / _use / _remove, chat-scoped and Claude-only; a switch moves the home's open chats to the new account's quota (usage.account.switched_at), clearing a reached limit. CLI: pipelines claude-account list|save|use|remove [--dir]. App: Claude account in the chat menu, and Switch to <account> on the usage-limit and usage-warning banners, with a confirmation snack. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/claude"
|
|
"git.a-wan.net/alan/pipelines/internal/claudeaccounts"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/workenv"
|
|
)
|
|
|
|
const claudeAccountUsage = "usage: pipelines claude-account list | save [name] | use <name> | remove <name> [--dir DIR] [--json]"
|
|
|
|
// claudeAccount saves and switches the Claude login of the Claude home a
|
|
// directory selects (its CLAUDE_CONFIG_DIR after direnv/mise), locally and
|
|
// without a server. It changes the same files /login does, so running Claude
|
|
// Code processes there continue on the new account.
|
|
func (a App) claudeAccount(ctx context.Context, args []string) error {
|
|
f := flag.NewFlagSet("claude-account", flag.ContinueOnError)
|
|
f.SetOutput(a.Err)
|
|
dir := f.String("dir", "", "Directory whose environment selects the Claude home (default: current directory)")
|
|
jsonMode := f.Bool("json", false, "Machine-readable JSON")
|
|
if err := parse(f, args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
pos := f.Args()
|
|
if len(pos) == 0 {
|
|
return errors.New(claudeAccountUsage)
|
|
}
|
|
directory := *dir
|
|
if directory == "" {
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
directory = wd
|
|
}
|
|
directory, err := filepath.Abs(directory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
configDir, err := claude.AccountConfigDir(ctx, workenv.Default, directory, os.Environ())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
store := claudeaccounts.Store{ConfigDir: configDir}
|
|
var list protocol.ClaudeAccountList
|
|
switch {
|
|
case pos[0] == "list" && len(pos) == 1:
|
|
list, err = store.List()
|
|
case pos[0] == "save" && len(pos) <= 2:
|
|
list, err = store.Save(strings.Join(pos[1:], ""))
|
|
case pos[0] == "use" && len(pos) == 2:
|
|
list, err = store.Use(pos[1])
|
|
case pos[0] == "remove" && len(pos) == 2:
|
|
list, err = store.Remove(pos[1])
|
|
default:
|
|
return errors.New(claudeAccountUsage)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if *jsonMode {
|
|
return a.output(list)
|
|
}
|
|
fmt.Fprintf(a.Out, "Claude home: %s\n", list.ConfigDir)
|
|
if len(list.Accounts) == 0 {
|
|
fmt.Fprintln(a.Out, "No saved accounts.")
|
|
}
|
|
for _, account := range list.Accounts {
|
|
mark := " "
|
|
if account.Active {
|
|
mark = "*"
|
|
}
|
|
fmt.Fprintf(a.Out, "%s %s%s\n", mark, account.Name, accountDetail(account))
|
|
}
|
|
if list.Active != nil && !list.Active.Saved {
|
|
fmt.Fprintf(a.Out, "* %s (signed in, not saved; pipelines claude-account save)\n", list.Active.Email)
|
|
}
|
|
if list.Active == nil {
|
|
fmt.Fprintln(a.Out, "Signed out.")
|
|
}
|
|
if pos[0] == "use" && list.Active != nil {
|
|
fmt.Fprintf(a.Out, "Switched to %s. Running Claude Code sessions continue on it.\n", list.Active.Email)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func accountDetail(account protocol.ClaudeAccount) string {
|
|
var parts []string
|
|
if account.Email != "" && account.Email != account.Name {
|
|
parts = append(parts, account.Email)
|
|
}
|
|
if account.Organization != "" {
|
|
parts = append(parts, account.Organization)
|
|
}
|
|
if account.Plan != "" {
|
|
parts = append(parts, account.Plan)
|
|
}
|
|
if len(parts) == 0 {
|
|
return ""
|
|
}
|
|
return " (" + strings.Join(parts, ", ") + ")"
|
|
}
|