-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcli.rs
More file actions
54 lines (50 loc) · 1.51 KB
/
Copy pathcli.rs
File metadata and controls
54 lines (50 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use anyhow::Result;
use blobs_wasm::BlobsNode;
use clap::Parser;
use iroh_blobs::ticket::BlobTicket;
#[derive(Debug, Parser)]
struct Args {
#[clap(subcommand)]
command: Command,
}
#[derive(Debug, Parser)]
enum Command {
Provide { data: String },
Download { ticket: BlobTicket },
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let args = Args::parse();
let node = BlobsNode::spawn().await?;
match args.command {
Command::Provide { data } => {
let data = data.as_bytes().to_vec();
let ticket = node.import(data.into()).await?;
println!("ticket:");
println!("{ticket}");
println!();
println!("providing... press Ctrl-C to abort");
tokio::signal::ctrl_c().await?;
}
Command::Download { ticket } => {
let hash = ticket.hash();
println!(
"downloading blob {hash} from {}",
ticket.addr().id.fmt_short()
);
node.download(ticket).await?;
println!("download finished");
let size = node.complete_size(hash).await?;
println!("size: {size}");
if size < 1024 * 1024 {
let data = node.blobs.get_bytes(hash).await?;
match std::str::from_utf8(&data) {
Ok(s) => println!("content: {s}"),
Err(_) => println!("(invalid utf-8)"),
}
}
}
}
Ok(())
}