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 | remove [--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, ", ") + ")" }