use std::path::Path;
use rho_providers::credentials::CredentialStore;
use super::tokens::TokenSet;
use crate::{
credential_store::UntrackedCredentialStore,
daemon::{account_lock_path, lock_account_file},
};
const LOCK_FILE: &str = ".rho-mcp-tokens.lock";
const GUARDED: &str = "MCP token";
pub(crate) fn update<T>(
change: impl FnOnce(&mut TokenSet) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
update_at(
&UntrackedCredentialStore,
&account_lock_path(LOCK_FILE, GUARDED)?,
change,
)
}
fn update_at<T>(
store: &dyn CredentialStore,
lock_path: &Path,
change: impl FnOnce(&mut TokenSet) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
let _lock = lock_account_file(lock_path, GUARDED)?;
let mut tokens = TokenSet::load(store)?;
let result = change(&mut tokens)?;
tokens.save(store)?;
Ok(result)
}
#[cfg(test)]
#[path = "token_store_tests.rs"]
mod tests;