package claudeaccounts import ( "encoding/json" "errors" "io/fs" "os" "path/filepath" "git.a-wan.net/alan/pipelines/internal/protocol" ) // settingsFile holds the Claude home's account-switching preferences, beside // the saved slots (a file, so slots() never reads it as an account). const settingsFile = "settings.json" type settings struct { // AutoSwitch is nil until the operator chooses; the default is on. AutoSwitch *bool `json:"auto_switch,omitempty"` } func (s Store) readSettings() settings { var out settings data, err := os.ReadFile(filepath.Join(s.slotsDir(), settingsFile)) if err == nil { _ = json.Unmarshal(data, &out) } return out } // AutoSwitch reports whether a chat on this Claude home that runs out of // usage is switched to another saved account automatically (on by default). func (s Store) AutoSwitch() bool { if on := s.readSettings().AutoSwitch; on != nil { return *on } return true } // SetAutoSwitch records the automatic-switch preference and returns the // accounts list, which reports it. func (s Store) SetAutoSwitch(on bool) (protocol.ClaudeAccountList, error) { if err := s.check(); err != nil { return protocol.ClaudeAccountList{}, err } dir := s.slotsDir() if err := os.Mkdir(dir, 0o700); err != nil && !errors.Is(err, fs.ErrExist) { return protocol.ClaudeAccountList{}, err } current := s.readSettings() current.AutoSwitch = &on data, err := marshal(current) if err != nil { return protocol.ClaudeAccountList{}, err } path := filepath.Join(dir, settingsFile) if err := writeAtomic(path, data, 0o600); err != nil { return protocol.ClaudeAccountList{}, err } if err := os.Chmod(path, 0o600); err != nil { return protocol.ClaudeAccountList{}, err } return s.List() }