diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-24 17:15:02 -0500 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-24 17:15:02 -0500 |
commit | dcd6f22f7c21ee8985e5de7f962818ac93bc5b26 (patch) | |
tree | d24402034645fd98f40a3df3e8f63c0a20f4c807 | |
parent | 488617996748464a546d1c036802e27ec0e975dc (diff) |
add initial cli implementation
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | ipfs-api/src/response/bitswap.rs | 2 | ||||
-rw-r--r-- | ipfs-cli/Cargo.toml | 10 | ||||
-rw-r--r-- | ipfs-cli/src/main.rs | 91 |
4 files changed, 104 insertions, 2 deletions
@@ -1,5 +1,6 @@ [workspace] members = [ - "ipfs-api" + "ipfs-api", + "ipfs-cli" ] diff --git a/ipfs-api/src/response/bitswap.rs b/ipfs-api/src/response/bitswap.rs index 9298355..acfad56 100644 --- a/ipfs-api/src/response/bitswap.rs +++ b/ipfs-api/src/response/bitswap.rs @@ -16,7 +16,7 @@ pub struct BitswapLedgerResponse { pub value: f64, pub sent: u64, pub recv: u64, - pub exchange: u64, + pub exchanged: u64, } diff --git a/ipfs-cli/Cargo.toml b/ipfs-cli/Cargo.toml new file mode 100644 index 0000000..7b1cb70 --- /dev/null +++ b/ipfs-cli/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ipfs-cli" +version = "0.4.0" +license = "MIT OR Apache-2.0" +authors = ["Ferris Tseng <ferristseng@fastmail.fm>"] + +[dependencies] +clap = "2.27" +ipfs-api = { path = "../ipfs-api" } +tokio-core = "0.1" diff --git a/ipfs-cli/src/main.rs b/ipfs-cli/src/main.rs new file mode 100644 index 0000000..815674b --- /dev/null +++ b/ipfs-cli/src/main.rs @@ -0,0 +1,91 @@ +#[macro_use] +extern crate clap; +extern crate ipfs_api; +extern crate tokio_core; + +use ipfs_api::IpfsClient; +use std::fs::File; +use tokio_core::reactor::Core; + +fn main() { + let matches = clap_app!( + app => + (name: "IPFS CLI") + (about: "CLI for Go IPFS") + (version: crate_version!()) + (author: "Ferris T. <ferristseng@fastmail.fm>") + (@setting SubcommandRequiredElseHelp) + (@subcommand add => + (about: "Add file to ipfs") + (@arg INPUT: +required "File to add") + ) + (@subcommand bitswap => + (@setting SubcommandRequiredElseHelp) + (@subcommand ledger => + (about: "Show the current ledger for a peer") + (@arg PEER: +required "Peer to inspect") + ) + ) + (@subcommand version => + (about: "Show ipfs version information") + ) + ).get_matches(); + + let mut core = Core::new().expect("expected event loop"); + let client = IpfsClient::default(&core.handle()); + + match matches.subcommand() { + ("add", Some(args)) => { + let path = args.value_of("INPUT").unwrap(); + let file = File::open(path).expect("expected to read input file"); + let metadata = file.metadata().expect("expected to read file's metadata"); + + if !metadata.is_file() { + panic!("input must be a file not directory"); + } + + let response = core.run(client.add(file)).expect( + "expected response from API", + ); + + println!(""); + println!(" name : {}", response.name); + println!(" hash : {}", response.hash); + println!(" size : {}", response.size); + println!(""); + } + ("bitswap", Some(bitswap)) => { + match bitswap.subcommand() { + ("ledger", Some(ledger)) => { + let peer = ledger.value_of("PEER").unwrap(); + let ledger = core.run(client.bitswap_ledger(&peer)).expect( + "expected response from API", + ); + + println!(""); + println!(" peer : {}", ledger.peer); + println!(" value : {}", ledger.value); + println!(" sent : {}", ledger.sent); + println!(" recv : {}", ledger.recv); + println!(" exchanged : {}", ledger.exchanged); + println!(""); + } + _ => unreachable!(), + } + } + ("version", _) => { + let version = core.run(client.version()).expect( + "expected response from API", + ); + + println!(""); + println!(" version : {}", version.version); + println!(" commit : {}", version.commit); + println!(" repo : {}", version.repo); + println!(" system : {}", version.system); + println!(" golang : {}", version.golang); + println!(""); + } + _ => unreachable!(), + } +} |