Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
feat: added command to list all saved recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilit0x committed Aug 7, 2023
1 parent 2629177 commit f2193f1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
18 changes: 18 additions & 0 deletions share/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
fs::{self, OpenOptions},
net::Ipv4Addr,
path::{Path, PathBuf},
process::exit,
};

use anyhow::{anyhow, Context, Result};
Expand Down Expand Up @@ -105,10 +106,27 @@ impl Config {
Ok(rpm)
}

Check warning on line 107 in share/src/config.rs

View check run for this annotation

Codecov / codecov/patch

share/src/config.rs#L106-L107

Added lines #L106 - L107 were not covered by tests

fn list_all_saved_peers(store: &Store) -> Result<Vec<ScsPeer>> {
ScsPeer::fetch_all_peers(store)
}

Check warning on line 111 in share/src/config.rs

View check run for this annotation

Codecov / codecov/patch

share/src/config.rs#L109-L111

Added lines #L109 - L111 were not covered by tests

pub fn new(opts: &Cli, store: &Store) -> Result<(Mode, Option<PeerId>, Config)> {
if opts.mode == Mode::List {
let peers = Self::list_all_saved_peers(store)?;
if peers.is_empty() {
println!("No saved peer");
} else {
for peer in peers {
println!("- {}", peer.name());
}

Check warning on line 121 in share/src/config.rs

View check run for this annotation

Codecov / codecov/patch

share/src/config.rs#L113-L121

Added lines #L113 - L121 were not covered by tests
}
exit(1)
}

Check warning on line 124 in share/src/config.rs

View check run for this annotation

Codecov / codecov/patch

share/src/config.rs#L123-L124

Added lines #L123 - L124 were not covered by tests

let rpm = match &opts.mode {
Mode::Send => Some(Self::remote_peer_id_polyfill(opts, store)?),
Mode::Receive => None,
Mode::List => exit(1),

Check warning on line 129 in share/src/config.rs

View check run for this annotation

Codecov / codecov/patch

share/src/config.rs#L126-L129

Added lines #L126 - L129 were not covered by tests
};

match &opts.config {
Expand Down
1 change: 1 addition & 0 deletions share/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Store {
let peer = self.is_peer_present(peer_id)?;

Check warning on line 63 in share/src/database/mod.rs

View check run for this annotation

Codecov / codecov/patch

share/src/database/mod.rs#L61-L63

Added lines #L61 - L63 were not covered by tests

let res = match peer {

Check warning on line 65 in share/src/database/mod.rs

View check run for this annotation

Codecov / codecov/patch

share/src/database/mod.rs#L65

Added line #L65 was not covered by tests
//TODO update last seen of peer
Some(_) => Ok(()),

Check warning on line 67 in share/src/database/mod.rs

View check run for this annotation

Codecov / codecov/patch

share/src/database/mod.rs#L67

Added line #L67 was not covered by tests
None => {
if Confirm::with_theme(&ColorfulTheme::default())
Expand Down
6 changes: 5 additions & 1 deletion share/src/database/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ impl ScsPeer {
}
}

Check warning on line 61 in share/src/database/peer.rs

View check run for this annotation

Codecov / codecov/patch

share/src/database/peer.rs#L61

Added line #L61 was not covered by tests

pub fn name(&self) -> String {
self.name.clone()
}

Check warning on line 65 in share/src/database/peer.rs

View check run for this annotation

Codecov / codecov/patch

share/src/database/peer.rs#L63-L65

Added lines #L63 - L65 were not covered by tests

pub fn fetch_all_peers(store: &Store) -> Result<Vec<ScsPeer>> {
let conn = store.get_conn_handle();
let mut stmt = conn.prepare("SELECT id, name, addrs, last_seen FROM peer")?;
let mut stmt = conn.prepare("SELECT id, name, addrs, peer_id, last_seen FROM peer")?;
let peer_iter = stmt.query_map([], |row| Ok(ScsPeer::try_from(row).unwrap()))?;
let peers = peer_iter.filter_map(|peer| peer.ok()).collect::<Vec<_>>();
Ok(peers)
Expand Down
4 changes: 3 additions & 1 deletion share/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct Cli {
pub enum Mode {
Receive,
Send,
List,
}

impl FromStr for Mode {
Expand All @@ -70,7 +71,8 @@ impl FromStr for Mode {
match mode {
"send" => Ok(Mode::Send),
"receive" => Ok(Mode::Receive),
_ => Err("Expected either 'send' or 'receive'".to_string()),
"list" => Ok(Mode::List),
_ => Err("Expected either 'send' or 'receive' or 'list'".to_string()),

Check warning on line 75 in share/src/main.rs

View check run for this annotation

Codecov / codecov/patch

share/src/main.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
}
}
}
Expand Down
1 change: 1 addition & 0 deletions share/src/network/hole_puncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub fn punch(
.listen_on(relay_address.with(Protocol::P2pCircuit))
.unwrap();
}
_ => {}
}
let mut connection_deets = ConnectionDetails::new();

Expand Down
2 changes: 1 addition & 1 deletion share/src/network/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn make_request(mode: Mode, swarm: &mut Swarm<Behaviour>, peer_id: PeerId, c
.request_response
.send_request(&peer_id, items);
}
Mode::Receive => {
Mode::Receive | Mode::List => {

Check warning on line 62 in share/src/network/request.rs

View check run for this annotation

Codecov / codecov/patch

share/src/network/request.rs#L62

Added line #L62 was not covered by tests
// if !is_ip_whitelisted(event, config)
}
}
Expand Down

0 comments on commit f2193f1

Please sign in to comment.