From 62d7f2fb26f2843fa8f7ff8b988bac14e5f756e2 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 14 Feb 2024 08:54:27 +0000 Subject: feat!: move `claim` > `init` this aligns with gitstr and is more intuative the idea behind using claim to indicate that it is only for maintainersto do is valid but its too confusing --- src/main.rs | 4 +- src/sub_commands/claim.rs | 152 ---------------------------------------------- src/sub_commands/init.rs | 152 ++++++++++++++++++++++++++++++++++++++++++++++ src/sub_commands/mod.rs | 2 +- 4 files changed, 155 insertions(+), 155 deletions(-) delete mode 100644 src/sub_commands/claim.rs create mode 100644 src/sub_commands/init.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 539d9ff..0e8e847 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ enum Commands { /// save encrypted nsec for future use Login(sub_commands::login::SubCommandArgs), /// issue repository reference event as a maintainers - Claim(sub_commands::claim::SubCommandArgs), + Init(sub_commands::init::SubCommandArgs), /// send a PR / patch / patch set via nostr events Send(sub_commands::send::SubCommandArgs), /// list open PRs / patches / patch sets and pull / apply them a branch @@ -51,7 +51,7 @@ async fn main() -> Result<()> { let cli = Cli::parse(); match &cli.command { Commands::Login(args) => sub_commands::login::launch(&cli, args).await, - Commands::Claim(args) => sub_commands::claim::launch(&cli, args).await, + Commands::Init(args) => sub_commands::init::launch(&cli, args).await, Commands::Send(args) => sub_commands::send::launch(&cli, args).await, Commands::List(args) => sub_commands::list::launch(&cli, args).await, Commands::Pull => sub_commands::pull::launch().await, diff --git a/src/sub_commands/claim.rs b/src/sub_commands/claim.rs deleted file mode 100644 index a95021d..0000000 --- a/src/sub_commands/claim.rs +++ /dev/null @@ -1,152 +0,0 @@ -use anyhow::{Context, Result}; - -use super::send::send_events; -#[cfg(not(test))] -use crate::client::Client; -#[cfg(test)] -use crate::client::MockConnect; -use crate::{ - cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, - client::Connect, - git::{Repo, RepoActions}, - login, - repo_ref::{extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml, RepoRef}, - Cli, -}; - -#[derive(Debug, clap::Args)] -pub struct SubCommandArgs { - #[clap(short, long)] - /// name of repository - title: Option, - #[clap(short, long)] - /// optional description - description: Option, - #[clap(short, long, value_parser, num_args = 1..)] - /// homepage - web: Vec, - #[clap(short, long, value_parser, num_args = 1..)] - /// relays contributors push patches and comments to - relays: Vec, -} - -pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { - let git_repo = Repo::discover().context("cannot find a git repository")?; - - let root_commit = git_repo - .get_root_commit() - .context("failed to get root commit of the repository")?; - - // TODO: check for empty repo - // TODO: check for existing maintaiers file - - let repo_config_result = get_repo_config_from_yaml(&git_repo); - // TODO: check for other claims - - let name = match &args.title { - Some(t) => t.clone(), - None => Interactor::default().input(PromptInputParms::default().with_prompt("name"))?, - }; - - let description = match &args.description { - Some(t) => t.clone(), - None => Interactor::default() - .input(PromptInputParms::default().with_prompt("description (Optional)"))?, - }; - - let git_server = git_repo - .get_origin_url() - .context( - "to claim the repository it must be available on a publically accessable git server", - ) - .context("no git remote origin configured")?; - - let web: Vec = if args.web.is_empty() { - Interactor::default() - .input(PromptInputParms::default().with_prompt("description (Optional)"))? - .split(' ') - .map(std::string::ToString::to_string) - .collect() - } else { - args.web.clone() - }; - #[cfg(not(test))] - let mut client = Client::default(); - #[cfg(test)] - let mut client = ::default(); - - let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; - - client.set_keys(&keys).await; - - let mut maintainers = vec![keys.public_key()]; - - let mut repo_relays: Vec = if !args.relays.is_empty() { - args.relays.clone() - } else if let Ok(config) = &repo_config_result { - config.relays.clone() - } else { - // TODO: choice input defaulting to user relay list filtered by non paid relays - // TODO: allow manual input for more relays - // TODO: reccommend some free relays - user_ref.relays.write() - }; - - // TODO: add blaster for just repo event and don't add it to repo relays - // TODO: fix the tests to support blaster - if std::env::var("NGITTEST").is_err() - && !repo_relays - .iter() - .any(|s| s.contains("nostr.mutinywallet.com")) - { - repo_relays.push("wss://nostr.mutinywallet.com".to_string()); - } - - if let Ok(config) = &repo_config_result { - maintainers = extract_pks(config.maintainers.clone())?; - } - - // if yaml file doesnt exist or needs updating - if match &repo_config_result { - Ok(config) => { - !(extract_pks(config.maintainers.clone())?.eq(&maintainers) - && config.relays.eq(&repo_relays)) - } - Err(_) => true, - } { - save_repo_config_to_yaml(&git_repo, maintainers.clone(), repo_relays.clone())?; - println!( - "maintainers.yaml {}. commit and push.", - if repo_config_result.is_err() { - "created" - } else { - "updated" - } - ); - } - - println!("publishing repostory reference..."); - - let repo_event = RepoRef { - identifier: root_commit.to_string()[..7].to_string(), - name, - description, - root_commit: root_commit.to_string(), - git_server, - web, - relays: repo_relays.clone(), - maintainers, - } - .to_event(&keys)?; - - send_events( - &client, - vec![repo_event], - user_ref.relays.write(), - repo_relays, - !cli_args.disable_cli_spinners, - ) - .await?; - - Ok(()) -} diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs new file mode 100644 index 0000000..a95021d --- /dev/null +++ b/src/sub_commands/init.rs @@ -0,0 +1,152 @@ +use anyhow::{Context, Result}; + +use super::send::send_events; +#[cfg(not(test))] +use crate::client::Client; +#[cfg(test)] +use crate::client::MockConnect; +use crate::{ + cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, + client::Connect, + git::{Repo, RepoActions}, + login, + repo_ref::{extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml, RepoRef}, + Cli, +}; + +#[derive(Debug, clap::Args)] +pub struct SubCommandArgs { + #[clap(short, long)] + /// name of repository + title: Option, + #[clap(short, long)] + /// optional description + description: Option, + #[clap(short, long, value_parser, num_args = 1..)] + /// homepage + web: Vec, + #[clap(short, long, value_parser, num_args = 1..)] + /// relays contributors push patches and comments to + relays: Vec, +} + +pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { + let git_repo = Repo::discover().context("cannot find a git repository")?; + + let root_commit = git_repo + .get_root_commit() + .context("failed to get root commit of the repository")?; + + // TODO: check for empty repo + // TODO: check for existing maintaiers file + + let repo_config_result = get_repo_config_from_yaml(&git_repo); + // TODO: check for other claims + + let name = match &args.title { + Some(t) => t.clone(), + None => Interactor::default().input(PromptInputParms::default().with_prompt("name"))?, + }; + + let description = match &args.description { + Some(t) => t.clone(), + None => Interactor::default() + .input(PromptInputParms::default().with_prompt("description (Optional)"))?, + }; + + let git_server = git_repo + .get_origin_url() + .context( + "to claim the repository it must be available on a publically accessable git server", + ) + .context("no git remote origin configured")?; + + let web: Vec = if args.web.is_empty() { + Interactor::default() + .input(PromptInputParms::default().with_prompt("description (Optional)"))? + .split(' ') + .map(std::string::ToString::to_string) + .collect() + } else { + args.web.clone() + }; + #[cfg(not(test))] + let mut client = Client::default(); + #[cfg(test)] + let mut client = ::default(); + + let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; + + client.set_keys(&keys).await; + + let mut maintainers = vec![keys.public_key()]; + + let mut repo_relays: Vec = if !args.relays.is_empty() { + args.relays.clone() + } else if let Ok(config) = &repo_config_result { + config.relays.clone() + } else { + // TODO: choice input defaulting to user relay list filtered by non paid relays + // TODO: allow manual input for more relays + // TODO: reccommend some free relays + user_ref.relays.write() + }; + + // TODO: add blaster for just repo event and don't add it to repo relays + // TODO: fix the tests to support blaster + if std::env::var("NGITTEST").is_err() + && !repo_relays + .iter() + .any(|s| s.contains("nostr.mutinywallet.com")) + { + repo_relays.push("wss://nostr.mutinywallet.com".to_string()); + } + + if let Ok(config) = &repo_config_result { + maintainers = extract_pks(config.maintainers.clone())?; + } + + // if yaml file doesnt exist or needs updating + if match &repo_config_result { + Ok(config) => { + !(extract_pks(config.maintainers.clone())?.eq(&maintainers) + && config.relays.eq(&repo_relays)) + } + Err(_) => true, + } { + save_repo_config_to_yaml(&git_repo, maintainers.clone(), repo_relays.clone())?; + println!( + "maintainers.yaml {}. commit and push.", + if repo_config_result.is_err() { + "created" + } else { + "updated" + } + ); + } + + println!("publishing repostory reference..."); + + let repo_event = RepoRef { + identifier: root_commit.to_string()[..7].to_string(), + name, + description, + root_commit: root_commit.to_string(), + git_server, + web, + relays: repo_relays.clone(), + maintainers, + } + .to_event(&keys)?; + + send_events( + &client, + vec![repo_event], + user_ref.relays.write(), + repo_relays, + !cli_args.disable_cli_spinners, + ) + .await?; + + Ok(()) +} diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs index b16d50f..9f97b7e 100644 --- a/src/sub_commands/mod.rs +++ b/src/sub_commands/mod.rs @@ -1,4 +1,4 @@ -pub mod claim; +pub mod init; pub mod list; pub mod login; pub mod pull; -- cgit v1.2.3