diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-05-23 09:00:48 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-05-23 09:00:48 +0200 |
commit | d241f2f9ee448e95066c3a66b6b6221931ce651e (patch) | |
tree | dd85b57d0de51d73b1c02beafa3929eec1d09f47 | |
parent | 63ef549825edb99a7fd7ab15e1acae14286ed8a0 (diff) |
Impl get subcommand
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/cli.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 23 |
2 files changed, 37 insertions, 0 deletions
@@ -43,4 +43,18 @@ pub fn app<'a>() -> App<'a> { .help("Post this TEXT as text/text") ) ) + + .subcommand(App::new("get") + .author(crate_authors!()) + .version(crate_version!()) + .about("Get block") + .arg(Arg::with_name("head") + .index(1) + .multiple(false) + .takes_value(true) + .required(true) + .value_name("HEAD") + .help("Get HEAD block") + ) + ) } diff --git a/src/main.rs b/src/main.rs index 3db85d5..1b97f6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::str::FromStr; +use anyhow::anyhow; use anyhow::Result; use daglib::DagBackend; use rand_os::OsRng; @@ -104,6 +105,28 @@ async fn main() -> Result<()> { Ok(()) }, + ("get", Some(mtch)) => { + let head = mtch + .value_of("head") + .map(cid::Cid::from_str) + .transpose()? + .map(crate::backend::Id::from) + .unwrap(); // Safe by clap + + let (id, node) = backend + .get(head.clone()) + .await? + .ok_or_else(|| anyhow!("Not found: {:?}", head))?; + + let payload = backend.ipfs().fetch(node.payload_id(), backend.ipfs().peers()).await?; + let payload = payload.decode::<libipld::cbor::DagCborCodec, crate::backend::Payload>()?; + + println!("id = {:?}", id); + println!("node = {:?}", node); + println!("payload = {:?}", payload); + Ok(()) + }, + (other, _) => { unimplemented!() }, |