mirror of https://github.com/buster-so/buster.git
auth clear option
This commit is contained in:
parent
2412a4ac96
commit
ef07010ff5
|
@ -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<String>,
|
||||
|
||||
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -42,6 +42,25 @@ pub async fn get_buster_credentials() -> Result<BusterCredentials, BusterError>
|
|||
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> {
|
||||
// Get the credentials.
|
||||
let creds = match get_buster_credentials().await {
|
||||
|
|
Loading…
Reference in New Issue