use super::{paths as claude, slot};
use crate::context::Context;
use crate::store::{Error, Live, RawStore};
use std::path::{Path, PathBuf};
pub(crate) fn credential_file(ctx: &Context) -> PathBuf {
PathBuf::from(claude::storage_dir(ctx)).join(slot::CRED_FILE)
}
pub(crate) fn chain(ctx: &Context) -> Live {
let host = ctx.host();
let mut backends: Vec<Box<dyn RawStore>> = Vec::new();
if let Some(keychain) = host.foreign_keychain(ctx, &slot::account_name(ctx)) {
backends.push(keychain);
}
backends.push(host.file(credential_file(ctx)));
Live::of(backends)
}
pub(crate) fn read_signin(ctx: &Context, dir: &Path) -> Result<Option<String>, Error> {
match ctx.host().foreign_keychain(ctx, &slot::account_name(ctx)) {
Some(keychain) => keychain.read(&slot::service_for_dir(&dir.to_string_lossy())),
None => ctx.host().file(dir.join(slot::CRED_FILE)).read(""),
}
}
pub(crate) fn discard_signin(ctx: &Context, dir: &Path) -> Result<(), Error> {
let service = slot::service_for_dir(&dir.to_string_lossy());
if service == slot::LIVE_SERVICE || service == claude::live_service(ctx) {
return Err(Error::Write(format!("refusing to delete {service}")));
}
match ctx.host().foreign_keychain(ctx, &slot::account_name(ctx)) {
Some(keychain) => keychain.delete(&service),
None => ctx.host().file(dir.join(slot::CRED_FILE)).delete(""),
}
}