diff options
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/ngit/sub_commands/login.rs | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index ed2414a..9081e66 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs | |||
| @@ -26,6 +26,24 @@ pub struct SubCommandArgs { | |||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | 28 | pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { |
| 29 | // Early validation: check if we have required parameters in non-interactive | ||
| 30 | // mode | ||
| 31 | let signer_info = extract_signer_cli_arguments(args)?; | ||
| 32 | if Interactor::is_non_interactive() && signer_info.is_none() { | ||
| 33 | use ngit::cli_interactor::cli_error; | ||
| 34 | return Err(cli_error( | ||
| 35 | "requires --nsec or --interactive", | ||
| 36 | &[ | ||
| 37 | ("--nsec <key>", "provide secret key (nsec or hex)"), | ||
| 38 | ("--interactive", "for nostr connect or bunker login"), | ||
| 39 | ], | ||
| 40 | &[ | ||
| 41 | "ngit account login --nsec <your-nsec>", | ||
| 42 | "ngit account create", | ||
| 43 | ], | ||
| 44 | )); | ||
| 45 | } | ||
| 46 | |||
| 29 | let git_repo_result = Repo::discover().context("failed to find a git repository"); | 47 | let git_repo_result = Repo::discover().context("failed to find a git repository"); |
| 30 | let git_repo = { git_repo_result.ok() }; | 48 | let git_repo = { git_repo_result.ok() }; |
| 31 | 49 | ||
| @@ -42,7 +60,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | |||
| 42 | fresh_login_or_signup( | 60 | fresh_login_or_signup( |
| 43 | &git_repo.as_ref(), | 61 | &git_repo.as_ref(), |
| 44 | client.as_ref(), | 62 | client.as_ref(), |
| 45 | extract_signer_cli_arguments(args)?, | 63 | signer_info, |
| 46 | log_in_locally_only || command_args.local, | 64 | log_in_locally_only || command_args.local, |
| 47 | ) | 65 | ) |
| 48 | .await?; | 66 | .await?; |
| @@ -56,6 +74,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | |||
| 56 | } | 74 | } |
| 57 | 75 | ||
| 58 | /// return ( bool - logged out, bool - log in to local git locally) | 76 | /// return ( bool - logged out, bool - log in to local git locally) |
| 77 | #[allow(clippy::too_many_lines)] | ||
| 59 | async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool)> { | 78 | async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool)> { |
| 60 | for source in if local_only || std::env::var("NGITTEST").is_ok() { | 79 | for source in if local_only || std::env::var("NGITTEST").is_ok() { |
| 61 | vec![SignerInfoSource::GitLocal] | 80 | vec![SignerInfoSource::GitLocal] |
| @@ -74,6 +93,41 @@ async fn logout(git_repo: Option<&Repo>, local_only: bool) -> Result<(bool, bool | |||
| 74 | ) | 93 | ) |
| 75 | .await | 94 | .await |
| 76 | { | 95 | { |
| 96 | // In non-interactive mode, automatically logout without prompting | ||
| 97 | if Interactor::is_non_interactive() { | ||
| 98 | for item in [ | ||
| 99 | "nostr.nsec", | ||
| 100 | "nostr.npub", | ||
| 101 | "nostr.bunker-uri", | ||
| 102 | "nostr.bunker-app-key", | ||
| 103 | ] { | ||
| 104 | if let Err(_error) = remove_git_config_item( | ||
| 105 | if source == SignerInfoSource::GitLocal { | ||
| 106 | &git_repo | ||
| 107 | } else { | ||
| 108 | &None | ||
| 109 | }, | ||
| 110 | item, | ||
| 111 | ) { | ||
| 112 | use ngit::cli_interactor::cli_error; | ||
| 113 | return Err(cli_error( | ||
| 114 | &format!( | ||
| 115 | "failed to edit {} git config item '{item}'", | ||
| 116 | if source == SignerInfoSource::GitGlobal { | ||
| 117 | "global" | ||
| 118 | } else { | ||
| 119 | "local" | ||
| 120 | }, | ||
| 121 | ), | ||
| 122 | &[], | ||
| 123 | &["ngit account login --local --nsec <your-nsec>"], | ||
| 124 | )); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | return Ok((true, local_only)); | ||
| 128 | } | ||
| 129 | |||
| 130 | // Interactive mode: prompt user for what to do | ||
| 77 | match Interactor::default().choice( | 131 | match Interactor::default().choice( |
| 78 | PromptChoiceParms::default() | 132 | PromptChoiceParms::default() |
| 79 | .with_default(0) | 133 | .with_default(0) |