1
1
use std:: path:: PathBuf ;
2
2
3
- use anyhow:: { bail , Result } ;
3
+ use anyhow:: Result ;
4
4
use iroh:: { protocol:: Router , Endpoint } ;
5
5
use iroh_blobs:: {
6
6
net_protocol:: Blobs ,
7
- rpc:: client:: blobs:: { self , WrapOption } ,
8
- store:: ExportMode ,
7
+ rpc:: client:: blobs:: WrapOption ,
8
+ store:: { ExportFormat , ExportMode } ,
9
9
ticket:: BlobTicket ,
10
10
util:: SetTagOption ,
11
11
} ;
@@ -26,24 +26,75 @@ async fn main() -> Result<()> {
26
26
. await ?;
27
27
28
28
// 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
+ ) ;
38
56
39
57
tokio:: signal:: ctrl_c ( ) . await ?;
40
58
}
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." ) ;
43
88
}
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]" ) ;
47
98
}
48
99
}
49
100
@@ -53,70 +104,3 @@ async fn main() -> Result<()> {
53
104
54
105
Ok ( ( ) )
55
106
}
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