Skip to content

Commit

Permalink
refactor: extracted folders into methods for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonPaulGithub committed Mar 21, 2024
1 parent 882c0a2 commit e580030
Showing 1 changed file with 119 additions and 44 deletions.
163 changes: 119 additions & 44 deletions sn_cli/src/subcommands/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// 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<String>,
},
Expand Down Expand Up @@ -109,87 +110,161 @@ 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,
batch_size,
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,
path,
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,
batch_size,
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<PathBuf>,
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<PathBuf>) -> 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<PathBuf>,
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<PathBuf>,
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<PathBuf>, to_join: Option<&str>) -> Result<PathBuf> {
Expand Down

0 comments on commit e580030

Please sign in to comment.