diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-22 17:12:01 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-22 17:12:01 +0000 |
| commit | 00eecf19b92d161ab4d18ed4eac2cced1b9db028 (patch) | |
| tree | b3319a1fdf4845db36d4ef3bc2ef130390832f67 /src/bin/ngit | |
| parent | 737b7c5728d1688ace4d35a016fbdaed5ffc4e79 (diff) | |
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
Diffstat (limited to 'src/bin/ngit')
| -rw-r--r-- | src/bin/ngit/sub_commands/login.rs | 73 |
1 files changed, 65 insertions, 8 deletions
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 @@ | |||
| 1 | use anyhow::{Context, Result}; | 1 | use anyhow::{Context, Result}; |
| 2 | use clap; | 2 | use clap; |
| 3 | use ngit::{ | ||
| 4 | cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms}, | ||
| 5 | git::remove_git_config_item, | ||
| 6 | login::{existing::load_existing_login, SignerInfoSource}, | ||
| 7 | }; | ||
| 3 | 8 | ||
| 4 | use crate::{ | 9 | use crate::{ |
| 5 | cli::{extract_signer_cli_arguments, Cli}, | 10 | cli::{extract_signer_cli_arguments, Cli}, |
| @@ -29,20 +34,22 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | |||
| 29 | }; | 34 | }; |
| 30 | 35 | ||
| 31 | let git_repo_result = Repo::discover().context("cannot find a git repository"); | 36 | let git_repo_result = Repo::discover().context("cannot find a git repository"); |
| 32 | let git_repo_option = { | 37 | let git_repo = { |
| 33 | match git_repo_result { | 38 | match git_repo_result { |
| 34 | Ok(git_repo) => Some(git_repo), | 39 | Ok(git_repo) => Some(git_repo), |
| 35 | Err(_) => None, | 40 | Err(_) => None, |
| 36 | } | 41 | } |
| 37 | }; | 42 | }; |
| 38 | 43 | ||
| 39 | fresh_login_or_signup( | 44 | if logout(git_repo.as_ref(), command_args.local).await? { |
| 40 | &git_repo_option.as_ref(), | 45 | fresh_login_or_signup( |
| 41 | client.as_ref(), | 46 | &git_repo.as_ref(), |
| 42 | extract_signer_cli_arguments(args)?, | 47 | client.as_ref(), |
| 43 | command_args.local, | 48 | extract_signer_cli_arguments(args)?, |
| 44 | ) | 49 | command_args.local, |
| 45 | .await?; | 50 | ) |
| 51 | .await?; | ||
| 52 | } | ||
| 46 | 53 | ||
| 47 | // If not offline, disconnect the client | 54 | // If not offline, disconnect the client |
| 48 | if let Some(client) = client { | 55 | if let Some(client) = client { |
| @@ -50,3 +57,53 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | |||
| 50 | } | 57 | } |
| 51 | Ok(()) | 58 | Ok(()) |
| 52 | } | 59 | } |
| 60 | |||
| 61 | async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<bool> { | ||
| 62 | for source in if local_only { | ||
| 63 | vec![SignerInfoSource::GitLocal] | ||
| 64 | } else { | ||
| 65 | vec![SignerInfoSource::GitLocal, SignerInfoSource::GitGlobal] | ||
| 66 | } { | ||
| 67 | if let Ok((_, user_ref, source)) = | ||
| 68 | load_existing_login(&git_repo, &None, &None, &Some(source), None, true, false).await | ||
| 69 | { | ||
| 70 | eprintln!( | ||
| 71 | "logged in {}as {}", | ||
| 72 | if source == SignerInfoSource::GitLocal { | ||
| 73 | "to local git repository " | ||
| 74 | } else { | ||
| 75 | "" | ||
| 76 | }, | ||
| 77 | user_ref.metadata.name | ||
| 78 | ); | ||
| 79 | match Interactor::default().choice( | ||
| 80 | PromptChoiceParms::default() | ||
| 81 | .with_default(0) | ||
| 82 | .with_choices(vec![ | ||
| 83 | format!("logout as \"{}\"", user_ref.metadata.name), | ||
| 84 | "remain logged in".to_string(), | ||
| 85 | ]), | ||
| 86 | )? { | ||
| 87 | 0 => { | ||
| 88 | for item in [ | ||
| 89 | "nostr.nsec", | ||
| 90 | "nostr.npub", | ||
| 91 | "nostr.bunker-uri", | ||
| 92 | "nostr.bunker-app-key", | ||
| 93 | ] { | ||
| 94 | remove_git_config_item( | ||
| 95 | if source == SignerInfoSource::GitLocal { | ||
| 96 | &git_repo | ||
| 97 | } else { | ||
| 98 | &None | ||
| 99 | }, | ||
| 100 | item, | ||
| 101 | )?; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | _ => return Ok(false), | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | Ok(true) | ||
| 109 | } | ||