diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-25 10:59:27 -0500 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-25 10:59:27 -0500 |
commit | b2ba32dcd849732d62eaed5940f5db5367e42fe1 (patch) | |
tree | 2e33b4545b7557c1e8f7b4f59ff0f17d8b708c07 | |
parent | 9f4887d08a712646d5c9afc37737a267474b0905 (diff) |
refactoring, and add new commands
-rw-r--r-- | ipfs-cli/Cargo.toml | 1 | ||||
-rw-r--r-- | ipfs-cli/src/command/add.rs | 4 | ||||
-rw-r--r-- | ipfs-cli/src/command/bitswap.rs | 10 | ||||
-rw-r--r-- | ipfs-cli/src/command/block.rs | 8 | ||||
-rw-r--r-- | ipfs-cli/src/command/bootstrap.rs | 4 | ||||
-rw-r--r-- | ipfs-cli/src/command/config.rs | 50 | ||||
-rw-r--r-- | ipfs-cli/src/command/dag.rs | 46 | ||||
-rw-r--r-- | ipfs-cli/src/command/dht.rs | 61 | ||||
-rw-r--r-- | ipfs-cli/src/command/mod.rs | 4 | ||||
-rw-r--r-- | ipfs-cli/src/main.rs | 19 |
10 files changed, 187 insertions, 20 deletions
diff --git a/ipfs-cli/Cargo.toml b/ipfs-cli/Cargo.toml index 7b1cb70..f7bcb2c 100644 --- a/ipfs-cli/Cargo.toml +++ b/ipfs-cli/Cargo.toml @@ -6,5 +6,6 @@ authors = ["Ferris Tseng <ferristseng@fastmail.fm>"] [dependencies] clap = "2.27" +futures = "0.1" ipfs-api = { path = "../ipfs-api" } tokio-core = "0.1" diff --git a/ipfs-cli/src/command/add.rs b/ipfs-cli/src/command/add.rs index 93e7c37..0ff592d 100644 --- a/ipfs-cli/src/command/add.rs +++ b/ipfs-cli/src/command/add.rs @@ -7,7 +7,7 @@ // use clap::{App, ArgMatches}; -use command::{verify_file, EXPECTED_API}; +use command::{verify_file, EXPECTED_API, EXPECTED_FILE}; use ipfs_api::IpfsClient; use std::fs::File; use tokio_core::reactor::Core; @@ -24,7 +24,7 @@ pub fn signature<'a, 'b>() -> App<'a, 'b> { pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { let path = args.value_of("INPUT").unwrap(); - let file = File::open(path).expect("expected to read input file"); + let file = File::open(path).expect(EXPECTED_FILE); let response = core.run(client.add(file)).expect(EXPECTED_API); println!(""); diff --git a/ipfs-cli/src/command/bitswap.rs b/ipfs-cli/src/command/bitswap.rs index fe03048..f3ccb0a 100644 --- a/ipfs-cli/src/command/bitswap.rs +++ b/ipfs-cli/src/command/bitswap.rs @@ -34,9 +34,9 @@ pub fn signature<'a, 'b>() -> App<'a, 'b> { } -pub fn handle(core: &mut Core, client: &IpfsClient, bitswap: &ArgMatches) { - match bitswap.subcommand() { - ("ledger", Some(ref args)) => { +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { + ("ledger", Some(args)) => { let peer = args.value_of("PEER").unwrap(); let ledger = core.run(client.bitswap_ledger(&peer)).expect(EXPECTED_API); @@ -69,14 +69,14 @@ pub fn handle(core: &mut Core, client: &IpfsClient, bitswap: &ArgMatches) { println!(" dup_data_received : {}", stat.dup_data_received); println!(""); } - ("unwant", Some(ref args)) => { + ("unwant", Some(args)) => { let key = args.value_of("KEY").unwrap(); core.run(client.bitswap_unwant(&key)).expect(EXPECTED_API); println!("OK"); } - ("wantlist", Some(ref args)) => { + ("wantlist", Some(args)) => { let peer = args.value_of("PEER"); let wantlist = core.run(client.bitswap_wantlist(peer)).expect(EXPECTED_API); diff --git a/ipfs-cli/src/command/block.rs b/ipfs-cli/src/command/block.rs index 848b799..8cf004e 100644 --- a/ipfs-cli/src/command/block.rs +++ b/ipfs-cli/src/command/block.rs @@ -7,7 +7,7 @@ // use clap::{App, ArgMatches}; -use command::{verify_file, EXPECTED_API}; +use command::{verify_file, EXPECTED_API, EXPECTED_FILE}; use ipfs_api::IpfsClient; use std::fs::File; use tokio_core::reactor::Core; @@ -37,8 +37,8 @@ pub fn signature<'a, 'b>() -> App<'a, 'b> { } -pub fn handle(core: &mut Core, client: &IpfsClient, block: &ArgMatches) { - match block.subcommand() { +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { ("get", Some(args)) => { let key = args.value_of("KEY").unwrap(); let block = core.run(client.block_get(key)).expect(EXPECTED_API); @@ -47,7 +47,7 @@ pub fn handle(core: &mut Core, client: &IpfsClient, block: &ArgMatches) { } ("put", Some(args)) => { let path = args.value_of("INPUT").unwrap(); - let file = File::open(path).expect("expected to read input file"); + let file = File::open(path).expect(EXPECTED_FILE); let block = core.run(client.block_put(file)).expect(EXPECTED_API); println!(""); diff --git a/ipfs-cli/src/command/bootstrap.rs b/ipfs-cli/src/command/bootstrap.rs index 04d3022..63c5925 100644 --- a/ipfs-cli/src/command/bootstrap.rs +++ b/ipfs-cli/src/command/bootstrap.rs @@ -45,8 +45,8 @@ fn print_peers(peers: &Vec<String>) { } -pub fn handle(core: &mut Core, client: &IpfsClient, bootstrap: &ArgMatches) { - match bootstrap.subcommand() { +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { ("add", Some(add)) => { match add.subcommand() { ("default", _) => { diff --git a/ipfs-cli/src/command/config.rs b/ipfs-cli/src/command/config.rs new file mode 100644 index 0000000..dad83a1 --- /dev/null +++ b/ipfs-cli/src/command/config.rs @@ -0,0 +1,50 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or +// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or +// http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use clap::{App, ArgMatches}; +use command::{verify_file, EXPECTED_API, EXPECTED_FILE}; +use ipfs_api::IpfsClient; +use std::fs::File; +use tokio_core::reactor::Core; + + +pub fn signature<'a, 'b>() -> App<'a, 'b> { + clap_app!( + @subcommand config => + (@setting SubcommandRequiredElseHelp) + (@subcommand edit => + (about: "Open the config file for editing") + ) + (@subcommand replace => + (about: "Replace the config file") + (@arg INPUT: +required {verify_file} "Config file to replace with") + ) + (@subcommand show => + (about: "Show the config file") + ) + ) +} + + +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { + ("edit", _) => core.run(client.config_edit()).expect(EXPECTED_API), + ("replace", Some(args)) => { + let path = args.value_of("INPUT").unwrap(); + let config = File::open(path).expect(EXPECTED_FILE); + + core.run(client.config_replace(config)).expect(EXPECTED_API); + } + ("show", _) => { + let config = core.run(client.config_show()).expect(EXPECTED_API); + + println!("{}", config); + } + _ => unreachable!(), + } +} diff --git a/ipfs-cli/src/command/dag.rs b/ipfs-cli/src/command/dag.rs new file mode 100644 index 0000000..e27375f --- /dev/null +++ b/ipfs-cli/src/command/dag.rs @@ -0,0 +1,46 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or +// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or +// http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use clap::{App, ArgMatches}; +use command::EXPECTED_API; +use ipfs_api::IpfsClient; +use tokio_core::reactor::Core; + + +pub fn signature<'a, 'b>() -> App<'a, 'b> { + clap_app!( + @subcommand dag => + (@setting SubcommandRequiredElseHelp) + (@subcommand get => + (about: "Get a dag node from IPFS") + (@arg KEY: +required "The key of the object to get") + ) + ) +} + + +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { + ("get", Some(args)) => { + let key = args.value_of("KEY").unwrap(); + let dag = core.run(client.dag_get(key)).expect(EXPECTED_API); + + println!(""); + if let Some(data) = dag.data { + println!(" data :"); + println!("{}", data); + } + println!(" links :"); + for link in dag.links { + println!(" {} ({}) ({:?})", link.name, link.size, link.cid); + } + println!(""); + } + _ => unreachable!(), + } +} diff --git a/ipfs-cli/src/command/dht.rs b/ipfs-cli/src/command/dht.rs new file mode 100644 index 0000000..dc28188 --- /dev/null +++ b/ipfs-cli/src/command/dht.rs @@ -0,0 +1,61 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or +// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or +// http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use clap::{App, ArgMatches}; +use command::EXPECTED_API; +use futures::stream::Stream; +use ipfs_api::IpfsClient; +use ipfs_api::response::DhtMessage; +use tokio_core::reactor::Core; + + +pub fn signature<'a, 'b>() -> App<'a, 'b> { + clap_app!( + @subcommand dht => + (@setting SubcommandRequiredElseHelp) + (@subcommand findpeer => + (about: "Query the DHT for all of the multiaddresses associated with a Peer ID") + (@arg PEER: +required "Peer to search for") + ) + ) +} + + +fn print_dht_response(res: DhtMessage) { + println!(""); + println!(" id : {}", res.id); + println!(" type : {}", res.typ); + println!(" responses :"); + for peer_res in res.responses { + println!(" id : {}", peer_res.id); + println!(" addrs :"); + for addr in peer_res.addrs { + println!(" {}", addr); + } + println!(""); + } + println!(" extra : {}", res.extra); + println!(""); +} + + +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { + ("findpeer", Some(args)) => { + let peer = args.value_of("PEER").unwrap(); + let req = client.dht_findpeer(&peer).for_each(|peer| { + print_dht_response(peer); + + Ok(()) + }); + + core.run(req).expect(EXPECTED_API); + } + _ => unreachable!(), + } +} diff --git a/ipfs-cli/src/command/mod.rs b/ipfs-cli/src/command/mod.rs index de20d77..cfb5582 100644 --- a/ipfs-cli/src/command/mod.rs +++ b/ipfs-cli/src/command/mod.rs @@ -11,6 +11,7 @@ use std::fs; pub const EXPECTED_API: &'static str = "expected response from API"; +pub const EXPECTED_FILE: &'static str = "expected to read input file"; /// Verifies that a path points to a file that exists, and not a directory. @@ -30,4 +31,7 @@ pub mod block; pub mod bootstrap; pub mod cat; pub mod commands; +pub mod config; +pub mod dag; +pub mod dht; pub mod version; diff --git a/ipfs-cli/src/main.rs b/ipfs-cli/src/main.rs index 116ac55..92bfc29 100644 --- a/ipfs-cli/src/main.rs +++ b/ipfs-cli/src/main.rs @@ -8,6 +8,7 @@ #[macro_use] extern crate clap; +extern crate futures; extern crate ipfs_api; extern crate tokio_core; @@ -30,6 +31,9 @@ fn main() { (subcommand: command::bootstrap::signature()) (subcommand: command::cat::signature()) (subcommand: command::commands::signature()) + (subcommand: command::config::signature()) + (subcommand: command::dag::signature()) + (subcommand: command::dht::signature()) (subcommand: command::version::signature()) ).get_matches(); @@ -37,14 +41,15 @@ fn main() { let client = IpfsClient::default(&core.handle()); match matches.subcommand() { - ("add", Some(args)) => command::add::handle(&mut core, &client, args), - ("bitswap", Some(bitswap)) => command::bitswap::handle(&mut core, &client, &bitswap), - ("block", Some(block)) => command::block::handle(&mut core, &client, &block), - ("bootstrap", Some(bootstrap)) => { - command::bootstrap::handle(&mut core, &client, &bootstrap) - } - ("cat", Some(cat)) => command::cat::handle(&mut core, &client, &cat), + ("add", Some(args)) => command::add::handle(&mut core, &client, &args), + ("bitswap", Some(args)) => command::bitswap::handle(&mut core, &client, &args), + ("block", Some(args)) => command::block::handle(&mut core, &client, &args), + ("bootstrap", Some(args)) => command::bootstrap::handle(&mut core, &client, &args), + ("cat", Some(args)) => command::cat::handle(&mut core, &client, &args), ("commands", _) => command::commands::handle(&mut core, &client), + ("config", Some(args)) => command::config::handle(&mut core, &client, &args), + ("dag", Some(args)) => command::dag::handle(&mut core, &client, &args), + ("dht", Some(args)) => command::dht::handle(&mut core, &client, &args), ("version", _) => command::version::handle(&mut core, &client), _ => unreachable!(), } |