Skip to content

Commit

Permalink
refactor: iroh-ctl now uses iroh to implement itself
Browse files Browse the repository at this point in the history
For now I've disabled the "gateway" CLI argument as that needs more work on
the iroh API side
  • Loading branch information
faassen committed Sep 26, 2022
1 parent a27d2d3 commit c30c1e1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 66 deletions.
9 changes: 8 additions & 1 deletion iroh-ctl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
name = "iroh-ctl"
version = "0.1.0"
edition = "2021"
authors = ["Kasey Huizinga <[email protected]>"]
authors = ["Kasey Huizinga <[email protected]>", "Martijn Faassen <[email protected]>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/n0-computer/iroh"
description = "Client for interacting with running iroh processes."
default-run = "iroh-ctl"

[[bin]]
bench = false
path = "src/main.rs"
name = "iroh-ctl"

[dependencies]
anyhow = "1.0"
Expand All @@ -22,6 +28,7 @@ iroh-util = { path = "../iroh-util" }
serde = { version = "1.0", features = ["derive"] }
git-version = "0.3.5"
iroh-metrics = { path = "../iroh-metrics", default-features = false, features = ["rpc-grpc"] }
iroh = { path = "../iroh"}
libp2p = { version = "0.48", default-features = false }
cid = "0.8.5"
multiaddr = "0.14.0"
Expand Down
9 changes: 4 additions & 5 deletions iroh-ctl/src/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Result;
use clap::{Args, Subcommand};
use iroh_rpc_client::Client;

#[derive(Args, Debug, Clone)]
pub struct Gateway {
Expand Down Expand Up @@ -30,11 +29,12 @@ pub enum DevCommands {
Head,
}

pub async fn run_command(rpc: Client, g: Gateway) -> Result<()> {
pub async fn run_command(g: Gateway) -> Result<()> {
match g.command {
GatewayCommands::Version => {
let v = rpc.try_gateway()?.version().await?;
println!("v{}", v);
todo!("Gateway version not yet implemented");
// let v = rpc.try_gateway()?.version().await?;
// println!("v{}", v);
}
GatewayCommands::Dev(dev) => match dev.command {
DevCommands::Get { curl } => {
Expand All @@ -45,5 +45,4 @@ pub async fn run_command(rpc: Client, g: Gateway) -> Result<()> {
}
},
}
Ok(())
}
38 changes: 25 additions & 13 deletions iroh-ctl/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;
use std::path::PathBuf;

use anyhow::Result;
use clap::{Parser, Subcommand};
use iroh::{api, Api};
use iroh_ctl::{
gateway::{run_command as run_gateway_command, Gateway},
p2p::{run_command as run_p2p_command, P2p},
Expand Down Expand Up @@ -37,12 +39,12 @@ impl Cli {
#[derive(Subcommand, Debug, Clone)]
enum Commands {
/// status checks the health of the different processes
#[clap(about = "Check the health of the different iroh processes.")]
Status {
#[clap(short, long)]
/// when true, updates the status table whenever a change in a process's status occurs
watch: bool,
},
// #[clap(about = "Check the health of the different iroh processes.")]
// Status {
// #[clap(short, long)]
// /// when true, updates the status table whenever a change in a process's status occurs
// watch: bool,
// },
Version,
P2p(P2p),
Store(Store),
Expand All @@ -69,16 +71,26 @@ async fn main() -> anyhow::Result<()> {

let client = Client::new(config.rpc_client).await?;

let api = Api::new(&client).await?;

run_cli_command(&api, cli).await
}

async fn run_cli_command<A: api::Api<P, S>, P: api::P2p, S: api::Store>(
api: &A,
cli: Cli,
) -> Result<()> {
match cli.command {
Commands::Status { watch } => {
crate::status::status(client, watch).await?;
}
// Commands::Status { watch } => {
// crate::status::status(client, watch).await?;
// }
Commands::Version => {
println!("v{}", env!("CARGO_PKG_VERSION"));
let version = api.version().await?;
println!("v{}", version);
}
Commands::P2p(p2p) => run_p2p_command(client, p2p).await?,
Commands::Store(store) => run_store_command(client, store).await?,
Commands::Gateway(gateway) => run_gateway_command(client, gateway).await?,
Commands::P2p(p2p) => run_p2p_command(api.p2p()?, p2p).await?,
Commands::Store(store) => run_store_command(api.store()?, store).await?,
Commands::Gateway(gateway) => run_gateway_command(gateway).await?,
};

Ok(())
Expand Down
75 changes: 34 additions & 41 deletions iroh-ctl/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use anyhow::Error;
use std::collections::HashSet;
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Result;
use bytes::Bytes;
use anyhow::{Error, Result};
use cid::Cid;
use clap::{Args, Subcommand};
use iroh_rpc_client::Client;
use libp2p::{gossipsub::TopicHash, Multiaddr, PeerId};
use tokio::{fs::File, io::stdin, io::AsyncReadExt};
use iroh::api;
use libp2p::{Multiaddr, PeerId};

#[derive(Args, Debug, Clone)]
#[clap(about = "Manage peer-2-peer networking.")]
Expand Down Expand Up @@ -223,43 +219,47 @@ pub enum GossipsubCommands {
},
}

pub async fn run_command(rpc: Client, cmd: P2p) -> Result<()> {
pub async fn run_command<P: api::P2p>(p2p: P, cmd: P2p) -> Result<()> {
match cmd.command {
P2pCommands::Version => {
let v = rpc.try_p2p()?.version().await?;
println!("v{}", v);
let version = p2p.p2p_version().await?;
println!("v{}", version);
}
P2pCommands::Addrs(addrs) => match addrs.command {
None => {
let addrs = rpc.try_p2p()?.get_peers().await?;
println!("{:#?}", addrs);
let peer_map = p2p.peers().await?;
println!("{:#?}", peer_map);
}
Some(AddrsCommands::Listen) => {
let addrs = rpc.try_p2p()?.get_listening_addrs().await?;
let addrs = p2p.addrs_listen().await?;
println!("{:#?}", addrs);
}
Some(AddrsCommands::Local) => {
todo!("Local not yet implemented.");
let addrs = p2p.addrs_local().await?;
println!("external addressses:");
addrs.iter().for_each(|a| println!("\t:{:?}", a));
}
},
P2pCommands::Connect { peer_id, addrs } => {
rpc.try_p2p()?.connect(peer_id, addrs).await?;
p2p.connect(&peer_id, &addrs).await?;
println!("connected to {}", peer_id);
}
P2pCommands::Disconnect { peer_id } => {
rpc.try_p2p()?.disconnect(peer_id).await?;
p2p.disconnect(&peer_id).await?;
println!("disconnected from {}", peer_id);
}
P2pCommands::Peers => {
let peers: Vec<PeerId> = rpc.try_p2p()?.get_peers().await?.into_keys().collect();
println!("{:#?}", peers);
let peer_ids = p2p.peer_ids().await?;
for peer_id in peer_ids {
println!("{}", peer_id);
}
}
P2pCommands::Ping { ping_args, count } => {
todo!("{:?} {:?}", ping_args, count);
}
P2pCommands::Dht(d) => match d.command {
DhtCommands::FindProvs { cid } => {
let providers = rpc.try_p2p()?.fetch_providers(&cid).await?;
let providers = p2p.fetch_providers(&cid).await?;
for prov in providers {
println!("{}", prov);
}
Expand All @@ -271,40 +271,33 @@ pub async fn run_command(rpc: Client, cmd: P2p) -> Result<()> {
}
P2pCommands::Dev(dev) => match dev.command {
DevCommands::FetchBitswap { cid, providers } => {
let providers = HashSet::from_iter(providers.into_iter());
let res = rpc.try_p2p()?.fetch_bitswap(cid, providers).await?;
let res = p2p.fetch_bitswap(&cid, &providers).await?;
println!("{:#?}", res);
}
DevCommands::FetchProviders { cid } => {
let res = rpc.try_p2p()?.fetch_providers(&cid).await?;
let res = p2p.fetch_providers(&cid).await?;
println!("{:#?}", res);
}
DevCommands::Gossipsub(g) => match g.command {
GossipsubCommands::Publish { topic, file } => {
let mut v: Vec<u8> = Vec::new();
if let Some(file) = file {
let mut f = File::open(file).await?;
f.read_to_end(&mut v).await?;
} else {
stdin().read_to_end(&mut v).await?;
}
let message_id = rpc
.try_p2p()?
.gossipsub_publish(TopicHash::from_raw(topic), Bytes::from(v))
.await?;
let message_id = p2p.publish(&topic, file.as_deref()).await?;
println!("Message Id: {}", message_id);
}
GossipsubCommands::Subscribe { topic } => {
rpc.try_p2p()?
.gossipsub_subscribe(TopicHash::from_raw(topic.clone()))
.await?;
println!("Subscribed to {}", topic);
let success = p2p.subscribe(&topic).await?;
if success {
println!("Subscribed to {}", topic);
} else {
println!("Failed to subscribe to {}", topic);
}
}
GossipsubCommands::Unsubscribe { topic } => {
rpc.try_p2p()?
.gossipsub_unsubscribe(TopicHash::from_raw(topic.clone()))
.await?;
println!("Unsubscribed from {}", topic);
let success = p2p.unsubscribe(&topic).await?;
if success {
println!("Unsubscribed from {}", topic);
} else {
println!("Failed to unsubscribe from {}", topic);
}
}
},
},
Expand Down
12 changes: 6 additions & 6 deletions iroh-ctl/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use anyhow::Result;
use cid::Cid;
use clap::{Args, Subcommand};
use iroh_rpc_client::Client;
use iroh::api;

#[derive(Args, Debug, Clone)]
pub struct Store {
Expand Down Expand Up @@ -49,15 +49,15 @@ Not yet implemented.",
Has { cid: Cid },
}

pub async fn run_command(rpc: Client, cmd: Store) -> Result<()> {
pub async fn run_command<S: api::Store>(store: S, cmd: Store) -> Result<()> {
match cmd.command {
StoreCommands::Version => {
let v = rpc.try_store()?.version().await?;
let v = store.store_version().await?;
println!("v{}", v);
}
StoreCommands::Block(block) => match block.command {
BlockCommands::Get { cid } => {
let b = rpc.try_store()?.get(cid).await?;
let b = store.block_get(&cid).await?;
println!("{:?}\n", b);
}
BlockCommands::Put { path } => {
Expand All @@ -70,12 +70,12 @@ pub async fn run_command(rpc: Client, cmd: Store) -> Result<()> {
);
}
BlockCommands::Has { cid } => {
let b = rpc.try_store()?.has(cid).await?;
let b = store.block_has(&cid).await?;
println!("{}", b);
}
},
StoreCommands::GetLinks { cid } => {
let links = rpc.try_store()?.get_links(cid).await?;
let links = store.get_links(&cid).await?;
println!("{:#?}", links);
}
};
Expand Down

0 comments on commit c30c1e1

Please sign in to comment.