From ef07010ff590e1f2fbd99a873dcfcca256789a3a Mon Sep 17 00:00:00 2001 From: dal Date: Mon, 5 May 2025 18:16:54 -0600 Subject: [PATCH] auth clear option --- cli/cli/src/commands/auth.rs | 21 ++++++++++++++++++-- cli/cli/src/main.rs | 6 ++++++ cli/cli/src/utils/file/buster_credentials.rs | 19 ++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/cli/cli/src/commands/auth.rs b/cli/cli/src/commands/auth.rs index 49d231549..223b7f650 100644 --- a/cli/cli/src/commands/auth.rs +++ b/cli/cli/src/commands/auth.rs @@ -6,7 +6,7 @@ use thiserror::Error; use crate::utils::{ 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"; @@ -24,7 +24,7 @@ pub enum AuthError { #[derive(Parser, Debug)] #[command(about = "Authenticate with Buster API")] pub struct AuthArgs { - /// The Buster API host URL + /// The Buster API host UL #[arg(long, env = "BUSTER_HOST")] pub host: Option, @@ -35,6 +35,10 @@ pub struct AuthArgs { /// Don't save credentials to disk #[arg(long)] pub no_save: bool, + + /// Clear saved credentials + #[arg(long)] + pub clear: bool, } // --- Credentials Validation Trait --- @@ -133,6 +137,19 @@ pub async fn check_authentication() -> 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 let mut buster_creds = match get_buster_credentials().await { Ok(creds) => creds, diff --git a/cli/cli/src/main.rs b/cli/cli/src/main.rs index eca7ef282..6117594ed 100644 --- a/cli/cli/src/main.rs +++ b/cli/cli/src/main.rs @@ -34,6 +34,10 @@ pub enum Commands { /// Don't save credentials to disk #[arg(long)] no_save: bool, + + /// Clear saved credentials + #[arg(long)] + clear: bool, }, /// Display version information Version, @@ -89,11 +93,13 @@ async fn main() { host, api_key, no_save, + clear, } => { commands::auth::auth_with_args(AuthArgs { host, api_key, no_save, + clear, }) .await } diff --git a/cli/cli/src/utils/file/buster_credentials.rs b/cli/cli/src/utils/file/buster_credentials.rs index 6bcc76a06..eb26e1fd3 100644 --- a/cli/cli/src/utils/file/buster_credentials.rs +++ b/cli/cli/src/utils/file/buster_credentials.rs @@ -42,6 +42,25 @@ pub async fn get_buster_credentials() -> Result 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 { // Get the credentials. let creds = match get_buster_credentials().await {