1
- use std:: {
2
- collections:: { BTreeMap , HashMap } ,
3
- net:: SocketAddr ,
4
- path:: PathBuf ,
5
- time:: Duration ,
6
- } ;
1
+ //! Define blob-related commands.
7
2
8
3
use anyhow:: { anyhow, bail, ensure, Context , Result } ;
9
4
use clap:: Subcommand ;
@@ -34,8 +29,15 @@ use iroh::{
34
29
} ,
35
30
net:: { key:: PublicKey , relay:: RelayUrl , NodeAddr } ,
36
31
} ;
32
+ use std:: {
33
+ collections:: { BTreeMap , HashMap } ,
34
+ net:: SocketAddr ,
35
+ path:: PathBuf ,
36
+ time:: Duration ,
37
+ } ;
37
38
use tokio:: io:: AsyncWriteExt ;
38
39
40
+ /// Subcommands for the blob command.
39
41
#[ allow( clippy:: large_enum_variant) ]
40
42
#[ derive( Subcommand , Debug , Clone ) ]
41
43
pub enum BlobCommands {
@@ -160,6 +162,7 @@ pub enum BlobCommands {
160
162
} ,
161
163
}
162
164
165
+ /// Possible outcomes of an input.
163
166
#[ derive( Debug , Clone , derive_more:: Display ) ]
164
167
pub enum TicketOrHash {
165
168
Ticket ( BlobTicket ) ,
@@ -181,6 +184,7 @@ impl std::str::FromStr for TicketOrHash {
181
184
}
182
185
183
186
impl BlobCommands {
187
+ /// Runs the blob command given the iroh client.
184
188
pub async fn run ( self , iroh : & Iroh ) -> Result < ( ) > {
185
189
match self {
186
190
Self :: Get {
@@ -437,6 +441,7 @@ pub struct BlobAddOptions {
437
441
pub no_ticket : bool ,
438
442
}
439
443
444
+ /// Possible list subcommands.
440
445
#[ derive( Subcommand , Debug , Clone ) ]
441
446
pub enum ListCommands {
442
447
/// List the available blobs on the running provider.
@@ -448,6 +453,7 @@ pub enum ListCommands {
448
453
}
449
454
450
455
impl ListCommands {
456
+ /// Runs a list subcommand.
451
457
pub async fn run ( self , iroh : & Iroh ) -> Result < ( ) > {
452
458
match self {
453
459
Self :: Blobs => {
@@ -494,6 +500,7 @@ impl ListCommands {
494
500
}
495
501
}
496
502
503
+ /// Possible delete subcommands.
497
504
#[ derive( Subcommand , Debug , Clone ) ]
498
505
pub enum DeleteCommands {
499
506
/// Delete the given blobs
@@ -505,6 +512,7 @@ pub enum DeleteCommands {
505
512
}
506
513
507
514
impl DeleteCommands {
515
+ /// Runs the delete command.
508
516
pub async fn run ( self , iroh : & Iroh ) -> Result < ( ) > {
509
517
match self {
510
518
Self :: Blob { hash } => {
@@ -518,6 +526,7 @@ impl DeleteCommands {
518
526
}
519
527
}
520
528
529
+ /// Returns the corresponding [`ReportLevel`] given the verbosity level.
521
530
fn get_report_level ( verbose : u8 ) -> ReportLevel {
522
531
match verbose {
523
532
0 => ReportLevel :: Warn ,
@@ -526,6 +535,7 @@ fn get_report_level(verbose: u8) -> ReportLevel {
526
535
}
527
536
}
528
537
538
+ /// Applies the report level to the given text.
529
539
fn apply_report_level ( text : String , level : ReportLevel ) -> console:: StyledObject < String > {
530
540
match level {
531
541
ReportLevel :: Trace => style ( text) . dim ( ) ,
@@ -535,6 +545,7 @@ fn apply_report_level(text: String, level: ReportLevel) -> console::StyledObject
535
545
}
536
546
}
537
547
548
+ /// Checks the consistency of the blobs on the running node, and repairs inconsistencies if instructed.
538
549
pub async fn consistency_check ( iroh : & Iroh , verbose : u8 , repair : bool ) -> Result < ( ) > {
539
550
let mut response = iroh. blobs ( ) . consistency_check ( repair) . await ?;
540
551
let verbosity = get_report_level ( verbose) ;
@@ -576,6 +587,7 @@ pub async fn consistency_check(iroh: &Iroh, verbose: u8, repair: bool) -> Result
576
587
Ok ( ( ) )
577
588
}
578
589
590
+ /// Checks the validity of the blobs on the running node, and repairs anything invalid if instructed.
579
591
pub async fn validate ( iroh : & Iroh , verbose : u8 , repair : bool ) -> Result < ( ) > {
580
592
let mut state = ValidateProgressState :: new ( ) ;
581
593
let mut response = iroh. blobs ( ) . validate ( repair) . await ?;
@@ -661,6 +673,7 @@ pub async fn validate(iroh: &Iroh, verbose: u8, repair: bool) -> Result<()> {
661
673
Ok ( ( ) )
662
674
}
663
675
676
+ /// Collection of all the validation progress state.
664
677
struct ValidateProgressState {
665
678
mp : MultiProgress ,
666
679
pbs : HashMap < u64 , ProgressBar > ,
@@ -671,6 +684,7 @@ struct ValidateProgressState {
671
684
}
672
685
673
686
impl ValidateProgressState {
687
+ /// Creates a new validation progress state collection.
674
688
fn new ( ) -> Self {
675
689
let mp = MultiProgress :: new ( ) ;
676
690
let overall = mp. add ( ProgressBar :: new ( 0 ) ) ;
@@ -685,6 +699,7 @@ impl ValidateProgressState {
685
699
}
686
700
}
687
701
702
+ /// Sets the total number to the provided value and style the progress bar to starting.
688
703
fn starting ( & mut self , total : u64 ) {
689
704
self . total = total;
690
705
self . errors = 0 ;
@@ -699,6 +714,7 @@ impl ValidateProgressState {
699
714
) ;
700
715
}
701
716
717
+ /// Adds a message to the progress bar in the given `id`.
702
718
fn add_entry ( & mut self , id : u64 , hash : Hash , path : Option < String > , size : u64 ) {
703
719
let pb = self . mp . insert_before ( & self . overall , ProgressBar :: new ( size) ) ;
704
720
pb. set_style ( ProgressStyle :: default_bar ( )
@@ -716,18 +732,21 @@ impl ValidateProgressState {
716
732
self . pbs . insert ( id, pb) ;
717
733
}
718
734
735
+ /// Progresses the progress bar with `id` by `progress` amount.
719
736
fn progress ( & mut self , id : u64 , progress : u64 ) {
720
737
if let Some ( pb) = self . pbs . get_mut ( & id) {
721
738
pb. set_position ( progress) ;
722
739
}
723
740
}
724
741
742
+ /// Set an error in the progress bar. Consumes the [`ValidateProgressState`].
725
743
fn abort ( self , error : String ) {
726
744
let error_line = self . mp . add ( ProgressBar :: new ( 0 ) ) ;
727
745
error_line. set_style ( ProgressStyle :: default_bar ( ) . template ( "{msg}" ) . unwrap ( ) ) ;
728
746
error_line. set_message ( error) ;
729
747
}
730
748
749
+ /// Finishes a progress bar with a given error message.
731
750
fn done ( & mut self , id : u64 , error : Option < String > ) {
732
751
if let Some ( pb) = self . pbs . remove ( & id) {
733
752
let ok_char = style ( Emoji ( "✔" , "OK" ) ) . green ( ) ;
@@ -796,6 +815,7 @@ pub enum TicketOption {
796
815
Print ,
797
816
}
798
817
818
+ /// Adds a [`BlobSource`] given some [`BlobAddOptions`].
799
819
pub async fn add_with_opts (
800
820
client : & iroh:: client:: Iroh ,
801
821
source : BlobSource ,
@@ -828,7 +848,7 @@ pub async fn add_with_opts(
828
848
add ( client, source, tag, ticket, wrap) . await
829
849
}
830
850
831
- /// Add data to iroh, either from a path or, if path is `None`, from STDIN.
851
+ /// Adds data to iroh, either from a path or, if path is `None`, from STDIN.
832
852
pub async fn add (
833
853
client : & iroh:: client:: Iroh ,
834
854
source : BlobSourceIroh ,
@@ -877,13 +897,15 @@ pub async fn add(
877
897
Ok ( ( ) )
878
898
}
879
899
900
+ /// Entry with a given name, size, and hash.
880
901
#[ derive( Debug ) ]
881
902
pub struct ProvideResponseEntry {
882
903
pub name : String ,
883
904
pub size : u64 ,
884
905
pub hash : Hash ,
885
906
}
886
907
908
+ /// Combines the [`AddProgress`] outputs from a [`Stream`] into a single tuple.
887
909
pub async fn aggregate_add_response (
888
910
mut stream : impl Stream < Item = Result < AddProgress > > + Unpin ,
889
911
) -> Result < ( Hash , BlobFormat , Vec < ProvideResponseEntry > ) > {
@@ -947,6 +969,7 @@ pub async fn aggregate_add_response(
947
969
Ok ( ( hash, format, entries) )
948
970
}
949
971
972
+ /// Prints out the add response.
950
973
pub fn print_add_response ( hash : Hash , format : BlobFormat , entries : Vec < ProvideResponseEntry > ) {
951
974
let mut total_size = 0 ;
952
975
for ProvideResponseEntry { name, size, hash } in entries {
@@ -961,20 +984,23 @@ pub fn print_add_response(hash: Hash, format: BlobFormat, entries: Vec<ProvideRe
961
984
}
962
985
}
963
986
987
+ /// Progress state for providing.
964
988
#[ derive( Debug ) ]
965
989
pub struct ProvideProgressState {
966
990
mp : MultiProgress ,
967
991
pbs : HashMap < u64 , ProgressBar > ,
968
992
}
969
993
970
994
impl ProvideProgressState {
995
+ /// Creates a new provide progress state.
971
996
fn new ( ) -> Self {
972
997
Self {
973
998
mp : MultiProgress :: new ( ) ,
974
999
pbs : HashMap :: new ( ) ,
975
1000
}
976
1001
}
977
1002
1003
+ /// Inserts a new progress bar with the given id, name, and size.
978
1004
fn found ( & mut self , name : String , id : u64 , size : u64 ) {
979
1005
let pb = self . mp . add ( ProgressBar :: new ( size) ) ;
980
1006
pb. set_style ( ProgressStyle :: default_bar ( )
@@ -987,28 +1013,33 @@ impl ProvideProgressState {
987
1013
self . pbs . insert ( id, pb) ;
988
1014
}
989
1015
1016
+ /// Adds some progress to the progress bar with the given id.
990
1017
fn progress ( & mut self , id : u64 , progress : u64 ) {
991
1018
if let Some ( pb) = self . pbs . get_mut ( & id) {
992
1019
pb. set_position ( progress) ;
993
1020
}
994
1021
}
995
1022
1023
+ /// Sets the multiprogress bar with the given id as finished and clear it.
996
1024
fn done ( & mut self , id : u64 , _hash : Hash ) {
997
1025
if let Some ( pb) = self . pbs . remove ( & id) {
998
1026
pb. finish_and_clear ( ) ;
999
1027
self . mp . remove ( & pb) ;
1000
1028
}
1001
1029
}
1002
1030
1031
+ /// Sets the multiprogress bar as finished and clear them.
1003
1032
fn all_done ( self ) {
1004
1033
self . mp . clear ( ) . ok ( ) ;
1005
1034
}
1006
1035
1036
+ /// Clears the multiprogress bar.
1007
1037
fn error ( self ) {
1008
1038
self . mp . clear ( ) . ok ( ) ;
1009
1039
}
1010
1040
}
1011
1041
1042
+ /// Displays the download progress for a given stream.
1012
1043
pub async fn show_download_progress (
1013
1044
hash : Hash ,
1014
1045
mut stream : impl Stream < Item = Result < DownloadProgress > > + Unpin ,
@@ -1124,6 +1155,7 @@ impl From<String> for OutputTarget {
1124
1155
}
1125
1156
}
1126
1157
1158
+ /// Creates a [`ProgressBar`] with some defaults for the overall progress.
1127
1159
fn make_overall_progress ( ) -> ProgressBar {
1128
1160
let pb = ProgressBar :: hidden ( ) ;
1129
1161
pb. enable_steady_tick ( std:: time:: Duration :: from_millis ( 100 ) ) ;
@@ -1137,6 +1169,7 @@ fn make_overall_progress() -> ProgressBar {
1137
1169
pb
1138
1170
}
1139
1171
1172
+ /// Creates a [`ProgressBar`] with some defaults for the individual progress.
1140
1173
fn make_individual_progress ( ) -> ProgressBar {
1141
1174
let pb = ProgressBar :: hidden ( ) ;
1142
1175
pb. enable_steady_tick ( std:: time:: Duration :: from_millis ( 100 ) ) ;
0 commit comments