diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/sub_commands/claim.rs | 114 | ||||
| -rw-r--r-- | src/sub_commands/mod.rs | 1 | ||||
| -rw-r--r-- | src/sub_commands/prs/create.rs | 12 |
4 files changed, 127 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 68b0ed6..54ad748 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -33,7 +33,9 @@ pub struct Cli { | |||
| 33 | enum Commands { | 33 | enum Commands { |
| 34 | /// save encrypted nsec for future use | 34 | /// save encrypted nsec for future use |
| 35 | Login(sub_commands::login::SubCommandArgs), | 35 | Login(sub_commands::login::SubCommandArgs), |
| 36 | /// create and issue Prs | 36 | /// issue repository reference event as a maintainers |
| 37 | Claim(sub_commands::claim::SubCommandArgs), | ||
| 38 | /// create and issue prs | ||
| 37 | Prs(sub_commands::prs::SubCommandArgs), | 39 | Prs(sub_commands::prs::SubCommandArgs), |
| 38 | } | 40 | } |
| 39 | 41 | ||
| @@ -44,6 +46,9 @@ async fn main() -> Result<()> { | |||
| 44 | Commands::Login(args) => { | 46 | Commands::Login(args) => { |
| 45 | futures::executor::block_on(sub_commands::login::launch(&cli, args)) | 47 | futures::executor::block_on(sub_commands::login::launch(&cli, args)) |
| 46 | } | 48 | } |
| 49 | Commands::Claim(args) => { | ||
| 50 | futures::executor::block_on(sub_commands::claim::launch(&cli, args)) | ||
| 51 | } | ||
| 47 | Commands::Prs(args) => futures::executor::block_on(sub_commands::prs::launch(&cli, args)), | 52 | Commands::Prs(args) => futures::executor::block_on(sub_commands::prs::launch(&cli, args)), |
| 48 | } | 53 | } |
| 49 | } | 54 | } |
diff --git a/src/sub_commands/claim.rs b/src/sub_commands/claim.rs new file mode 100644 index 0000000..5eb66bb --- /dev/null +++ b/src/sub_commands/claim.rs | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use nostr::{EventBuilder, Tag}; | ||
| 3 | |||
| 4 | use super::prs::create::send_events; | ||
| 5 | #[cfg(not(test))] | ||
| 6 | use crate::client::Client; | ||
| 7 | #[cfg(test)] | ||
| 8 | use crate::client::MockConnect; | ||
| 9 | use crate::{ | ||
| 10 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, | ||
| 11 | client::Connect, | ||
| 12 | git::{Repo, RepoActions}, | ||
| 13 | login, Cli, | ||
| 14 | }; | ||
| 15 | |||
| 16 | #[derive(Debug, clap::Args)] | ||
| 17 | pub struct SubCommandArgs { | ||
| 18 | #[clap(short, long)] | ||
| 19 | /// name of repository | ||
| 20 | title: Option<String>, | ||
| 21 | #[clap(short, long)] | ||
| 22 | /// optional description | ||
| 23 | description: Option<String>, | ||
| 24 | } | ||
| 25 | |||
| 26 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | ||
| 27 | let git_repo = Repo::discover().context("cannot find a git repository")?; | ||
| 28 | |||
| 29 | let (main_or_master_branch_name, _) = git_repo | ||
| 30 | .get_main_or_master_branch() | ||
| 31 | .context("no main or master branch")?; | ||
| 32 | |||
| 33 | let root_commit = git_repo | ||
| 34 | .get_root_commit(main_or_master_branch_name) | ||
| 35 | .context("failed to get root commit of the repository")?; | ||
| 36 | |||
| 37 | // TODO: check for empty repo | ||
| 38 | // TODO: check for existing maintaiers file | ||
| 39 | // TODO: check for other claims | ||
| 40 | |||
| 41 | let name = match &args.title { | ||
| 42 | Some(t) => t.clone(), | ||
| 43 | None => Interactor::default().input(PromptInputParms::default().with_prompt("name"))?, | ||
| 44 | }; | ||
| 45 | |||
| 46 | let description = match &args.description { | ||
| 47 | Some(t) => t.clone(), | ||
| 48 | None => Interactor::default() | ||
| 49 | .input(PromptInputParms::default().with_prompt("description (Optional)"))?, | ||
| 50 | }; | ||
| 51 | |||
| 52 | #[cfg(not(test))] | ||
| 53 | let mut client = Client::default(); | ||
| 54 | #[cfg(test)] | ||
| 55 | let mut client = <MockConnect as std::default::Default>::default(); | ||
| 56 | |||
| 57 | let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; | ||
| 58 | |||
| 59 | client.set_keys(&keys).await; | ||
| 60 | |||
| 61 | // TODO: choice input defaulting to user relay list filtered by non paid relays | ||
| 62 | let repo_relays: Vec<String> = vec![ | ||
| 63 | "ws://localhost:8055".to_string(), | ||
| 64 | "ws://localhost:8056".to_string(), | ||
| 65 | ]; | ||
| 66 | |||
| 67 | println!("publishing repostory reference..."); | ||
| 68 | |||
| 69 | send_events( | ||
| 70 | &client, | ||
| 71 | vec![generate_repo_event( | ||
| 72 | &name, | ||
| 73 | &description, | ||
| 74 | &repo_relays, | ||
| 75 | &root_commit.to_string(), | ||
| 76 | &keys, | ||
| 77 | )?], | ||
| 78 | user_ref.relays.write(), | ||
| 79 | repo_relays, | ||
| 80 | !cli_args.disable_cli_spinners, | ||
| 81 | ) | ||
| 82 | .await?; | ||
| 83 | |||
| 84 | Ok(()) | ||
| 85 | } | ||
| 86 | |||
| 87 | fn generate_repo_event( | ||
| 88 | name: &str, | ||
| 89 | description: &str, | ||
| 90 | relays: &[String], | ||
| 91 | // git_server: String, | ||
| 92 | root_commit: &String, | ||
| 93 | keys: &nostr::Keys, | ||
| 94 | ) -> Result<nostr::Event> { | ||
| 95 | EventBuilder::new( | ||
| 96 | nostr::event::Kind::Custom(30017), | ||
| 97 | "", | ||
| 98 | &[ | ||
| 99 | vec![ | ||
| 100 | Tag::Identifier(root_commit.to_string()), | ||
| 101 | Tag::Reference(format!("r-{root_commit}")), | ||
| 102 | Tag::Name(name.to_owned()), | ||
| 103 | Tag::Description(description.to_owned()), | ||
| 104 | ], | ||
| 105 | relays.iter().map(|r| Tag::Relay(r.into())).collect(), | ||
| 106 | // git_servers | ||
| 107 | // other maintainers | ||
| 108 | // code languages and hashtags | ||
| 109 | ] | ||
| 110 | .concat(), | ||
| 111 | ) | ||
| 112 | .to_event(keys) | ||
| 113 | .context("failed to create pr event") | ||
| 114 | } | ||
diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs index 3c3da1d..6e99ca5 100644 --- a/src/sub_commands/mod.rs +++ b/src/sub_commands/mod.rs | |||
| @@ -1,2 +1,3 @@ | |||
| 1 | pub mod claim; | ||
| 1 | pub mod login; | 2 | pub mod login; |
| 2 | pub mod prs; | 3 | pub mod prs; |
diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs index aad80f4..d82f53e 100644 --- a/src/sub_commands/prs/create.rs +++ b/src/sub_commands/prs/create.rs | |||
| @@ -105,6 +105,11 @@ pub async fn launch( | |||
| 105 | "ws://localhost:8056".to_string(), | 105 | "ws://localhost:8056".to_string(), |
| 106 | ]; | 106 | ]; |
| 107 | 107 | ||
| 108 | println!( | ||
| 109 | "posting 1 pull request with {} commits...", | ||
| 110 | events.len() - 1 | ||
| 111 | ); | ||
| 112 | |||
| 108 | send_events( | 113 | send_events( |
| 109 | &client, | 114 | &client, |
| 110 | events, | 115 | events, |
| @@ -118,7 +123,7 @@ pub async fn launch( | |||
| 118 | Ok(()) | 123 | Ok(()) |
| 119 | } | 124 | } |
| 120 | 125 | ||
| 121 | async fn send_events( | 126 | pub async fn send_events( |
| 122 | #[cfg(test)] client: &crate::client::MockConnect, | 127 | #[cfg(test)] client: &crate::client::MockConnect, |
| 123 | #[cfg(not(test))] client: &Client, | 128 | #[cfg(not(test))] client: &Client, |
| 124 | events: Vec<nostr::Event>, | 129 | events: Vec<nostr::Event>, |
| @@ -128,11 +133,6 @@ async fn send_events( | |||
| 128 | ) -> Result<()> { | 133 | ) -> Result<()> { |
| 129 | let (_, _, _, all) = unique_and_duplicate_all(&my_write_relays, &repo_read_relays); | 134 | let (_, _, _, all) = unique_and_duplicate_all(&my_write_relays, &repo_read_relays); |
| 130 | 135 | ||
| 131 | println!( | ||
| 132 | "posting 1 pull request with {} commits...", | ||
| 133 | events.len() - 1 | ||
| 134 | ); | ||
| 135 | |||
| 136 | let m = MultiProgress::new(); | 136 | let m = MultiProgress::new(); |
| 137 | let pb_style = ProgressStyle::with_template(if animate { | 137 | let pb_style = ProgressStyle::with_template(if animate { |
| 138 | " {spinner} {prefix} {bar} {pos}/{len} {msg}" | 138 | " {spinner} {prefix} {bar} {pos}/{len} {msg}" |