Skip to content

Commit b3602a0

Browse files
committed
Undo extracting into functions
1 parent 5a92c08 commit b3602a0

File tree

1 file changed

+68
-84
lines changed

1 file changed

+68
-84
lines changed

examples/transfer.rs

Lines changed: 68 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::path::PathBuf;
22

3-
use anyhow::{bail, Result};
3+
use anyhow::Result;
44
use iroh::{protocol::Router, Endpoint};
55
use iroh_blobs::{
66
net_protocol::Blobs,
7-
rpc::client::blobs::{self, WrapOption},
8-
store::ExportMode,
7+
rpc::client::blobs::WrapOption,
8+
store::{ExportFormat, ExportMode},
99
ticket::BlobTicket,
1010
util::SetTagOption,
1111
};
@@ -26,24 +26,75 @@ async fn main() -> Result<()> {
2626
.await?;
2727

2828
// Grab all passed in arguments, the first one is the binary itself, so we skip it.
29-
let args: Vec<_> = std::env::args().skip(1).collect();
30-
if args.len() < 2 {
31-
print_usage();
32-
bail!("too few arguments");
33-
}
34-
35-
match &*args[0] {
36-
"send" => {
37-
send(&router, blobs.client(), &args).await?;
29+
let args: Vec<String> = std::env::args().skip(1).collect();
30+
// Convert to &str, so we can pattern-match easily:
31+
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
32+
33+
match arg_refs.as_slice() {
34+
["send", filename] => {
35+
let filename: PathBuf = filename.parse()?;
36+
let abs_path = std::path::absolute(&filename)?;
37+
38+
println!("Analyzing file.");
39+
40+
// keep the file in place and link it, instead of copying it into the in-memory blobs database
41+
let in_place = true;
42+
let blob = blobs
43+
.client()
44+
.add_from_path(abs_path, in_place, SetTagOption::Auto, WrapOption::NoWrap)
45+
.await?
46+
.await?;
47+
48+
let node_id = router.endpoint().node_id();
49+
let ticket = BlobTicket::new(node_id.into(), blob.hash, blob.format)?;
50+
51+
println!("File analyzed. Fetch this file by running:");
52+
println!(
53+
"cargo run --example transfer -- receive {ticket} {}",
54+
filename.display()
55+
);
3856

3957
tokio::signal::ctrl_c().await?;
4058
}
41-
"receive" => {
42-
receive(blobs.client(), &args).await?;
59+
["receive", ticket, filename] => {
60+
let filename: PathBuf = filename.parse()?;
61+
let abs_path = std::path::absolute(filename)?;
62+
let ticket: BlobTicket = ticket.parse()?;
63+
64+
println!("Starting download.");
65+
66+
blobs
67+
.client()
68+
.download(ticket.hash(), ticket.node_addr().clone())
69+
.await?
70+
.await?;
71+
72+
println!("Finished download.");
73+
println!("Copying to destination.");
74+
75+
blobs
76+
.client()
77+
.export(
78+
ticket.hash(),
79+
abs_path,
80+
ExportFormat::Blob,
81+
ExportMode::Copy,
82+
)
83+
.await?
84+
.finish()
85+
.await?;
86+
87+
println!("Finished copying.");
4388
}
44-
cmd => {
45-
print_usage();
46-
bail!("unknown command {}", cmd);
89+
_ => {
90+
println!("Couldn't parse command line arguments: {args:?}");
91+
println!("Usage:");
92+
println!(" # to send:");
93+
println!(" cargo run --example transfer -- send [FILE]");
94+
println!(" # this will print a ticket.");
95+
println!();
96+
println!(" # to receive:");
97+
println!(" cargo run --example transfer -- receive [TICKET] [FILE]");
4798
}
4899
}
49100

@@ -53,70 +104,3 @@ async fn main() -> Result<()> {
53104

54105
Ok(())
55106
}
56-
57-
async fn send(router: &Router, blobs: &blobs::MemClient, args: &[String]) -> Result<()> {
58-
let path: PathBuf = args[1].parse()?;
59-
let abs_path = path.canonicalize()?;
60-
61-
println!("Analyzing file.");
62-
63-
// keep the file in place, and link it
64-
let in_place = true;
65-
let blob = blobs
66-
.add_from_path(abs_path, in_place, SetTagOption::Auto, WrapOption::NoWrap)
67-
.await?
68-
.await?;
69-
70-
let node_id = router.endpoint().node_id();
71-
let ticket = BlobTicket::new(node_id.into(), blob.hash, blob.format)?;
72-
73-
println!("File analyzed. Fetch this file by running:");
74-
println!(
75-
"cargo run --example transfer -- receive {ticket} {}",
76-
path.display()
77-
);
78-
Ok(())
79-
}
80-
81-
async fn receive(blobs: &blobs::MemClient, args: &[String]) -> Result<()> {
82-
if args.len() < 3 {
83-
print_usage();
84-
bail!("too few arguments");
85-
}
86-
let path_buf: PathBuf = args[1].parse()?;
87-
let ticket: BlobTicket = args[2].parse()?;
88-
89-
println!("Starting download.");
90-
91-
blobs
92-
.download(ticket.hash(), ticket.node_addr().clone())
93-
.await?
94-
.await?;
95-
96-
println!("Finished download.");
97-
println!("Copying to destination.");
98-
99-
blobs
100-
.export(
101-
ticket.hash(),
102-
path_buf,
103-
ticket.format().into(),
104-
ExportMode::Copy,
105-
)
106-
.await?;
107-
108-
println!("Finished copying.");
109-
110-
Ok(())
111-
}
112-
113-
fn print_usage() {
114-
println!("Couldn't parse command line arguments.");
115-
println!("Usage:");
116-
println!(" # to send:");
117-
println!(" cargo run --example transfer -- send [FILE]");
118-
println!(" # this will print a ticket.");
119-
println!();
120-
println!(" # to receive:");
121-
println!(" cargo run --example transfer -- receive [TICKET] [FILE]");
122-
}

0 commit comments

Comments
 (0)