auth clear option

This commit is contained in:
dal 2025-05-05 18:16:54 -06:00
parent 2412a4ac96
commit ef07010ff5
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
3 changed files with 44 additions and 2 deletions

View File

@ -6,7 +6,7 @@ use thiserror::Error;
use crate::utils::{ use crate::utils::{
buster::BusterClient, buster::BusterClient,
file::buster_credentials::{get_buster_credentials, set_buster_credentials, BusterCredentials}, file::buster_credentials::{get_buster_credentials, set_buster_credentials, delete_buster_credentials, BusterCredentials},
}; };
const DEFAULT_HOST: &str = "https://api.buster.so"; const DEFAULT_HOST: &str = "https://api.buster.so";
@ -24,7 +24,7 @@ pub enum AuthError {
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(about = "Authenticate with Buster API")] #[command(about = "Authenticate with Buster API")]
pub struct AuthArgs { pub struct AuthArgs {
/// The Buster API host URL /// The Buster API host UL
#[arg(long, env = "BUSTER_HOST")] #[arg(long, env = "BUSTER_HOST")]
pub host: Option<String>, pub host: Option<String>,
@ -35,6 +35,10 @@ pub struct AuthArgs {
/// Don't save credentials to disk /// Don't save credentials to disk
#[arg(long)] #[arg(long)]
pub no_save: bool, pub no_save: bool,
/// Clear saved credentials
#[arg(long)]
pub clear: bool,
} }
// --- Credentials Validation Trait --- // --- Credentials Validation Trait ---
@ -133,6 +137,19 @@ pub async fn check_authentication() -> Result<()> {
} }
pub async fn auth_with_args(args: AuthArgs) -> Result<()> { pub async fn auth_with_args(args: AuthArgs) -> Result<()> {
// Handle --clear flag first
if args.clear {
match delete_buster_credentials().await {
Ok(_) => {
println!("Saved credentials cleared successfully.");
return Ok(());
}
Err(e) => {
return Err(anyhow::anyhow!("Failed to clear credentials: {}", e));
}
}
}
// Get existing credentials or create default // Get existing credentials or create default
let mut buster_creds = match get_buster_credentials().await { let mut buster_creds = match get_buster_credentials().await {
Ok(creds) => creds, Ok(creds) => creds,

View File

@ -34,6 +34,10 @@ pub enum Commands {
/// Don't save credentials to disk /// Don't save credentials to disk
#[arg(long)] #[arg(long)]
no_save: bool, no_save: bool,
/// Clear saved credentials
#[arg(long)]
clear: bool,
}, },
/// Display version information /// Display version information
Version, Version,
@ -89,11 +93,13 @@ async fn main() {
host, host,
api_key, api_key,
no_save, no_save,
clear,
} => { } => {
commands::auth::auth_with_args(AuthArgs { commands::auth::auth_with_args(AuthArgs {
host, host,
api_key, api_key,
no_save, no_save,
clear,
}) })
.await .await
} }

View File

@ -42,6 +42,25 @@ pub async fn get_buster_credentials() -> Result<BusterCredentials, BusterError>
Ok(creds_yaml) Ok(creds_yaml)
} }
pub async fn delete_buster_credentials() -> Result<(), BusterError> {
let mut path = home_dir().unwrap_or_default();
path.push(".buster");
path.push("credentials.yml");
if path.exists() {
match fs::remove_file(&path).await {
Ok(_) => Ok(()),
Err(e) => Err(BusterError::FileWriteError {
path,
error: format!("Failed to delete credentials file: {}", e),
}),
}
} else {
// It's not an error if the file doesn't exist when trying to clear
Ok(())
}
}
pub async fn get_and_validate_buster_credentials() -> Result<BusterCredentials, BusterError> { pub async fn get_and_validate_buster_credentials() -> Result<BusterCredentials, BusterError> {
// Get the credentials. // Get the credentials.
let creds = match get_buster_credentials().await { let creds = match get_buster_credentials().await {