switcher

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package switcher swaps which account Claude Code is logged in as.

ccswap owns only the claudeAiOauth key of Claude Code's entry. Vault invariant: nothing refreshes a vault copy of the chain in Claude Code's entry; such a copy is parked after a switch until the daemon sees the switch settled and drops it.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNearRefresh = errors.New("access token is about to be refreshed by Claude Code")
	// ErrTargetRefreshing: the target's vault copy has no access token yet; the daemon is refreshing it.
	ErrTargetRefreshing = errors.New("the target login is still being refreshed")
	ErrUnknownActive    = errors.New("the current Claude Code login is not a known account")
	ErrNeedsLogin       = errors.New("account needs a fresh login")
	ErrNoSuchAccount    = errors.New("no such account")
	ErrCannotIdentify   = errors.New("cannot identify the current Claude Code login")
	ErrHolderChanged    = errors.New("the Claude Code login changed since the decision")
	// ErrEntryChanged: Claude Code kept rewriting its login while the switch was ready to write.
	ErrEntryChanged = errors.New("the Claude Code login kept changing during the switch")
	// ErrEntryExpired: Claude Code has not run for a while, so its access token expired and the
	// profile cannot name the login. It is an ErrCannotIdentify.
	ErrEntryExpired = fmt.Errorf("%w: Claude Code's access token has expired", ErrCannotIdentify)
	// ErrFrozen: a migrate export holds the account, so no switch may reach it.
	ErrFrozen = errors.New("the account is being moved to another machine")
)

Functions

This section is empty.

Types

type Deps

type Deps struct {
	KC                             keychain.Store
	Store                          *store.Store
	Profiles                       Profiler
	Service, KCAccount, ClaudeJSON string
	RefreshGuard                   time.Duration
	Now                            func() time.Time
	// ClaudeDir is Claude Code's config dir, whose locks guard the entry; "" takes no locks (tests).
	ClaudeDir string
}

type Profiler

type Profiler interface {
	Profile(ctx context.Context, accessToken string) (anthropic.Profile, error)
}

type Switcher

type Switcher struct {
	// contains filtered or unexported fields
}

func New

func New(d Deps) *Switcher

func (*Switcher) Adopt

func (s *Switcher) Adopt(ctx context.Context, name string, priority int) error

Adopt registers the login currently in Claude Code's entry as account name and marks it active. It writes metadata only: the active account has no vault item.

func (*Switcher) ForgetLocked

func (s *Switcher) ForgetLocked(name string)

ForgetLocked drops the fingerprint when it names name (the account is being removed). The caller holds Locked.

func (*Switcher) Freeze

func (s *Switcher) Freeze(names []string, until time.Time)

Freeze makes switches to names fail with ErrFrozen until the deadline.

func (*Switcher) Frozen

func (s *Switcher) Frozen(name string) bool

Frozen reports whether name is frozen now; an expired entry is dropped.

func (*Switcher) Identify

Identify names the account whose login is in Claude Code's entry, and returns the entry as it read it.

func (*Switcher) LastWrite

func (s *Switcher) LastWrite() time.Time

LastWrite is when ccswap last wrote Claude Code's entry (zero: not since start).

func (*Switcher) Locked

func (s *Switcher) Locked(fn func() error) error

Locked runs fn under the switcher's mutex; every vault refresh must go through it.

func (*Switcher) Switch

func (s *Switcher) Switch(ctx context.Context, to string, force bool) (warning error, err error)

Switch puts to's login in Claude Code's entry. A switch that landed but left something undone returns a warning: an *i18n.Error, or several of them joined (errors.Join).

func (*Switcher) SwitchFrom

func (s *Switcher) SwitchFrom(ctx context.Context, from, to string) (warning error, err error)

SwitchFrom is Switch(to, false) that writes nothing (ErrHolderChanged) unless Claude Code's entry still holds from: an automatic decision made for from is void once someone else switched.

func (*Switcher) Thaw

func (s *Switcher) Thaw(names []string)

Thaw lifts the freeze on names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL