upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.rs65
-rw-r--r--src/main.rs6
-rw-r--r--src/sub_commands/prs/create.rs20
-rw-r--r--src/sub_commands/prs/mod.rs4
4 files changed, 90 insertions, 5 deletions
diff --git a/src/client.rs b/src/client.rs
new file mode 100644
index 0000000..a6f7dda
--- /dev/null
+++ b/src/client.rs
@@ -0,0 +1,65 @@
1// have you considered
2
3// TO USE ASYNC
4
5// in traits (required for mocking unit tests)
6// https://rust-lang.github.io/async-book/07_workarounds/05_async_in_traits.html
7// https://github.com/dtolnay/async-trait
8// see https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html
9// I think we can use the async-trait crate and switch to the native feature
10// which is currently in nightly. alternatively we can use nightly as it looks
11// certain that the implementation is going to make it to stable but we don't
12// want to inadvertlty use other features of nightly that might be removed.
13use anyhow::Result;
14use async_trait::async_trait;
15#[cfg(test)]
16use mockall::*;
17use nostr::Event;
18
19pub struct Client {
20 client: nostr_sdk::Client,
21}
22
23#[async_trait]
24#[cfg_attr(test, automock)]
25pub trait Connect {
26 fn default() -> Self;
27 fn new(opts: Params) -> Self;
28 async fn connect(&self) -> Result<()>;
29 async fn send_event_to(&self, url: &str, event: nostr::event::Event) -> Result<nostr::EventId>;
30}
31
32#[async_trait]
33impl Connect for Client {
34 fn default() -> Self {
35 Client {
36 client: nostr_sdk::Client::new(&nostr::Keys::generate()),
37 }
38 }
39 fn new(opts: Params) -> Self {
40 Client {
41 client: nostr_sdk::Client::new(&opts.keys.unwrap_or(nostr::Keys::generate())),
42 }
43 }
44 async fn connect(&self) -> Result<()> {
45 self.client.add_relay("ws://localhost:8080", None).await?;
46 self.client.connect().await;
47 // self.client.s
48 Ok(())
49 }
50 async fn send_event_to(&self, url: &str, event: Event) -> Result<nostr::EventId> {
51 Ok(self.client.send_event_to(url, event).await?)
52 }
53}
54
55#[derive(Default)]
56pub struct Params {
57 pub keys: Option<nostr::Keys>,
58}
59
60impl Params {
61 pub fn with_keys(mut self, keys: nostr::Keys) -> Self {
62 self.keys = Some(keys);
63 self
64 }
65}
diff --git a/src/main.rs b/src/main.rs
index 5094c11..9c37aa7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,6 +5,7 @@ use anyhow::Result;
5use clap::{Parser, Subcommand}; 5use clap::{Parser, Subcommand};
6 6
7mod cli_interactor; 7mod cli_interactor;
8mod client;
8mod config; 9mod config;
9mod git; 10mod git;
10mod key_handling; 11mod key_handling;
@@ -33,10 +34,11 @@ enum Commands {
33 Prs(sub_commands::prs::SubCommandArgs), 34 Prs(sub_commands::prs::SubCommandArgs),
34} 35}
35 36
36fn main() -> Result<()> { 37#[tokio::main]
38async fn main() -> Result<()> {
37 let cli = Cli::parse(); 39 let cli = Cli::parse();
38 match &cli.command { 40 match &cli.command {
39 Commands::Login(args) => sub_commands::login::launch(&cli, args), 41 Commands::Login(args) => sub_commands::login::launch(&cli, args),
40 Commands::Prs(args) => sub_commands::prs::launch(&cli, args), 42 Commands::Prs(args) => futures::executor::block_on(sub_commands::prs::launch(&cli, args)),
41 } 43 }
42} 44}
diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs
index dd32c65..89ea652 100644
--- a/src/sub_commands/prs/create.rs
+++ b/src/sub_commands/prs/create.rs
@@ -3,6 +3,7 @@ use nostr::{prelude::sha1::Hash as Sha1Hash, EventBuilder, Marker, Tag, TagKind}
3 3
4use crate::{ 4use crate::{
5 cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms}, 5 cli_interactor::{Interactor, InteractorPrompt, PromptConfirmParms, PromptInputParms},
6 client::{Client, Connect, Params as ClientParams},
6 git::{Repo, RepoActions}, 7 git::{Repo, RepoActions},
7 login, Cli, 8 login, Cli,
8}; 9};
@@ -23,7 +24,7 @@ pub struct SubCommandArgs {
23 to_branch: Option<String>, 24 to_branch: Option<String>,
24} 25}
25 26
26pub fn launch( 27pub async fn launch(
27 cli_args: &Cli, 28 cli_args: &Cli,
28 _pr_args: &super::SubCommandArgs, 29 _pr_args: &super::SubCommandArgs,
29 args: &SubCommandArgs, 30 args: &SubCommandArgs,
@@ -81,6 +82,7 @@ pub fn launch(
81 let root_commit = git_repo 82 let root_commit = git_repo
82 .get_root_commit(to_branch.as_str()) 83 .get_root_commit(to_branch.as_str())
83 .context("failed to get root commit of the repository")?; 84 .context("failed to get root commit of the repository")?;
85
84 // create PR event 86 // create PR event
85 87
86 let keys = login::launch(&cli_args.nsec, &cli_args.password)?; 88 let keys = login::launch(&cli_args.nsec, &cli_args.password)?;
@@ -138,7 +140,23 @@ pub fn launch(
138 ); 140 );
139 } 141 }
140 142
143 let client = Client::new(ClientParams::default().with_keys(keys));
144
145 println!("connecting...");
146 client.connect().await?;
147 println!("connected...");
148
141 // TODO check if there is already a similarly named PR 149 // TODO check if there is already a similarly named PR
150 let _ = client
151 .send_event_to("ws://localhost:8080", pr_event)
152 .await?;
153 // TODO post each PR
154 // TODO report
155 println!("posted successfully to 4/5 of your relays and 0/4 of maintainers relays");
156 // should we have a relays in Repository event?
157 // yes
158 //
159
142 // TODO connect to relays and post 160 // TODO connect to relays and post
143 161
144 Ok(()) 162 Ok(())
diff --git a/src/sub_commands/prs/mod.rs b/src/sub_commands/prs/mod.rs
index c316e73..982e866 100644
--- a/src/sub_commands/prs/mod.rs
+++ b/src/sub_commands/prs/mod.rs
@@ -15,8 +15,8 @@ pub enum Commands {
15 Create(create::SubCommandArgs), 15 Create(create::SubCommandArgs),
16} 16}
17 17
18pub fn launch(cli_args: &Cli, pr_args: &SubCommandArgs) -> Result<()> { 18pub async fn launch(cli_args: &Cli, pr_args: &SubCommandArgs) -> Result<()> {
19 match &pr_args.prs_command { 19 match &pr_args.prs_command {
20 Commands::Create(args) => create::launch(cli_args, pr_args, args), 20 Commands::Create(args) => create::launch(cli_args, pr_args, args).await,
21 } 21 }
22} 22}