diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-25 00:14:09 -0500 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-25 00:14:09 -0500 |
commit | 9f4887d08a712646d5c9afc37737a267474b0905 (patch) | |
tree | cb83ee17e286f4d3ae314a97418e720ca0aec0e8 | |
parent | feb3931bd8ebc073e254fed0de966e71a668f71b (diff) |
add new cli commands
-rw-r--r-- | ipfs-cli/src/command/add.rs | 2 | ||||
-rw-r--r-- | ipfs-cli/src/command/cat.rs | 29 | ||||
-rw-r--r-- | ipfs-cli/src/command/commands.rs | 47 | ||||
-rw-r--r-- | ipfs-cli/src/command/mod.rs | 2 | ||||
-rw-r--r-- | ipfs-cli/src/main.rs | 4 |
5 files changed, 83 insertions, 1 deletions
diff --git a/ipfs-cli/src/command/add.rs b/ipfs-cli/src/command/add.rs index cc5c5e6..93e7c37 100644 --- a/ipfs-cli/src/command/add.rs +++ b/ipfs-cli/src/command/add.rs @@ -16,7 +16,7 @@ use tokio_core::reactor::Core; pub fn signature<'a, 'b>() -> App<'a, 'b> { clap_app!( @subcommand add => - (about: "Add file to ipfs") + (about: "Add file to IPFS") (@arg INPUT: +required {verify_file} "File to add") ) } diff --git a/ipfs-cli/src/command/cat.rs b/ipfs-cli/src/command/cat.rs new file mode 100644 index 0000000..55872b0 --- /dev/null +++ b/ipfs-cli/src/command/cat.rs @@ -0,0 +1,29 @@ +// 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 cat => + (about: "Show IPFS object data") + (@arg PATH: +required "The path of the IPFS object to get") + ) +} + + +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + let path = args.value_of("PATH").unwrap(); + let data = core.run(client.cat(&path)).expect(EXPECTED_API); + + println!("{}", String::from_utf8_lossy(&data)); +} diff --git a/ipfs-cli/src/command/commands.rs b/ipfs-cli/src/command/commands.rs new file mode 100644 index 0000000..481b5a0 --- /dev/null +++ b/ipfs-cli/src/command/commands.rs @@ -0,0 +1,47 @@ +// 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; +use command::EXPECTED_API; +use ipfs_api::IpfsClient; +use ipfs_api::response::CommandsResponse; +use tokio_core::reactor::Core; + + +pub fn signature<'a, 'b>() -> App<'a, 'b> { + clap_app!( + @subcommand commands => + (about: "List all available commands") + ) +} + + +fn recursive_print_commands(cmd: CommandsResponse, stack: &mut Vec<String>) { + if cmd.subcommands.is_empty() { + println!(" {} {}", stack.join(" "), cmd.name); + } else { + let (name, subcommands) = (cmd.name, cmd.subcommands); + + stack.push(name); + + for subcommand in subcommands { + recursive_print_commands(subcommand, stack); + } + + stack.pop(); + } +} + + +pub fn handle(core: &mut Core, client: &IpfsClient) { + let commands = core.run(client.commands()).expect(EXPECTED_API); + + println!(""); + recursive_print_commands(commands, &mut Vec::new()); + println!(""); +} diff --git a/ipfs-cli/src/command/mod.rs b/ipfs-cli/src/command/mod.rs index 498fafb..de20d77 100644 --- a/ipfs-cli/src/command/mod.rs +++ b/ipfs-cli/src/command/mod.rs @@ -28,4 +28,6 @@ pub mod add; pub mod bitswap; pub mod block; pub mod bootstrap; +pub mod cat; +pub mod commands; pub mod version; diff --git a/ipfs-cli/src/main.rs b/ipfs-cli/src/main.rs index 3849272..116ac55 100644 --- a/ipfs-cli/src/main.rs +++ b/ipfs-cli/src/main.rs @@ -28,6 +28,8 @@ fn main() { (subcommand: command::bitswap::signature()) (subcommand: command::block::signature()) (subcommand: command::bootstrap::signature()) + (subcommand: command::cat::signature()) + (subcommand: command::commands::signature()) (subcommand: command::version::signature()) ).get_matches(); @@ -41,6 +43,8 @@ fn main() { ("bootstrap", Some(bootstrap)) => { command::bootstrap::handle(&mut core, &client, &bootstrap) } + ("cat", Some(cat)) => command::cat::handle(&mut core, &client, &cat), + ("commands", _) => command::commands::handle(&mut core, &client), ("version", _) => command::version::handle(&mut core, &client), _ => unreachable!(), } |