upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/sub_commands/logout.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-11-27 08:55:31 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-11-27 08:55:31 +0000
commitc002cef1e5d19946244086531f1178446fed8545 (patch)
tree39067fb72431b464a31d23adb3adee85443c04d4 /src/bin/ngit/sub_commands/logout.rs
parent242601c972b7186ad380d301b7c12ce9d19aa959 (diff)
feat(logout): add logout command
rather than using `ngit login` which is less intuative
Diffstat (limited to 'src/bin/ngit/sub_commands/logout.rs')
-rw-r--r--src/bin/ngit/sub_commands/logout.rs81
1 files changed, 81 insertions, 0 deletions
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 @@
1use anyhow::{Context, Result};
2use ngit::{
3 git::remove_git_config_item,
4 login::{existing::load_existing_login, SignerInfoSource},
5};
6
7use crate::{
8 git::Repo,
9 sub_commands::login::{format_items_as_list, get_global_login_config_items_set},
10};
11
12pub 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
23async 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}