Skip to content

Commit

Permalink
feat(iroh-cli)!: realign cli commands with library (#2522)
Browse files Browse the repository at this point in the history
## Description

We had quite a few missmatches between CLI and library naming, this
aligns it.

## Breaking Changes

- cli:
  -  `author` -> `authors`
  - `blob` -> `blobs`
  - `doc` -> `docs`
  - `tag` -> `tags`
  - `new` -> `create`

## Notes & open questions

- Depends on n0-computer/chuck#61

## Change checklist

- [x] Self-review.
- [x] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [x] Tests if relevant.
- [x] All breaking changes documented.
  • Loading branch information
dignifiedquire authored Jul 22, 2024
1 parent 0102b05 commit 4c11c58
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 27 deletions.
12 changes: 6 additions & 6 deletions iroh-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ use iroh::client::Iroh;

use crate::config::{ConsoleEnv, NodeConfig};

use self::blob::{BlobAddOptions, BlobSource};
use self::blobs::{BlobAddOptions, BlobSource};
use self::rpc::RpcCommands;
use self::start::RunType;

pub(crate) mod author;
pub(crate) mod blob;
pub(crate) mod authors;
pub(crate) mod blobs;
pub(crate) mod console;
pub(crate) mod doc;
pub(crate) mod docs;
pub(crate) mod doctor;
pub(crate) mod gossip;
pub(crate) mod node;
pub(crate) mod rpc;
pub(crate) mod start;
pub(crate) mod tag;
pub(crate) mod tags;

/// iroh is a tool for syncing bytes
/// https://iroh.computer/docs
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Cli {
}
let config = Self::load_config(self.config, self.metrics_port).await?;

let add_command = add.map(|source| blob::BlobCommands::Add {
let add_command = add.map(|source| blobs::BlobCommands::Add {
source,
options: add_options,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum AuthorCommands {
/// Set the active author (only works within the Iroh console).
Switch { author: AuthorId },
/// Create a new author.
New {
Create {
/// Switch to the created author (only in the Iroh console).
#[clap(long)]
switch: bool,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl AuthorCommands {
println!("Active author is now {}", fmt_short(author_id.as_bytes()));
}
}
Self::New { switch } => {
Self::Create { switch } => {
if switch && !env.is_console() {
bail!("The --switch flag is only supported within the Iroh console.");
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub enum DocCommands {
/// Set the active document (only works within the Iroh console).
Switch { id: NamespaceId },
/// Create a new document.
New {
Create {
/// Switch to the created document (only in the Iroh console).
#[clap(long)]
switch: bool,
Expand Down Expand Up @@ -308,7 +308,7 @@ impl DocCommands {
env.set_doc(doc)?;
println!("Active doc is now {}", fmt_short(doc.as_bytes()));
}
Self::New { switch } => {
Self::Create { switch } => {
if switch && !env.is_console() {
bail!("The --switch flag is only supported within the Iroh console.");
}
Expand Down
56 changes: 49 additions & 7 deletions iroh-cli/src/commands/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::net::SocketAddr;
use std::time::Duration;

use anyhow::Result;
Expand All @@ -8,18 +9,17 @@ use comfy_table::{presets::NOTHING, Cell};
use futures_lite::{Stream, StreamExt};
use human_time::ToHumanTimeString;
use iroh::client::Iroh;
use iroh::net::{
endpoint::{ConnectionInfo, DirectAddrInfo},
key::PublicKey,
};
use iroh::net::endpoint::{ConnectionInfo, DirectAddrInfo};
use iroh::net::relay::RelayUrl;
use iroh::net::{NodeAddr, NodeId};

#[derive(Subcommand, Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum NodeCommands {
/// Get information about the different connections we have made
Connections,
/// Get connection information about a particular node
Connection { node_id: PublicKey },
ConnectionInfo { node_id: NodeId },
/// Get status of the running node.
Status,
/// Get statistics and metrics from the running node.
Expand All @@ -33,6 +33,16 @@ pub enum NodeCommands {
#[clap(long, default_value_t = false)]
force: bool,
},
/// Get the node addr of this node.
NodeAddr,
/// Add this node addr to the known nodes.
AddNodeAddr {
node_id: NodeId,
relay: Option<RelayUrl>,
addresses: Vec<SocketAddr>,
},
/// Get the relay server we are connected to.
HomeRelay,
}

impl NodeCommands {
Expand All @@ -51,7 +61,7 @@ impl NodeCommands {
fmt_connections(connections).await
);
}
Self::Connection { node_id } => {
Self::ConnectionInfo { node_id } => {
let conn_info = iroh.connection_info(node_id).await?;
match conn_info {
Some(info) => println!("{}", fmt_connection(info)),
Expand All @@ -73,12 +83,44 @@ impl NodeCommands {
Self::Status => {
let response = iroh.status().await?;
println!("Listening addresses: {:#?}", response.listen_addrs);
println!("Node public key: {}", response.addr.node_id);
println!("Node ID: {}", response.addr.node_id);
println!("Version: {}", response.version);
if let Some(addr) = response.rpc_addr {
println!("RPC Addr: {}", addr);
}
}
Self::NodeAddr => {
let addr = iroh.node_addr().await?;
println!("Node ID: {}", addr.node_id);
let relay = addr
.info
.relay_url
.map(|s| s.to_string())
.unwrap_or_else(|| "Not Available".to_string());
println!("Home Relay: {}", relay);
println!("Direct Addresses ({}):", addr.info.direct_addresses.len());
for da in &addr.info.direct_addresses {
println!(" {}", da);
}
}
Self::AddNodeAddr {
node_id,
relay,
addresses,
} => {
let mut addr = NodeAddr::new(node_id).with_direct_addresses(addresses);
if let Some(relay) = relay {
addr = addr.with_relay_url(relay);
}
iroh.add_node_addr(addr).await?;
}
Self::HomeRelay => {
let relay = iroh.home_relay().await?;
let relay = relay
.map(|s| s.to_string())
.unwrap_or_else(|| "Not Available".to_string());
println!("Home Relay: {}", relay);
}
}
Ok(())
}
Expand Down
20 changes: 10 additions & 10 deletions iroh-cli/src/commands/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use iroh::client::Iroh;
use crate::config::ConsoleEnv;

use super::{
author::AuthorCommands, blob::BlobCommands, doc::DocCommands, gossip::GossipCommands,
node::NodeCommands, tag::TagCommands,
authors::AuthorCommands, blobs::BlobCommands, docs::DocCommands, gossip::GossipCommands,
node::NodeCommands, tags::TagCommands,
};

#[derive(Subcommand, Debug, Clone)]
Expand All @@ -16,23 +16,23 @@ pub enum RpcCommands {
///
/// Documents are mutable, syncable key-value stores.
/// For more on docs see https://iroh.computer/docs/layers/documents
Doc {
Docs {
#[clap(subcommand)]
command: DocCommands,
},

/// Manage document authors
///
/// Authors are keypairs that identify writers to documents.
Author {
Authors {
#[clap(subcommand)]
command: AuthorCommands,
},
/// Manage blobs
///
/// Blobs are immutable, opaque chunks of arbitrary-sized data.
/// For more on blobs see https://iroh.computer/docs/layers/blobs
Blob {
Blobs {
#[clap(subcommand)]
command: BlobCommands,
},
Expand All @@ -57,7 +57,7 @@ pub enum RpcCommands {
/// a tag.
///
/// Any data iroh fetches without a tag will be periodically deleted.
Tag {
Tags {
#[clap(subcommand)]
command: TagCommands,
},
Expand All @@ -67,10 +67,10 @@ impl RpcCommands {
pub async fn run(self, iroh: &Iroh, env: &ConsoleEnv) -> Result<()> {
match self {
Self::Node { command } => command.run(iroh).await,
Self::Blob { command } => command.run(iroh).await,
Self::Doc { command } => command.run(iroh, env).await,
Self::Author { command } => command.run(iroh, env).await,
Self::Tag { command } => command.run(iroh).await,
Self::Blobs { command } => command.run(iroh).await,
Self::Docs { command } => command.run(iroh, env).await,
Self::Authors { command } => command.run(iroh, env).await,
Self::Tags { command } => command.run(iroh).await,
Self::Gossip { command } => command.run(iroh).await,
}
}
Expand Down
File renamed without changes.

0 comments on commit 4c11c58

Please sign in to comment.