diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-27 08:55:31 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-27 08:55:31 +0000 |
| commit | c002cef1e5d19946244086531f1178446fed8545 (patch) | |
| tree | 39067fb72431b464a31d23adb3adee85443c04d4 | |
| parent | 242601c972b7186ad380d301b7c12ce9d19aa959 (diff) | |
feat(logout): add logout command
rather than using `ngit login` which is less intuative
| -rw-r--r-- | src/bin/ngit/cli.rs | 2 | ||||
| -rw-r--r-- | src/bin/ngit/main.rs | 1 | ||||
| -rw-r--r-- | src/bin/ngit/sub_commands/login.rs | 4 | ||||
| -rw-r--r-- | src/bin/ngit/sub_commands/logout.rs | 81 | ||||
| -rw-r--r-- | src/bin/ngit/sub_commands/mod.rs | 1 |
5 files changed, 87 insertions, 2 deletions
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 { | |||
| 69 | Pull, | 69 | Pull, |
| 70 | /// run with --nsec flag to change npub | 70 | /// run with --nsec flag to change npub |
| 71 | Login(sub_commands::login::SubCommandArgs), | 71 | Login(sub_commands::login::SubCommandArgs), |
| 72 | /// remove nostr account details stored in git config | ||
| 73 | Logout, | ||
| 72 | } | 74 | } |
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<()> { | |||
| 17 | match &cli.command { | 17 | match &cli.command { |
| 18 | Commands::Fetch(args) => sub_commands::fetch::launch(&cli, args).await, | 18 | Commands::Fetch(args) => sub_commands::fetch::launch(&cli, args).await, |
| 19 | Commands::Login(args) => sub_commands::login::launch(&cli, args).await, | 19 | Commands::Login(args) => sub_commands::login::launch(&cli, args).await, |
| 20 | Commands::Logout => sub_commands::logout::launch().await, | ||
| 20 | Commands::Init(args) => sub_commands::init::launch(&cli, args).await, | 21 | Commands::Init(args) => sub_commands::init::launch(&cli, args).await, |
| 21 | Commands::ExportKeys => sub_commands::export_keys::launch().await, | 22 | Commands::ExportKeys => sub_commands::export_keys::launch().await, |
| 22 | Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await, | 23 | 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 | |||
| 144 | Ok((true, local_only)) | 144 | Ok((true, local_only)) |
| 145 | } | 145 | } |
| 146 | 146 | ||
| 147 | fn get_global_login_config_items_set() -> Vec<&'static str> { | 147 | pub fn get_global_login_config_items_set() -> Vec<&'static str> { |
| 148 | [ | 148 | [ |
| 149 | "nostr.nsec", | 149 | "nostr.nsec", |
| 150 | "nostr.npub", | 150 | "nostr.npub", |
| @@ -157,7 +157,7 @@ fn get_global_login_config_items_set() -> Vec<&'static str> { | |||
| 157 | .collect::<Vec<&str>>() | 157 | .collect::<Vec<&str>>() |
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | fn format_items_as_list(items: &[&str]) -> String { | 160 | pub fn format_items_as_list(items: &[&str]) -> String { |
| 161 | match items.len() { | 161 | match items.len() { |
| 162 | 0 => String::new(), | 162 | 0 => String::new(), |
| 163 | 1 => items[0].to_string(), | 163 | 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 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use ngit::{ | ||
| 3 | git::remove_git_config_item, | ||
| 4 | login::{existing::load_existing_login, SignerInfoSource}, | ||
| 5 | }; | ||
| 6 | |||
| 7 | use crate::{ | ||
| 8 | git::Repo, | ||
| 9 | sub_commands::login::{format_items_as_list, get_global_login_config_items_set}, | ||
| 10 | }; | ||
| 11 | |||
| 12 | pub async fn launch() -> Result<()> { | ||
| 13 | let git_repo_result = Repo::discover().context("failed to find a git repository"); | ||
| 14 | let git_repo = { | ||
| 15 | match git_repo_result { | ||
| 16 | Ok(git_repo) => Some(git_repo), | ||
| 17 | Err(_) => None, | ||
| 18 | } | ||
| 19 | }; | ||
| 20 | logout(git_repo.as_ref()).await | ||
| 21 | } | ||
| 22 | |||
| 23 | async fn logout(git_repo: Option<&Repo>) -> Result<()> { | ||
| 24 | for source in if std::env::var("NGITTEST").is_ok() { | ||
| 25 | vec![SignerInfoSource::GitLocal] | ||
| 26 | } else { | ||
| 27 | vec![SignerInfoSource::GitLocal, SignerInfoSource::GitGlobal] | ||
| 28 | } { | ||
| 29 | if let Ok((_, user_ref, source)) = | ||
| 30 | load_existing_login(&git_repo, &None, &None, &Some(source), None, true, false).await | ||
| 31 | { | ||
| 32 | for item in [ | ||
| 33 | "nostr.nsec", | ||
| 34 | "nostr.npub", | ||
| 35 | "nostr.bunker-uri", | ||
| 36 | "nostr.bunker-app-key", | ||
| 37 | ] { | ||
| 38 | if let Err(error) = remove_git_config_item( | ||
| 39 | if source == SignerInfoSource::GitLocal { | ||
| 40 | &git_repo | ||
| 41 | } else { | ||
| 42 | &None | ||
| 43 | }, | ||
| 44 | item, | ||
| 45 | ) { | ||
| 46 | println!( | ||
| 47 | "failed to log out {}as {}", | ||
| 48 | if source == SignerInfoSource::GitLocal { | ||
| 49 | "from local git repository " | ||
| 50 | } else { | ||
| 51 | "" | ||
| 52 | }, | ||
| 53 | user_ref.metadata.name | ||
| 54 | ); | ||
| 55 | eprintln!("{error:?}"); | ||
| 56 | eprintln!( | ||
| 57 | "consider manually removing {} git config items: {}", | ||
| 58 | if source == SignerInfoSource::GitGlobal { | ||
| 59 | "global" | ||
| 60 | } else { | ||
| 61 | "local" | ||
| 62 | }, | ||
| 63 | format_items_as_list(&get_global_login_config_items_set()) | ||
| 64 | ); | ||
| 65 | return Ok(()); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | println!( | ||
| 69 | "logged out {}as {}", | ||
| 70 | if source == SignerInfoSource::GitLocal { | ||
| 71 | "from local git repository " | ||
| 72 | } else { | ||
| 73 | "" | ||
| 74 | }, | ||
| 75 | user_ref.metadata.name | ||
| 76 | ); | ||
| 77 | return Ok(()); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | Ok(()) | ||
| 81 | } | ||
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; | |||
| 3 | pub mod init; | 3 | pub mod init; |
| 4 | pub mod list; | 4 | pub mod list; |
| 5 | pub mod login; | 5 | pub mod login; |
| 6 | pub mod logout; | ||
| 6 | pub mod pull; | 7 | pub mod pull; |
| 7 | pub mod push; | 8 | pub mod push; |
| 8 | pub mod send; | 9 | pub mod send; |