diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-27 07:41:25 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-11-27 07:41:25 +0000 |
| commit | c92e252adc9990fa4d147bad0a8bccafc19dfbb8 (patch) | |
| tree | 4bea092f25cd8c06ca0ad11a691cf623a24ff9cb /src/bin/ngit | |
| parent | bdf71cb3d5a5ff8399c10c8d2492d3dd01c5fa33 (diff) | |
feat(export-keys): to use in other clients
as part of the easy on-boaridng flow
Diffstat (limited to 'src/bin/ngit')
| -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/export_keys.rs | 89 | ||||
| -rw-r--r-- | src/bin/ngit/sub_commands/login.rs | 2 | ||||
| -rw-r--r-- | src/bin/ngit/sub_commands/mod.rs | 1 |
5 files changed, 93 insertions, 2 deletions
diff --git a/src/bin/ngit/cli.rs b/src/bin/ngit/cli.rs index b243f70..c5cbcfa 100644 --- a/src/bin/ngit/cli.rs +++ b/src/bin/ngit/cli.rs | |||
| @@ -57,6 +57,8 @@ pub enum Commands { | |||
| 57 | Fetch(sub_commands::fetch::SubCommandArgs), | 57 | Fetch(sub_commands::fetch::SubCommandArgs), |
| 58 | /// signal you are this repo's maintainer accepting proposals via nostr | 58 | /// signal you are this repo's maintainer accepting proposals via nostr |
| 59 | Init(sub_commands::init::SubCommandArgs), | 59 | Init(sub_commands::init::SubCommandArgs), |
| 60 | /// export nostr keys to login to other nostr clients | ||
| 61 | ExportKeys, | ||
| 60 | /// issue commits as a proposal | 62 | /// issue commits as a proposal |
| 61 | Send(sub_commands::send::SubCommandArgs), | 63 | Send(sub_commands::send::SubCommandArgs), |
| 62 | /// list proposals; checkout, apply or download selected | 64 | /// list proposals; checkout, apply or download selected |
diff --git a/src/bin/ngit/main.rs b/src/bin/ngit/main.rs index 45cbef5..53fee06 100644 --- a/src/bin/ngit/main.rs +++ b/src/bin/ngit/main.rs | |||
| @@ -18,6 +18,7 @@ async fn main() -> Result<()> { | |||
| 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::Init(args) => sub_commands::init::launch(&cli, args).await, | 20 | Commands::Init(args) => sub_commands::init::launch(&cli, args).await, |
| 21 | Commands::ExportKeys => sub_commands::export_keys::launch().await, | ||
| 21 | Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await, | 22 | Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await, |
| 22 | Commands::List => sub_commands::list::launch().await, | 23 | Commands::List => sub_commands::list::launch().await, |
| 23 | Commands::Pull => sub_commands::pull::launch().await, | 24 | Commands::Pull => sub_commands::pull::launch().await, |
diff --git a/src/bin/ngit/sub_commands/export_keys.rs b/src/bin/ngit/sub_commands/export_keys.rs new file mode 100644 index 0000000..12fc380 --- /dev/null +++ b/src/bin/ngit/sub_commands/export_keys.rs | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use ngit::{ | ||
| 3 | cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms}, | ||
| 4 | login::{ | ||
| 5 | existing::{get_signer_info, load_existing_login}, | ||
| 6 | fresh::generate_qr, | ||
| 7 | SignerInfo, SignerInfoSource, | ||
| 8 | }, | ||
| 9 | }; | ||
| 10 | |||
| 11 | use crate::git::Repo; | ||
| 12 | |||
| 13 | pub async fn launch() -> Result<()> { | ||
| 14 | let git_repo_result = Repo::discover().context("failed to find a git repository"); | ||
| 15 | let git_repo = { | ||
| 16 | match git_repo_result { | ||
| 17 | Ok(git_repo) => Some(git_repo), | ||
| 18 | Err(_) => None, | ||
| 19 | } | ||
| 20 | }; | ||
| 21 | |||
| 22 | if let Ok((signer_info, source)) = get_signer_info(&git_repo.as_ref(), &None, &None, &None) { | ||
| 23 | if let Ok((_, user_ref, source)) = load_existing_login( | ||
| 24 | &git_repo.as_ref(), | ||
| 25 | &None, | ||
| 26 | &None, | ||
| 27 | &Some(source), | ||
| 28 | None, | ||
| 29 | true, | ||
| 30 | false, | ||
| 31 | ) | ||
| 32 | .await | ||
| 33 | { | ||
| 34 | let logged_in_msg = format!( | ||
| 35 | "logged in {}as {}", | ||
| 36 | if source == SignerInfoSource::GitLocal { | ||
| 37 | "to local git repository " | ||
| 38 | } else { | ||
| 39 | "" | ||
| 40 | }, | ||
| 41 | user_ref.metadata.name | ||
| 42 | ); | ||
| 43 | match signer_info { | ||
| 44 | SignerInfo::Bunker { | ||
| 45 | bunker_uri: _, | ||
| 46 | bunker_app_key: _, | ||
| 47 | npub: _, | ||
| 48 | } => { | ||
| 49 | eprintln!( | ||
| 50 | "failed: {logged_in_msg} using nostr connect so your keys are stored in a remote signer" | ||
| 51 | ); | ||
| 52 | return Ok(()); | ||
| 53 | } | ||
| 54 | SignerInfo::Nsec { | ||
| 55 | nsec, | ||
| 56 | password: _, | ||
| 57 | npub: _, | ||
| 58 | } => { | ||
| 59 | match Interactor::default().choice( | ||
| 60 | PromptChoiceParms::default() | ||
| 61 | .with_default(0) | ||
| 62 | .with_prompt(logged_in_msg) | ||
| 63 | .with_choices(vec![ | ||
| 64 | "print nsec".to_string(), | ||
| 65 | "show QR code of nsec".to_string(), | ||
| 66 | "cancel".to_string(), | ||
| 67 | ]), | ||
| 68 | )? { | ||
| 69 | 0 => { | ||
| 70 | println!("{nsec}"); | ||
| 71 | return Ok(()); | ||
| 72 | } | ||
| 73 | 1 => { | ||
| 74 | for line in generate_qr(&nsec)? { | ||
| 75 | println!("{line}"); | ||
| 76 | } | ||
| 77 | return Ok(()); | ||
| 78 | } | ||
| 79 | _ => { | ||
| 80 | return Ok(()); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | eprintln!("not logged in so no keys are stored"); | ||
| 88 | Ok(()) | ||
| 89 | } | ||
diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index ff58ec6..afde145 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs | |||
| @@ -25,8 +25,6 @@ pub struct SubCommandArgs { | |||
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | 27 | pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { |
| 28 | // TODO show existing login on record, prompt to logout | ||
| 29 | |||
| 30 | let client = if command_args.offline { | 28 | let client = if command_args.offline { |
| 31 | None | 29 | None |
| 32 | } else { | 30 | } else { |
diff --git a/src/bin/ngit/sub_commands/mod.rs b/src/bin/ngit/sub_commands/mod.rs index 29a60f9..ac89d47 100644 --- a/src/bin/ngit/sub_commands/mod.rs +++ b/src/bin/ngit/sub_commands/mod.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | pub mod export_keys; | ||
| 1 | pub mod fetch; | 2 | pub mod fetch; |
| 2 | pub mod init; | 3 | pub mod init; |
| 3 | pub mod list; | 4 | pub mod list; |