From 318e467b158b158cf9eb843318dc14141d1b8c54 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 21 May 2023 11:27:04 +0000 Subject: main and remaining subcommands --- src/sub_commands/change_user.rs | 63 ++++++++++++++++++++++++++++++++++++++ src/sub_commands/fetch.rs | 22 +++++++++++++ src/sub_commands/mod.rs | 9 ++++++ src/sub_commands/pull.rs | 26 ++++++++++++++++ src/sub_commands/push.rs | 31 +++++++++++++++++++ src/sub_commands/rebroadcast.rs | 68 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 219 insertions(+) create mode 100644 src/sub_commands/change_user.rs create mode 100644 src/sub_commands/fetch.rs create mode 100644 src/sub_commands/mod.rs create mode 100644 src/sub_commands/pull.rs create mode 100644 src/sub_commands/push.rs create mode 100644 src/sub_commands/rebroadcast.rs (limited to 'src/sub_commands') diff --git a/src/sub_commands/change_user.rs b/src/sub_commands/change_user.rs new file mode 100644 index 0000000..ac15d1d --- /dev/null +++ b/src/sub_commands/change_user.rs @@ -0,0 +1,63 @@ + + +use clap::Args; +use dialoguer::{Select, theme::ColorfulTheme, Confirm, Password}; +use nostr::{Keys, prelude::{FromSkStr}}; + +use crate::{config::{load_config, save_conifg}}; + +#[derive(Args)] +pub struct ChangeUserSubCommand { +} + +pub fn change_user(_sub_command_args: &ChangeUserSubCommand) { + + let mut cfg = load_config(); + + if cfg.private_key.is_some() { + if !Confirm::with_theme(&ColorfulTheme::default()) + .with_prompt("overwrite existing?") + .default(false) + .interact() + .unwrap() { + return; + } + } + + let selection = Select::with_theme(&ColorfulTheme::default()) + .items(&vec!["enter existing private key", "generate new keys"]) + .default(0) + .with_prompt("no keys are stored") + .interact().unwrap(); + + let key = match selection { + 0 => { + let mut prompt = "secret key (nsec, hex, etc)"; + loop { + let pk: String = Password::with_theme(&ColorfulTheme::default()) + .with_prompt(prompt) + .interact() + .unwrap(); + match Keys::from_sk_str(&pk) { + Ok(key) => { break key; }, + Err(_e) => { prompt = "error interpeting secret key. try again with nsec, hex, etc"; }, + } + } + } + _ => Keys::generate(), + }; + cfg.private_key = Some(key.secret_key().unwrap()); + + if cfg.default_admin_group_event_serialized.is_some() { + if !Confirm::with_theme(&ColorfulTheme::default()) + .with_prompt("remove default admin group? If not permissions on new repositories can only be changed by the previous user.") + .default(true) + .interact() + .unwrap() { + cfg.default_admin_group_event_serialized = None; + } + } + + save_conifg(&cfg); + println!("private key updated") +} diff --git a/src/sub_commands/fetch.rs b/src/sub_commands/fetch.rs new file mode 100644 index 0000000..36f1954 --- /dev/null +++ b/src/sub_commands/fetch.rs @@ -0,0 +1,22 @@ +use clap::Args; +use nostr::Keys; + +use crate::fetch_pull_push::fetch_pull_push; + +#[derive(Args)] +pub struct FetchSubCommand { +} + +pub fn fetch_from_relays(keys: Option<&Keys>) { + + fetch_pull_push( + keys, + false, + false, + None, + false, + None, + None, + ); + +} diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs new file mode 100644 index 0000000..280f959 --- /dev/null +++ b/src/sub_commands/mod.rs @@ -0,0 +1,9 @@ +pub mod clone; +pub mod init; +pub mod pull; +pub mod push; +pub mod prs; +pub mod fetch; +pub mod merge; +pub mod rebroadcast; +pub mod change_user; diff --git a/src/sub_commands/pull.rs b/src/sub_commands/pull.rs new file mode 100644 index 0000000..0030dbe --- /dev/null +++ b/src/sub_commands/pull.rs @@ -0,0 +1,26 @@ + + +use clap::Args; +use nostr::{Keys}; + +use crate::{fetch_pull_push::fetch_pull_push}; + +#[derive(Args)] +pub struct PullSubCommand { + /// branch nevent or hex to pull into a new local branch + #[arg(short, long)] + pub branch: Option, +} + +pub fn pull_from_relays(keys: Option<&Keys>, sub_command_args: &PullSubCommand) { + + fetch_pull_push( + keys, + true, + false, + sub_command_args.branch.clone(), + false, + None, + None, + ); +} diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs new file mode 100644 index 0000000..e0fda7b --- /dev/null +++ b/src/sub_commands/push.rs @@ -0,0 +1,31 @@ + +use clap::{Args}; + +use crate::fetch_pull_push::fetch_pull_push; + +#[derive(Args)] +struct PushRepo { + /// Relays + #[arg(short, long)] + relays: Option, +} + +#[derive(Args)] +pub struct PushSubCommand { +} + +pub fn push( + _sub_command_args: &PushSubCommand, +) { + + fetch_pull_push( + None, + false, + true, + None, + false, + None, + None, + ); + +} diff --git a/src/sub_commands/rebroadcast.rs b/src/sub_commands/rebroadcast.rs new file mode 100644 index 0000000..d3ece23 --- /dev/null +++ b/src/sub_commands/rebroadcast.rs @@ -0,0 +1,68 @@ + + +use std::fs; + +use clap::Args; + +use nostr::{Keys}; + +use crate::{cli_helpers::select_relays, config::load_config, utils::{create_client, load_event}}; + +#[derive(Args)] +pub struct RebroadcastSubCommand { +} + +pub fn rebroadcast( + _sub_command_args: &RebroadcastSubCommand, +) { + + // get relay input + let relays = select_relays( + &mut load_config(), + &vec![], + ) + .expect("relays to be selected"); + + let client = create_client(&Keys::generate(), relays) + .expect("create_client to return client for create_and_broadcast_patches"); + + let repo_dir_path = std::env::current_dir().unwrap(); + + // cycle through directories and send events + for dir_name in [ + "groups", + "branches", + "patches", + "merges", + "prs", + "issues", + "comments", + ] { + if !repo_dir_path.join(".ngit").exists() { + println!("this isn't a repository here to rebroadcast") + } + let dir_path = repo_dir_path.join(".ngit").join(&dir_name); + if dir_path.exists() { + let dir = fs::read_dir(&dir_path) + .expect("read_dir to produce ReadDir from a path that exists"); + // get json in directories + for entry in dir { + let path = entry + .expect("DirEntry to return from ReadDir") + .path(); + // send event + match client.send_event( + load_event(&path) + .expect("every file in .ngit paths is a valid json event") + ) { + Ok(_) => { + println!("sent: {}", &path.to_string_lossy()); + }, + // TODO: this isn't working - if a relay is specified with a type it will wait 30ish secs and then return successful + Err(e) => { println!("error broadcasting event: {}",e); }, + } + // TODO: better error handling here / reporting. potentially warn if taking a while and report on troublesome relays + } + } + } +} -- cgit v1.2.3