1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
use anyhow::{Context, Result};
use clap;
use ngit::{
cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms},
client::Params,
git::{get_git_config_item, remove_git_config_item},
login::{SignerInfoSource, existing::load_existing_login},
};
use crate::{
cli::{Cli, extract_signer_cli_arguments},
client::{Client, Connect},
git::Repo,
login::fresh::fresh_login_or_signup,
};
#[derive(clap::Args)]
pub struct SubCommandArgs {
/// login to the local git repository only
#[arg(long, action)]
local: bool,
/// don't fetch user metadata and relay list from relays
#[arg(long, action)]
offline: bool,
/// signer relay for nostrconnect (can be used multiple times)
#[arg(long = "signer-relay")]
signer_relays: Vec<String>,
}
pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> {
// Early validation: check if we have required parameters in non-interactive
// mode
let signer_info = extract_signer_cli_arguments(args)?;
if Interactor::is_non_interactive() && signer_info.is_none() {
use ngit::cli_interactor::cli_error;
return Err(cli_error(
"requires --nsec or --interactive",
&[
("--nsec <key>", "provide secret key (nsec or hex)"),
("--interactive", "for nostr connect or bunker login"),
],
&[
"ngit account login --nsec <your-nsec>",
"ngit account create",
],
));
}
let git_repo_result = Repo::discover().context("failed to find a git repository");
let git_repo = { git_repo_result.ok() };
let client = if command_args.offline {
None
} else {
Some(Client::new(Params::with_git_config_relay_defaults(
&git_repo.as_ref(),
)))
};
let (logged_out, log_in_locally_only) = logout(git_repo.as_ref(), command_args.local).await?;
if logged_out || log_in_locally_only {
fresh_login_or_signup(
&git_repo.as_ref(),
client.as_ref(),
signer_info,
log_in_locally_only || command_args.local,
&command_args.signer_relays,
)
.await?;
}
// If not offline, disconnect the client
if let Some(client) = client {
client.disconnect().await?;
}
Ok(())
}
/// return ( bool - logged out, bool - log in to local git locally)
#[allow(clippy::too_many_lines)]
async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool)> {
for source in if local_only || std::env::var("NGITTEST").is_ok() {
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,
false,
)
.await
{
// In non-interactive mode, automatically logout without prompting
if Interactor::is_non_interactive() {
for item in [
"nostr.nsec",
"nostr.npub",
"nostr.bunker-uri",
"nostr.bunker-app-key",
] {
if let Err(_error) = remove_git_config_item(
if source == SignerInfoSource::GitLocal {
&git_repo
} else {
&None
},
item,
) {
use ngit::cli_interactor::cli_error;
return Err(cli_error(
&format!(
"failed to edit {} git config item '{item}'",
if source == SignerInfoSource::GitGlobal {
"global"
} else {
"local"
},
),
&[],
&["ngit account login --local --nsec <your-nsec>"],
));
}
}
return Ok((true, local_only));
}
// Interactive mode: prompt user for what to do
match Interactor::default().choice(
PromptChoiceParms::default()
.with_default(0)
.with_prompt(format!(
"logged in {}as {}",
if source == SignerInfoSource::GitLocal {
"to local git repository "
} else {
""
},
user_ref.metadata.name
))
.with_choices(if source == SignerInfoSource::GitGlobal {
vec![
"logout".to_string(),
"remain logged in".to_string(),
"login to local git repo only as another user".to_string(),
]
} else {
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",
] {
if let Err(error) = remove_git_config_item(
if source == SignerInfoSource::GitLocal {
&git_repo
} else {
&None
},
item,
) {
eprintln!("{error:?}");
eprintln!(
"consider manually removing {} git config items: {}",
if source == SignerInfoSource::GitGlobal {
"global"
} else {
"local"
},
format_items_as_list(&get_global_login_config_items_set())
);
match Interactor::default().choice(
PromptChoiceParms::default().with_default(0)
.with_prompt("failed to remove login details from global git config")
.with_choices(
vec![
"continue with global login to reveal what git config items to manually set".to_string(),
"login to this local repository with a different account".to_string(),
"cancel".to_string(),
]
),
)? {
0 => return Ok((true, false)),
1 => return Ok((true, true)),
_ => return Ok((false, local_only)),
}
}
}
}
1 => return Ok((false, local_only)),
_ => return Ok((false, true)),
}
}
}
Ok((true, local_only))
}
pub fn get_global_login_config_items_set() -> Vec<&'static str> {
[
"nostr.nsec",
"nostr.npub",
"nostr.bunker-uri",
"nostr.bunker-app-key",
]
.iter()
.copied()
.filter(|item| get_git_config_item(&None, item).is_ok_and(|item| item.is_some()))
.collect::<Vec<&str>>()
}
pub fn format_items_as_list(items: &[&str]) -> String {
match items.len() {
0 => String::new(),
1 => items[0].to_string(),
2 => format!("{} and {}", items[0], items[1]),
_ => {
let all_but_last = items[..items.len() - 1].join(", ");
format!("{}, and {}", all_but_last, items[items.len() - 1])
}
}
}
|