From e580030dd91e87c566d361f200b69bedcc89e17e Mon Sep 17 00:00:00 2001 From: Jason Paul Date: Thu, 21 Mar 2024 11:30:20 +0000 Subject: [PATCH] refactor: extracted folders into methods for readability --- sn_cli/src/subcommands/folders.rs | 163 ++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 44 deletions(-) diff --git a/sn_cli/src/subcommands/folders.rs b/sn_cli/src/subcommands/folders.rs index cf3fb9d61e..c2380bbf63 100644 --- a/sn_cli/src/subcommands/folders.rs +++ b/sn_cli/src/subcommands/folders.rs @@ -23,11 +23,12 @@ use std::{ #[derive(Parser, Debug)] pub enum FoldersCmds { Init { - /// The directory to initialise as a root folder, which can then be stored on the network (and kept in sync with). - /// By default the current path is assumed. + /// The directory to initialise as a root folder, which can then be stored on the network + /// (and kept in sync with). By default the current path is assumed. #[clap(name = "path", value_name = "PATH")] path: Option, - /// The hex address where the root Folder will be stored on the network (a random address will be otherwise generated by default). + /// The hex address where the root Folder will be stored on the network (a random address + /// will be otherwise generated by default). #[clap(long = "address")] folder_addr: Option, }, @@ -109,7 +110,11 @@ pub(crate) async fn folders_cmds( folder_addr.and_then(|hex_str| RegisterAddress::from_hex(&hex_str).ok()); let acc_packet = AccountPacket::init(client.clone(), root_dir, &path, root_folder_addr)?; - println!("Directoy at {path:?} initialised as a root Folder, ready to track and sync changes with the network at address: {}", acc_packet.root_folder_addr().to_hex()) + println!( + "Directoy at {path:?} initialised as a root Folder, ready to track and sync \ + changes with the network at address: {}", + acc_packet.root_folder_addr().to_hex() + ) } FoldersCmds::Upload { path, @@ -117,22 +122,16 @@ pub(crate) async fn folders_cmds( make_data_public, retry_strategy, } => { - // initialise path as a fresh new Folder with a random network address - let path = get_path(path, None)?; - let mut acc_packet = AccountPacket::init(client.clone(), root_dir, &path, None)?; - - let options = FilesUploadOptions { - make_data_public, + upload( + client, + root_dir, verify_store, + path, batch_size, + make_data_public, retry_strategy, - }; - acc_packet.sync(options).await?; - - println!( - "\nFolder hierarchy from {path:?} uploaded successfully at {}", - acc_packet.root_folder_addr().to_hex() - ); + ) + .await?; } FoldersCmds::Download { folder_addr, @@ -140,34 +139,18 @@ pub(crate) async fn folders_cmds( batch_size, retry_strategy, } => { - let address = RegisterAddress::from_hex(&folder_addr) - .map_err(|err| eyre!("Failed to parse Folder address: {err}"))?; - - let addr_hex = address.to_hex(); - let folder_name = format!( - "folder_{}_{}", - &addr_hex[..6], - &addr_hex[addr_hex.len() - 6..] - ); - let download_folder_path = get_path(path, Some(&folder_name))?; - println!( - "Downloading onto {download_folder_path:?}, with batch-size {batch_size}, from {addr_hex}" ); - debug!("Downloading onto {download_folder_path:?} from {addr_hex}"); - - let _ = AccountPacket::retrieve_folders( + download( client, root_dir, - address, - &download_folder_path, + &folder_addr, + path, batch_size, retry_strategy, ) .await?; } FoldersCmds::Status { path } => { - let path = get_path(path, None)?; - let acc_packet = AccountPacket::from_path(client.clone(), root_dir, &path)?; - acc_packet.status()?; + status(client, root_dir, path)?; } FoldersCmds::Sync { path, @@ -175,21 +158,113 @@ pub(crate) async fn folders_cmds( make_data_public, retry_strategy, } => { - let path = get_path(path, None)?; - let mut acc_packet = AccountPacket::from_path(client.clone(), root_dir, &path)?; - - let options = FilesUploadOptions { - make_data_public, + sync( + client, + root_dir, verify_store, + path, batch_size, + make_data_public, retry_strategy, - }; - acc_packet.sync(options).await?; + ) + .await?; } } Ok(()) } +async fn sync( + client: &Client, + root_dir: &Path, + verify_store: bool, + path: Option, + batch_size: usize, + make_data_public: bool, + retry_strategy: RetryStrategy, +) -> Result<()> { + let path = get_path(path, None)?; + let mut acc_packet = AccountPacket::from_path(client.clone(), root_dir, &path)?; + + let options = FilesUploadOptions { + make_data_public, + verify_store, + batch_size, + retry_strategy, + }; + acc_packet.sync(options).await?; + Ok(()) +} + +fn status(client: &Client, root_dir: &Path, path: Option) -> Result<()> { + let path = get_path(path, None)?; + let acc_packet = AccountPacket::from_path(client.clone(), root_dir, &path)?; + acc_packet.status()?; + Ok(()) +} + +async fn download( + client: &Client, + root_dir: &Path, + folder_addr: &str, + path: Option, + batch_size: usize, + retry_strategy: RetryStrategy, +) -> Result<()> { + let address = RegisterAddress::from_hex(folder_addr) + .map_err(|err| eyre!("Failed to parse Folder address: {err}"))?; + + let addr_hex = address.to_hex(); + let folder_name = format!( + "folder_{}_{}", + &addr_hex[..6], + &addr_hex[addr_hex.len() - 6..] + ); + let download_folder_path = get_path(path, Some(&folder_name))?; + println!( + "Downloading onto {download_folder_path:?}, with batch-size {batch_size}, from {addr_hex}" + ); + debug!("Downloading onto {download_folder_path:?} from {addr_hex}"); + + let _ = AccountPacket::retrieve_folders( + client, + root_dir, + address, + &download_folder_path, + batch_size, + retry_strategy, + ) + .await?; + Ok(()) +} + +async fn upload( + client: &Client, + root_dir: &Path, + verify_store: bool, + path: Option, + batch_size: usize, + make_data_public: bool, + retry_strategy: RetryStrategy, +) -> Result<()> { + // initialise path as a fresh new Folder with a random network address + let path = get_path(path, None)?; + let mut acc_packet = AccountPacket::init(client.clone(), root_dir, &path, None)?; + + let options = FilesUploadOptions { + make_data_public, + verify_store, + batch_size, + retry_strategy, + }; + acc_packet.sync(options).await?; + + println!( + "\nFolder hierarchy from {path:?} uploaded successfully at {}", + acc_packet.root_folder_addr().to_hex() + ); + Ok(()) +} + // Unwrap provided path, or return the current path if none was provided. // Optionally it can be provided a string to adjoin when the current dir is returned. fn get_path(path: Option, to_join: Option<&str>) -> Result {