diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-25 12:31:50 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-25 12:31:50 +0000 |
| commit | b09cf3ef8bbb5d31e4c424ff28282f5029f95fb2 (patch) | |
| tree | 7081e193a6dfa0dc076191dcd66ea974df69695b /src/bin/ngit/sub_commands/login.rs | |
| parent | 264c6dfe4229cd73bf017f55be0178d596cf06eb (diff) | |
fix(login): handle git config save errors
to guide the users how to login dispite the git config errors
Diffstat (limited to 'src/bin/ngit/sub_commands/login.rs')
| -rw-r--r-- | src/bin/ngit/sub_commands/login.rs | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index 1a70118..7bb0906 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs | |||
| @@ -2,7 +2,7 @@ use anyhow::{Context, Result}; | |||
| 2 | use clap; | 2 | use clap; |
| 3 | use ngit::{ | 3 | use ngit::{ |
| 4 | cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms}, | 4 | cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms}, |
| 5 | git::remove_git_config_item, | 5 | git::{get_git_config_item, remove_git_config_item}, |
| 6 | login::{existing::load_existing_login, SignerInfoSource}, | 6 | login::{existing::load_existing_login, SignerInfoSource}, |
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| @@ -101,14 +101,39 @@ async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool | |||
| 101 | "nostr.bunker-uri", | 101 | "nostr.bunker-uri", |
| 102 | "nostr.bunker-app-key", | 102 | "nostr.bunker-app-key", |
| 103 | ] { | 103 | ] { |
| 104 | remove_git_config_item( | 104 | if let Err(error) = remove_git_config_item( |
| 105 | if source == SignerInfoSource::GitLocal { | 105 | if source == SignerInfoSource::GitLocal { |
| 106 | &git_repo | 106 | &git_repo |
| 107 | } else { | 107 | } else { |
| 108 | &None | 108 | &None |
| 109 | }, | 109 | }, |
| 110 | item, | 110 | item, |
| 111 | )?; | 111 | ) { |
| 112 | eprintln!("{error:?}"); | ||
| 113 | |||
| 114 | eprintln!( | ||
| 115 | "consider manually removing {} git config items: {}", | ||
| 116 | if source == SignerInfoSource::GitGlobal { | ||
| 117 | "global" | ||
| 118 | } else { | ||
| 119 | "local" | ||
| 120 | }, | ||
| 121 | format_items_as_list(&get_global_login_config_items_set()) | ||
| 122 | ); | ||
| 123 | match Interactor::default().choice( | ||
| 124 | PromptChoiceParms::default().with_default(0).with_choices( | ||
| 125 | vec![ | ||
| 126 | "continue with global login to reveal what git config items to manually set".to_string(), | ||
| 127 | "login to this local repository with a different account".to_string(), | ||
| 128 | "cancel".to_string(), | ||
| 129 | ] | ||
| 130 | ), | ||
| 131 | )? { | ||
| 132 | 0 => return Ok((true, false)), | ||
| 133 | 1 => return Ok((true, true)), | ||
| 134 | _ => return Ok((false, local_only)), | ||
| 135 | } | ||
| 136 | } | ||
| 112 | } | 137 | } |
| 113 | } | 138 | } |
| 114 | 1 => return Ok((false, local_only)), | 139 | 1 => return Ok((false, local_only)), |
| @@ -118,3 +143,28 @@ async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool | |||
| 118 | } | 143 | } |
| 119 | Ok((true, local_only)) | 144 | Ok((true, local_only)) |
| 120 | } | 145 | } |
| 146 | |||
| 147 | fn get_global_login_config_items_set() -> Vec<&'static str> { | ||
| 148 | [ | ||
| 149 | "nostr.nsec", | ||
| 150 | "nostr.npub", | ||
| 151 | "nostr.bunker-uri", | ||
| 152 | "nostr.bunker-app-key", | ||
| 153 | ] | ||
| 154 | .iter() | ||
| 155 | .copied() | ||
| 156 | .filter(|item| get_git_config_item(&None, item).is_ok_and(|item| item.is_some())) | ||
| 157 | .collect::<Vec<&str>>() | ||
| 158 | } | ||
| 159 | |||
| 160 | fn format_items_as_list(items: &[&str]) -> String { | ||
| 161 | match items.len() { | ||
| 162 | 0 => String::new(), | ||
| 163 | 1 => items[0].to_string(), | ||
| 164 | 2 => format!("{} and {}", items[0], items[1]), | ||
| 165 | _ => { | ||
| 166 | let all_but_last = items[..items.len() - 1].join(", "); | ||
| 167 | format!("{}, and {}", all_but_last, items[items.len() - 1]) | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||