diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-26 14:46:03 -0500 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2017-11-26 14:46:03 -0500 |
commit | 43413b530a8e81cb5442cf05423def787cdf1811 (patch) | |
tree | d0355220a52c40661deaaa7cecaa8fa3bb8a834c /ipfs-cli | |
parent | fd74a5d8d1c0ab51219814dad104d6a34944fc1f (diff) |
add files commands to cli
Diffstat (limited to 'ipfs-cli')
-rw-r--r-- | ipfs-cli/src/command/file.rs | 6 | ||||
-rw-r--r-- | ipfs-cli/src/command/files.rs | 171 | ||||
-rw-r--r-- | ipfs-cli/src/command/mod.rs | 1 | ||||
-rw-r--r-- | ipfs-cli/src/main.rs | 2 |
4 files changed, 177 insertions, 3 deletions
diff --git a/ipfs-cli/src/command/file.rs b/ipfs-cli/src/command/file.rs index 4235534..9a007a3 100644 --- a/ipfs-cli/src/command/file.rs +++ b/ipfs-cli/src/command/file.rs @@ -32,19 +32,19 @@ pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { println!(""); println!(" arguments :"); - for (k, arg) in ls.arguments.iter() { + for (k, arg) in ls.arguments { println!(" arg : {}", k); println!(" value : {}", arg); println!(""); } println!(" objects :"); - for (k, obj) in ls.objects.iter() { + for (k, obj) in ls.objects { println!(" key : {}", k); println!(" hash : {}", obj.hash); println!(" size : {}", obj.size); println!(" type : {}", obj.typ); println!(" links :"); - for link in obj.links.iter() { + for link in obj.links { println!(" name : {}", link.name); println!(" hash : {}", link.hash); println!(" size : {}", link.size); diff --git a/ipfs-cli/src/command/files.rs b/ipfs-cli/src/command/files.rs new file mode 100644 index 0000000..03bef88 --- /dev/null +++ b/ipfs-cli/src/command/files.rs @@ -0,0 +1,171 @@ +// 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 files => + (@setting SubcommandRequiredElseHelp) + (@subcommand cp => + (about: "Copy files in MFS") + (@arg SRC: +required "The source object to copy") + (@arg DEST: +required "The destination to copy the object to") + ) + (@subcommand flush => + (about: "Flush a path's data to disk") + (@arg PATH: "The path to flush") + ) + (@subcommand ls => + (about: "List directories in MFS") + (@arg PATH: "The past to list") + ) + (@subcommand mkdir => + (about: "Make directories in MFS") + (@arg PATH: +required "The directory to create") + (@arg parents: -p --parents "Create parents if the directory \ + does not already exist") + ) + (@subcommand mv => + (about: "Move files in MFS") + (@arg SRC: +required "The source object to move") + (@arg DEST: +required "The destination to move the object to") + ) + (@subcommand read => + (about: "Read a file in MFS") + (@arg PATH: +required "The path to read") + ) + (@subcommand rm => + (about: "Remove a file in MFS") + (@arg PATH: +required "The file to remove") + (@arg recursive: -r --recursive "Recursively remove directories") + ) + (@subcommand stat => + (about: "Display status for a file in MFS") + (@arg PATH: +required "The file to get status for") + ) + (@subcommand write => + (about: "Write a file to MFS") + (@arg DEST: +required "The path to write to") + (@arg INPUT: +required {verify_file} "The file to write") + (@arg create: --create "Create the file if it does not exist") + (@arg truncate: --truncate "Truncate the file before writing") + ) + ) +} + + +pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) { + match args.subcommand() { + ("cp", Some(args)) => { + let src = args.value_of("SRC").unwrap(); + let dest = args.value_of("DEST").unwrap(); + + core.run(client.files_cp(&src, &dest)).expect(EXPECTED_API); + + println!(""); + println!(" OK"); + println!(""); + } + ("flush", Some(args)) => { + let path = args.value_of("PATH"); + + core.run(client.files_flush(&path)).expect(EXPECTED_API); + + println!(""); + println!(" OK"); + println!(""); + } + ("ls", Some(args)) => { + let path = args.value_of("PATH"); + let ls = core.run(client.files_ls(&path)).expect(EXPECTED_API); + + println!(""); + println!(" entries :"); + for entry in ls.entries { + println!(" name : {}", entry.name); + println!(" type : {}", entry.typ); + println!(" size : {}", entry.size); + println!(" hash : {}", entry.hash); + println!(""); + } + println!(""); + } + ("mkdir", Some(args)) => { + let path = args.value_of("PATH").unwrap(); + + core.run(client.files_mkdir(&path, args.is_present("parents"))) + .expect(EXPECTED_API); + + println!(""); + println!(" OK"); + println!(""); + } + ("mv", Some(args)) => { + let src = args.value_of("SRC").unwrap(); + let dest = args.value_of("DEST").unwrap(); + + core.run(client.files_mv(&src, &dest)).expect(EXPECTED_API); + + println!(""); + println!(" OK"); + println!(""); + } + ("read", Some(args)) => { + let path = args.value_of("PATH").unwrap(); + let data = core.run(client.files_read(&path)).expect(EXPECTED_API); + + println!("{}", String::from_utf8_lossy(&data)); + } + ("rm", Some(args)) => { + let path = args.value_of("PATH").unwrap(); + let req = client.files_rm(&path, args.is_present("recursive")); + + core.run(req).expect(EXPECTED_API); + + println!(""); + println!(" OK"); + println!(""); + } + ("stat", Some(args)) => { + let path = args.value_of("PATH").unwrap(); + let stat = core.run(client.files_stat(&path)).expect(EXPECTED_API); + + println!(""); + println!(" hash : {}", stat.hash); + println!(" size : {}", stat.size); + println!(" cumulative_size : {}", stat.cumulative_size); + println!(" blocks : {}", stat.blocks); + println!(" type : {}", stat.typ); + println!(""); + } + ("write", Some(args)) => { + let dest = args.value_of("DEST").unwrap(); + let path = args.value_of("INPUT").unwrap(); + let file = File::open(path).expect(EXPECTED_FILE); + let req = client.files_write( + &dest, + args.is_present("create"), + args.is_present("truncate"), + file, + ); + + core.run(req).expect(EXPECTED_API); + + println!(""); + println!(" OK"); + println!(""); + } + _ => unreachable!(), + } +} diff --git a/ipfs-cli/src/command/mod.rs b/ipfs-cli/src/command/mod.rs index 208a89f..a14adf1 100644 --- a/ipfs-cli/src/command/mod.rs +++ b/ipfs-cli/src/command/mod.rs @@ -37,4 +37,5 @@ pub mod dht; pub mod diag; pub mod dns; pub mod file; +pub mod files; pub mod version; diff --git a/ipfs-cli/src/main.rs b/ipfs-cli/src/main.rs index 1b485ed..8eb18dc 100644 --- a/ipfs-cli/src/main.rs +++ b/ipfs-cli/src/main.rs @@ -37,6 +37,7 @@ fn main() { (subcommand: command::diag::signature()) (subcommand: command::dns::signature()) (subcommand: command::file::signature()) + (subcommand: command::files::signature()) (subcommand: command::version::signature()) ).get_matches(); @@ -56,6 +57,7 @@ fn main() { ("diag", Some(args)) => command::diag::handle(&mut core, &client, &args), ("dns", Some(args)) => command::dns::handle(&mut core, &client, &args), ("file", Some(args)) => command::file::handle(&mut core, &client, &args), + ("files", Some(args)) => command::files::handle(&mut core, &client, &args), ("version", _) => command::version::handle(&mut core, &client), _ => unreachable!(), } |