use crate::auth;
use crate::cli::AccountsCommands;
use crate::config::Config;
use crate::error::Result;
use crate::output::{create_table, print_success, print_table};
use comfy_table::Cell;
pub async fn execute(command: &AccountsCommands) -> Result<()> {
match command {
AccountsCommands::List => list_accounts().await,
AccountsCommands::Switch { name } => switch_account(name).await,
AccountsCommands::Remove { name } => remove_account(name).await,
}
}
async fn list_accounts() -> Result<()> {
let config = Config::load()?;
if config.accounts.is_empty() {
println!("No accounts found. Use 'oauth-db login' to add an account.");
return Ok(());
}
let mut table = create_table();
table.set_header(vec!["Default", "Name", "Server", "Username", "Role"]);
for account in &config.accounts {
let default_marker = if account.default { "✓" } else { "" };
table.add_row(vec![
Cell::new(default_marker),
Cell::new(&account.name),
Cell::new(&account.server),
Cell::new(&account.username),
Cell::new(&account.role),
]);
}
print_table(table);
Ok(())
}
async fn switch_account(name: &str) -> Result<()> {
auth::switch_account(name)?;
print_success(&format!("Switched to account: {}", name));
Ok(())
}
async fn remove_account(name: &str) -> Result<()> {
auth::remove_account(name)?;
print_success(&format!("Removed account: {}", name));
Ok(())
}