Skip to content

Commit

Permalink
feat(account): move login/out cmds to account
Browse files Browse the repository at this point in the history
move login, logout export-keys commands to sub commands under account
  • Loading branch information
DanConwayDev committed Nov 27, 2024
1 parent c002cef commit cb92d2a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
16 changes: 14 additions & 2 deletions src/bin/ngit/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub enum Commands {
Fetch(sub_commands::fetch::SubCommandArgs),
/// signal you are this repo's maintainer accepting proposals via nostr
Init(sub_commands::init::SubCommandArgs),
/// export nostr keys to login to other nostr clients
ExportKeys,
/// issue commits as a proposal
Send(sub_commands::send::SubCommandArgs),
/// list proposals; checkout, apply or download selected
Expand All @@ -67,8 +65,22 @@ pub enum Commands {
Push(sub_commands::push::SubCommandArgs),
/// fetch and apply new proposal commits / revisions linked to branch
Pull,
/// login, logout or export keys
Account(AccountSubCommandArgs),
}

#[derive(Subcommand)]
pub enum AccountCommands {
/// run with --nsec flag to change npub
Login(sub_commands::login::SubCommandArgs),
/// remove nostr account details stored in git config
Logout,
/// export nostr keys to login to other nostr clients
ExportKeys,
}

#[derive(clap::Parser)]
pub struct AccountSubCommandArgs {
#[command(subcommand)]
pub account_command: AccountCommands,
}
12 changes: 7 additions & 5 deletions src/bin/ngit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use anyhow::Result;
use clap::Parser;
use cli::{Cli, Commands};
use cli::{AccountCommands, Cli, Commands};

mod cli;
use ngit::{cli_interactor, client, git, git_events, login, repo_ref};
Expand All @@ -15,14 +15,16 @@ mod sub_commands;
async fn main() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Commands::Account(args) => match &args.account_command {
AccountCommands::Login(sub_args) => sub_commands::login::launch(&cli, sub_args).await,
AccountCommands::Logout => sub_commands::logout::launch().await,
AccountCommands::ExportKeys => sub_commands::export_keys::launch().await,
},
Commands::Fetch(args) => sub_commands::fetch::launch(&cli, args).await,
Commands::Login(args) => sub_commands::login::launch(&cli, args).await,
Commands::Logout => sub_commands::logout::launch().await,
Commands::Init(args) => sub_commands::init::launch(&cli, args).await,
Commands::ExportKeys => sub_commands::export_keys::launch().await,
Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await,
Commands::List => sub_commands::list::launch().await,
Commands::Pull => sub_commands::pull::launch().await,
Commands::Push(args) => sub_commands::push::launch(&cli, args).await,
Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await,
}
}
25 changes: 13 additions & 12 deletions tests/ngit_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn first_time_login_choices_succeeds_with_nsec(p: &mut CliTester, nsec: &str) ->

fn standard_first_time_login_with_nsec() -> Result<CliTester> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login", "--offline"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login", "--offline"]);

first_time_login_choices_succeeds_with_nsec(&mut p, TEST_KEY_1_NSEC)?;

Expand Down Expand Up @@ -77,7 +77,7 @@ mod with_relays {

let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login"]);

first_time_login_choices_succeeds_with_nsec(&mut p, TEST_KEY_1_NSEC)?;

Expand Down Expand Up @@ -108,7 +108,7 @@ mod with_relays {

let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login"]);

first_time_login_choices_succeeds_with_nsec(&mut p, TEST_KEY_1_NSEC)?;

Expand Down Expand Up @@ -416,7 +416,7 @@ mod with_relays {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(
&test_repo.dir,
["login", "--nsec", TEST_KEY_1_NSEC],
["account", "login", "--nsec", TEST_KEY_1_NSEC],
);

p.expect("saved login details to local git config. you are only logged in to this local repository.\r\n")?;
Expand Down Expand Up @@ -458,7 +458,7 @@ mod with_relays {

let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login"]);

first_time_login_choices_succeeds_with_nsec(&mut p, TEST_KEY_1_NSEC)?;

Expand Down Expand Up @@ -512,7 +512,7 @@ mod with_relays {

let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login"]);

first_time_login_choices_succeeds_with_nsec(&mut p, TEST_KEY_1_NSEC)?;

Expand Down Expand Up @@ -553,7 +553,7 @@ mod with_relays {

let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login"]);

first_time_login_choices_succeeds_with_nsec(&mut p, TEST_KEY_1_NSEC)?;

Expand Down Expand Up @@ -628,7 +628,7 @@ mod with_offline_flag {
#[test]
fn succeeds_with_text_logged_in_as_npub() -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login", "--offline"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login", "--offline"]);

show_first_time_login_choices(&mut p)?.succeeds_with(0, false, Some(0))?;

Expand All @@ -643,7 +643,7 @@ mod with_offline_flag {
#[test]
fn succeeds_with_hex_secret_key_in_place_of_nsec() -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login", "--offline"]);
let mut p = CliTester::new_from_dir(&test_repo.dir, ["account", "login", "--offline"]);

show_first_time_login_choices(&mut p)?.succeeds_with(0, false, Some(0))?;

Expand All @@ -661,7 +661,8 @@ mod with_offline_flag {
#[test]
fn prompts_for_nsec_until_valid() -> Result<()> {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(&test_repo.dir, ["login", "--offline"]);
let mut p =
CliTester::new_from_dir(&test_repo.dir, ["account", "login", "--offline"]);

show_first_time_login_choices(&mut p)?.succeeds_with(0, false, Some(0))?;

Expand Down Expand Up @@ -698,7 +699,7 @@ mod with_offline_flag {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(
&test_repo.dir,
["login", "--offline", "--nsec", TEST_KEY_1_NSEC],
["account", "login", "--offline", "--nsec", TEST_KEY_1_NSEC],
);

p.expect("saved login details to local git config. you are only logged in to this local repository.\r\n")?;
Expand All @@ -713,7 +714,7 @@ mod with_offline_flag {
let test_repo = GitTestRepo::default();
let mut p = CliTester::new_from_dir(
&test_repo.dir,
["login", "--offline", "--nsec", TEST_INVALID_NSEC],
["account", "login", "--offline", "--nsec", TEST_INVALID_NSEC],
);

p.expect_end_with(
Expand Down

0 comments on commit cb92d2a

Please sign in to comment.