From c002cef1e5d19946244086531f1178446fed8545 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 27 Nov 2024 08:55:31 +0000 Subject: feat(logout): add logout command rather than using `ngit login` which is less intuative --- src/bin/ngit/cli.rs | 2 + src/bin/ngit/main.rs | 1 + src/bin/ngit/sub_commands/login.rs | 4 +- src/bin/ngit/sub_commands/logout.rs | 81 +++++++++++++++++++++++++++++++++++++ src/bin/ngit/sub_commands/mod.rs | 1 + 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/bin/ngit/sub_commands/logout.rs (limited to 'src') diff --git a/src/bin/ngit/cli.rs b/src/bin/ngit/cli.rs index c5cbcfa..fce5664 100644 --- a/src/bin/ngit/cli.rs +++ b/src/bin/ngit/cli.rs @@ -69,4 +69,6 @@ pub enum Commands { Pull, /// run with --nsec flag to change npub Login(sub_commands::login::SubCommandArgs), + /// remove nostr account details stored in git config + Logout, } diff --git a/src/bin/ngit/main.rs b/src/bin/ngit/main.rs index 53fee06..cdd0e97 100644 --- a/src/bin/ngit/main.rs +++ b/src/bin/ngit/main.rs @@ -17,6 +17,7 @@ async fn main() -> Result<()> { match &cli.command { Commands::Fetch(args) => sub_commands::fetch::launch(&cli, args).await, Commands::Login(args) => sub_commands::login::launch(&cli, args).await, + Commands::Logout => sub_commands::logout::launch().await, Commands::Init(args) => sub_commands::init::launch(&cli, args).await, Commands::ExportKeys => sub_commands::export_keys::launch().await, Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await, diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index afde145..7670bc3 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs @@ -144,7 +144,7 @@ async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool Ok((true, local_only)) } -fn get_global_login_config_items_set() -> Vec<&'static str> { +pub fn get_global_login_config_items_set() -> Vec<&'static str> { [ "nostr.nsec", "nostr.npub", @@ -157,7 +157,7 @@ fn get_global_login_config_items_set() -> Vec<&'static str> { .collect::>() } -fn format_items_as_list(items: &[&str]) -> String { +pub fn format_items_as_list(items: &[&str]) -> String { match items.len() { 0 => String::new(), 1 => items[0].to_string(), diff --git a/src/bin/ngit/sub_commands/logout.rs b/src/bin/ngit/sub_commands/logout.rs new file mode 100644 index 0000000..682c017 --- /dev/null +++ b/src/bin/ngit/sub_commands/logout.rs @@ -0,0 +1,81 @@ +use anyhow::{Context, Result}; +use ngit::{ + git::remove_git_config_item, + login::{existing::load_existing_login, SignerInfoSource}, +}; + +use crate::{ + git::Repo, + sub_commands::login::{format_items_as_list, get_global_login_config_items_set}, +}; + +pub async fn launch() -> Result<()> { + let git_repo_result = Repo::discover().context("failed to find a git repository"); + let git_repo = { + match git_repo_result { + Ok(git_repo) => Some(git_repo), + Err(_) => None, + } + }; + logout(git_repo.as_ref()).await +} + +async fn logout(git_repo: Option<&Repo>) -> Result<()> { + for source in if std::env::var("NGITTEST").is_ok() { + vec![SignerInfoSource::GitLocal] + } else { + vec![SignerInfoSource::GitLocal, SignerInfoSource::GitGlobal] + } { + if let Ok((_, user_ref, source)) = + load_existing_login(&git_repo, &None, &None, &Some(source), None, true, false).await + { + for item in [ + "nostr.nsec", + "nostr.npub", + "nostr.bunker-uri", + "nostr.bunker-app-key", + ] { + if let Err(error) = remove_git_config_item( + if source == SignerInfoSource::GitLocal { + &git_repo + } else { + &None + }, + item, + ) { + println!( + "failed to log out {}as {}", + if source == SignerInfoSource::GitLocal { + "from local git repository " + } else { + "" + }, + user_ref.metadata.name + ); + eprintln!("{error:?}"); + eprintln!( + "consider manually removing {} git config items: {}", + if source == SignerInfoSource::GitGlobal { + "global" + } else { + "local" + }, + format_items_as_list(&get_global_login_config_items_set()) + ); + return Ok(()); + } + } + println!( + "logged out {}as {}", + if source == SignerInfoSource::GitLocal { + "from local git repository " + } else { + "" + }, + user_ref.metadata.name + ); + return Ok(()); + } + } + Ok(()) +} diff --git a/src/bin/ngit/sub_commands/mod.rs b/src/bin/ngit/sub_commands/mod.rs index ac89d47..b6b1e3b 100644 --- a/src/bin/ngit/sub_commands/mod.rs +++ b/src/bin/ngit/sub_commands/mod.rs @@ -3,6 +3,7 @@ pub mod fetch; pub mod init; pub mod list; pub mod login; +pub mod logout; pub mod pull; pub mod push; pub mod send; -- cgit v1.2.3