From 00eecf19b92d161ab4d18ed4eac2cced1b9db028 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 22 Nov 2024 17:12:01 +0000 Subject: feat(login): `ngit login` prompt to logout if a local and or global account is already logged in, prompt to logout before overwriting with new login details --- src/bin/ngit/sub_commands/login.rs | 73 +++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 8 deletions(-) (limited to 'src/bin/ngit/sub_commands') diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index 390f431..df45975 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs @@ -1,5 +1,10 @@ use anyhow::{Context, Result}; use clap; +use ngit::{ + cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms}, + git::remove_git_config_item, + login::{existing::load_existing_login, SignerInfoSource}, +}; use crate::{ cli::{extract_signer_cli_arguments, Cli}, @@ -29,20 +34,22 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { }; let git_repo_result = Repo::discover().context("cannot find a git repository"); - let git_repo_option = { + let git_repo = { match git_repo_result { Ok(git_repo) => Some(git_repo), Err(_) => None, } }; - fresh_login_or_signup( - &git_repo_option.as_ref(), - client.as_ref(), - extract_signer_cli_arguments(args)?, - command_args.local, - ) - .await?; + if logout(git_repo.as_ref(), command_args.local).await? { + fresh_login_or_signup( + &git_repo.as_ref(), + client.as_ref(), + extract_signer_cli_arguments(args)?, + command_args.local, + ) + .await?; + } // If not offline, disconnect the client if let Some(client) = client { @@ -50,3 +57,53 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { } Ok(()) } + +async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result { + for source in if local_only { + 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 + { + eprintln!( + "logged in {}as {}", + if source == SignerInfoSource::GitLocal { + "to local git repository " + } else { + "" + }, + user_ref.metadata.name + ); + match Interactor::default().choice( + PromptChoiceParms::default() + .with_default(0) + .with_choices(vec![ + format!("logout as \"{}\"", user_ref.metadata.name), + "remain logged in".to_string(), + ]), + )? { + 0 => { + for item in [ + "nostr.nsec", + "nostr.npub", + "nostr.bunker-uri", + "nostr.bunker-app-key", + ] { + remove_git_config_item( + if source == SignerInfoSource::GitLocal { + &git_repo + } else { + &None + }, + item, + )?; + } + } + _ => return Ok(false), + } + } + } + Ok(true) +} -- cgit v1.2.3