diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-10 13:10:18 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-10 13:10:18 +0000 |
| commit | 1e7aeb4d7972d29c6586df18128a8a4f7667845a (patch) | |
| tree | 0f7e5fcaa5a005aeec7ae2d9f35b2c473ef8f785 /src/lib/login | |
| parent | d2412565334f48bd31e57d29d7959c24258ccd98 (diff) | |
| parent | aae452697d152694a8f163219f707356e84b420b (diff) | |
Make ngit non-interactive by default
Implements non-interactive mode as the default behavior for ngit.
Users must now use -i flag for interactive prompts, or provide
all required arguments explicitly. Adds -d flag for sensible
defaults and -f flag for force operations.
Changes:
- CLI interactor infrastructure supports non-interactive mode
- Global flags: -i (interactive), --defaults (use defaults), -f (force)
- ngit init: requires --name or --identifier, supports --defaults
- ngit account: new signup command, login supports non-interactive
- ngit send: validates required fields, supports --defaults
- git-remote-nostr: fixed to prevent interactive prompts during push
- Comprehensive test coverage: 234 unit tests + integration tests
Diffstat (limited to 'src/lib/login')
| -rw-r--r-- | src/lib/login/fresh.rs | 179 |
1 files changed, 146 insertions, 33 deletions
diff --git a/src/lib/login/fresh.rs b/src/lib/login/fresh.rs index e01d4c3..886b0e4 100644 --- a/src/lib/login/fresh.rs +++ b/src/lib/login/fresh.rs | |||
| @@ -25,7 +25,7 @@ use crate::{ | |||
| 25 | Interactor, InteractorPrompt, Printer, PromptChoiceParms, PromptConfirmParms, | 25 | Interactor, InteractorPrompt, Printer, PromptChoiceParms, PromptConfirmParms, |
| 26 | PromptInputParms, PromptPasswordParms, | 26 | PromptInputParms, PromptPasswordParms, |
| 27 | }, | 27 | }, |
| 28 | client::{Connect, nip05_query, send_events}, | 28 | client::{Connect, nip05_query, save_event_in_global_cache, send_events}, |
| 29 | git::{Repo, RepoActions, remove_git_config_item, save_git_config_item}, | 29 | git::{Repo, RepoActions, remove_git_config_item, save_git_config_item}, |
| 30 | }; | 30 | }; |
| 31 | 31 | ||
| @@ -123,7 +123,7 @@ pub async fn get_fresh_nsec_signer() -> Result< | |||
| 123 | .input( | 123 | .input( |
| 124 | PromptInputParms::default() | 124 | PromptInputParms::default() |
| 125 | .with_prompt("nsec") | 125 | .with_prompt("nsec") |
| 126 | .optional() | 126 | .with_flag_name("--nsec") |
| 127 | .dont_report(), | 127 | .dont_report(), |
| 128 | ) | 128 | ) |
| 129 | .context("failed to get nsec input from interactor")?; | 129 | .context("failed to get nsec input from interactor")?; |
| @@ -509,6 +509,26 @@ async fn save_to_git_config( | |||
| 509 | if let Err(error) = | 509 | if let Err(error) = |
| 510 | silently_save_to_git_config(git_repo, signer_info, global).context(err_msg.clone()) | 510 | silently_save_to_git_config(git_repo, signer_info, global).context(err_msg.clone()) |
| 511 | { | 511 | { |
| 512 | // Check if this is a read-only file system error | ||
| 513 | let is_readonly_error = error | ||
| 514 | .chain() | ||
| 515 | .any(|e| e.to_string().contains("Read-only file system")); | ||
| 516 | |||
| 517 | if is_readonly_error && global { | ||
| 518 | // In non-interactive mode, provide a clear error with --local suggestion | ||
| 519 | if crate::cli_interactor::Interactor::is_non_interactive() { | ||
| 520 | use crate::cli_interactor::cli_error; | ||
| 521 | return Err(cli_error( | ||
| 522 | "failed to create account", | ||
| 523 | &[("cause", "global git config is read-only")], | ||
| 524 | &[ | ||
| 525 | "ngit account create --local --nsec <your-nsec>", | ||
| 526 | "ngit account login --local --nsec <your-nsec>", | ||
| 527 | ], | ||
| 528 | )); | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 512 | eprintln!("Error: {error:?}"); | 532 | eprintln!("Error: {error:?}"); |
| 513 | match signer_info { | 533 | match signer_info { |
| 514 | SignerInfo::Nsec { | 534 | SignerInfo::Nsec { |
| @@ -678,6 +698,119 @@ fn silently_save_to_git_config( | |||
| 678 | Ok(()) | 698 | Ok(()) |
| 679 | } | 699 | } |
| 680 | 700 | ||
| 701 | /// Non-interactive signup function for creating a new account | ||
| 702 | /// | ||
| 703 | /// # Arguments | ||
| 704 | /// * `name` - Display name for the new account | ||
| 705 | /// * `client` - Optional client for publishing metadata to relays | ||
| 706 | /// * `save_local` - If true, save credentials to local git config only | ||
| 707 | /// * `publish` - If true, publish metadata and relay list to relays | ||
| 708 | /// | ||
| 709 | /// # Returns | ||
| 710 | /// Returns a tuple of (signer, public_key, signer_info, keys) where keys can be | ||
| 711 | /// used to display the nsec | ||
| 712 | pub async fn signup_non_interactive( | ||
| 713 | name: String, | ||
| 714 | #[cfg(test)] client: Option<&MockConnect>, | ||
| 715 | #[cfg(not(test))] client: Option<&Client>, | ||
| 716 | save_local: bool, | ||
| 717 | publish: bool, | ||
| 718 | ) -> Result<(Arc<dyn NostrSigner>, PublicKey, SignerInfo, Keys)> { | ||
| 719 | // Generate new keypair | ||
| 720 | let keys = nostr::Keys::generate(); | ||
| 721 | let nsec = keys.secret_key().to_bech32()?; | ||
| 722 | let public_key = keys.public_key(); | ||
| 723 | |||
| 724 | let signer_info = SignerInfo::Nsec { | ||
| 725 | nsec, | ||
| 726 | password: None, | ||
| 727 | npub: Some(public_key.to_bech32()?), | ||
| 728 | }; | ||
| 729 | |||
| 730 | // Save to git config | ||
| 731 | let git_repo = Repo::discover().ok(); | ||
| 732 | if let Err(error) = silently_save_to_git_config(&git_repo.as_ref(), &signer_info, !save_local) { | ||
| 733 | let is_readonly = error | ||
| 734 | .chain() | ||
| 735 | .any(|e| e.to_string().contains("Read-only file system")); | ||
| 736 | |||
| 737 | if is_readonly && !save_local { | ||
| 738 | use crate::cli_interactor::cli_error; | ||
| 739 | |||
| 740 | let mut cmds: Vec<String> = match &signer_info { | ||
| 741 | SignerInfo::Nsec { nsec, npub, .. } => { | ||
| 742 | let mut v = vec![format!("git config --global nostr.nsec {nsec}")]; | ||
| 743 | if let Some(npub) = npub { | ||
| 744 | v.push(format!("git config --global nostr.npub {npub}")); | ||
| 745 | } | ||
| 746 | v | ||
| 747 | } | ||
| 748 | SignerInfo::Bunker { | ||
| 749 | bunker_uri, | ||
| 750 | bunker_app_key, | ||
| 751 | npub, | ||
| 752 | } => { | ||
| 753 | let mut v = vec![ | ||
| 754 | format!("git config --global nostr.bunker-uri {bunker_uri}"), | ||
| 755 | format!("git config --global nostr.bunker-app-key {bunker_app_key}"), | ||
| 756 | ]; | ||
| 757 | if let Some(npub) = npub { | ||
| 758 | v.push(format!("git config --global nostr.npub {npub}")); | ||
| 759 | } | ||
| 760 | v | ||
| 761 | } | ||
| 762 | }; | ||
| 763 | cmds.push("ngit account create --local --name <your-name>".to_string()); | ||
| 764 | |||
| 765 | let cmd_refs: Vec<&str> = cmds.iter().map(String::as_str).collect(); | ||
| 766 | return Err(cli_error( | ||
| 767 | "global git config is read-only. login to local repo or save git config manually", | ||
| 768 | &[("--local", "login scoped to this repositoriy")], | ||
| 769 | &cmd_refs, | ||
| 770 | )); | ||
| 771 | } | ||
| 772 | |||
| 773 | return Err(error); | ||
| 774 | } | ||
| 775 | |||
| 776 | let git_repo_path = if let Some(ref git_repo) = git_repo { | ||
| 777 | Some(git_repo.get_path()?) | ||
| 778 | } else { | ||
| 779 | None | ||
| 780 | }; | ||
| 781 | |||
| 782 | // Build events, save to cache, and optionally publish to relays | ||
| 783 | if let Some(client) = client { | ||
| 784 | let profile = EventBuilder::metadata(&Metadata::new().name(name)).sign_with_keys(&keys)?; | ||
| 785 | let relay_list = EventBuilder::relay_list( | ||
| 786 | client | ||
| 787 | .get_relay_default_set() | ||
| 788 | .iter() | ||
| 789 | .map(|s| (RelayUrl::parse(s).unwrap(), None)), | ||
| 790 | ) | ||
| 791 | .sign_with_keys(&keys)?; | ||
| 792 | |||
| 793 | // Save to global cache so subsequent commands don't need to fetch | ||
| 794 | save_event_in_global_cache(git_repo_path, &profile).await?; | ||
| 795 | save_event_in_global_cache(git_repo_path, &relay_list).await?; | ||
| 796 | |||
| 797 | if publish { | ||
| 798 | send_events( | ||
| 799 | client, | ||
| 800 | git_repo_path, | ||
| 801 | vec![profile, relay_list], | ||
| 802 | client.get_relay_default_set().clone(), | ||
| 803 | vec![], | ||
| 804 | true, | ||
| 805 | false, | ||
| 806 | ) | ||
| 807 | .await?; | ||
| 808 | } | ||
| 809 | } | ||
| 810 | |||
| 811 | Ok((Arc::new(keys.clone()), public_key, signer_info, keys)) | ||
| 812 | } | ||
| 813 | |||
| 681 | async fn signup( | 814 | async fn signup( |
| 682 | #[cfg(test)] client: Option<&MockConnect>, | 815 | #[cfg(test)] client: Option<&MockConnect>, |
| 683 | #[cfg(not(test))] client: Option<&Client>, | 816 | #[cfg(not(test))] client: Option<&Client>, |
| @@ -714,42 +847,22 @@ async fn signup( | |||
| 714 | _ => break Ok(None), | 847 | _ => break Ok(None), |
| 715 | } | 848 | } |
| 716 | } | 849 | } |
| 717 | let keys = nostr::Keys::generate(); | 850 | |
| 718 | let nsec = keys.secret_key().to_bech32()?; | 851 | // Call the non-interactive function |
| 852 | let (signer, public_key, signer_info, _keys) = signup_non_interactive( | ||
| 853 | name.clone(), | ||
| 854 | client, | ||
| 855 | false, // save_local = false (will be saved globally by caller) | ||
| 856 | true, // publish = true (always publish in interactive mode) | ||
| 857 | ) | ||
| 858 | .await?; | ||
| 859 | |||
| 719 | show_prompt_success("user display name", &name); | 860 | show_prompt_success("user display name", &name); |
| 720 | let signer_info = SignerInfo::Nsec { | ||
| 721 | nsec, | ||
| 722 | password: None, | ||
| 723 | npub: Some(keys.public_key().to_bech32()?), | ||
| 724 | }; | ||
| 725 | let public_key = keys.public_key(); | ||
| 726 | if let Some(client) = client { | ||
| 727 | let profile = | ||
| 728 | EventBuilder::metadata(&Metadata::new().name(name)).sign_with_keys(&keys)?; | ||
| 729 | let relay_list = EventBuilder::relay_list( | ||
| 730 | client | ||
| 731 | .get_relay_default_set() | ||
| 732 | .iter() | ||
| 733 | .map(|s| (RelayUrl::parse(s).unwrap(), None)), | ||
| 734 | ) | ||
| 735 | .sign_with_keys(&keys)?; | ||
| 736 | eprintln!("publishing user profile to relays"); | ||
| 737 | send_events( | ||
| 738 | client, | ||
| 739 | None, | ||
| 740 | vec![profile, relay_list], | ||
| 741 | client.get_relay_default_set().clone(), | ||
| 742 | vec![], | ||
| 743 | true, | ||
| 744 | false, | ||
| 745 | ) | ||
| 746 | .await?; | ||
| 747 | } | ||
| 748 | eprintln!( | 861 | eprintln!( |
| 749 | "to login to other nostr clients eg. gitworkshop.dev with this account run `ngit export-keys` at any time to reveal your nostr account secret" | 862 | "to login to other nostr clients eg. gitworkshop.dev with this account run `ngit export-keys` at any time to reveal your nostr account secret" |
| 750 | ); | 863 | ); |
| 751 | break Ok(Some(( | 864 | break Ok(Some(( |
| 752 | Arc::new(keys), | 865 | signer, |
| 753 | public_key, | 866 | public_key, |
| 754 | signer_info, | 867 | signer_info, |
| 755 | // TODO factor in source | 868 | // TODO factor in source |