Skip to content

Commit

Permalink
feat(cli): init cmd to initialise a directory as root Folder for stor…
Browse files Browse the repository at this point in the history
…ing and syncing on/with network
  • Loading branch information
bochaco committed Mar 5, 2024
1 parent b4ba121 commit 7693e67
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
18 changes: 12 additions & 6 deletions sn_cli/src/subcommands/acc_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,13 @@ pub struct AccountPacket {
}

impl AccountPacket {
/// Initialise directory as a fresh new packet with new random address for root folder.
pub fn init(client: Client, wallet_dir: &Path, path: &Path) -> Result<Self> {
/// Initialise directory as a fresh new packet with the given/random address for its root Folder.
pub fn init(
client: Client,
wallet_dir: &Path,
path: &Path,
root_folder_addr: Option<RegisterAddress>,
) -> Result<Self> {
let (_, _, meta_dir) = build_tracking_info_paths(path)?;

// if there is already some tracking info we bail out as this is meant ot be a fresh new packet.
Expand All @@ -145,19 +150,20 @@ impl AccountPacket {
}

let mut rng = rand::thread_rng();
let root_folder_addr = RegisterAddress::new(XorName::random(&mut rng), client.signer_pk());
let root_folder_addr = root_folder_addr
.unwrap_or_else(|| RegisterAddress::new(XorName::random(&mut rng), client.signer_pk()));
store_root_folder_tracking_info(&meta_dir, root_folder_addr, false)?;
Self::from_path(client, wallet_dir, path)
}

/// Create AccountPacket instance from a directory which already contains tracking information.
/// Create AccountPacket instance from a directory which has been already initialised.
pub fn from_path(client: Client, wallet_dir: &Path, path: &Path) -> Result<Self> {
let (files_dir, tracking_info_dir, meta_dir) = build_tracking_info_paths(path)?;

// this will fail if there is no tracking information found
// this will fail if the directory was not previously initialised with 'init'.
let curr_metadata = read_tracking_info_from_disk(&meta_dir)?;
let (root_folder_addr,root_folder_created) = read_root_folder_addr(&meta_dir)
.map_err(|_| eyre!("Root Folder address not found, make sure the dir is initialised to be tracked."))?;
.map_err(|_| eyre!("Root Folder address not found, make sure the directory {path:?} is initialised."))?;

Ok(Self {
client,
Expand Down
20 changes: 18 additions & 2 deletions sn_cli/src/subcommands/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ 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).
#[clap(name = "path", value_name = "PATH")]
path: PathBuf,
/// The hex address where the root Folder will be stored on the network (a random address will be otherwise generated by default).
#[clap(name = "address")]
folder_addr: Option<String>,
},
Upload {
/// The location of the file(s) to upload for creating the folder on the network.
///
Expand Down Expand Up @@ -59,7 +67,7 @@ pub enum FoldersCmds {
#[clap(long, default_value_t = RetryStrategy::Quick, short = 'r', help = "Sets the retry strategy on download failure. Options: 'quick' for minimal effort, 'balanced' for moderate effort, or 'persistent' for maximum effort.")]
retry_strategy: RetryStrategy,
},
/// Report any differences found in local files/folders in comparison with their versions stored on the network.
/// Report any changes made to local version of files/folders (this doesn't compare it with their versions stored on the network).
Status {
/// Can be a file or a directory.
#[clap(name = "path", value_name = "PATH")]
Expand Down Expand Up @@ -93,14 +101,22 @@ pub(crate) async fn folders_cmds(
verify_store: bool,
) -> Result<()> {
match cmds {
FoldersCmds::Init { path, folder_addr } => {
// init path as a fresh new folder
let root_folder_addr =
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())
}
FoldersCmds::Upload {
path,
batch_size,
make_data_public,
retry_strategy,
} => {
// init path as a fresh new folder
let mut acc_packet = AccountPacket::init(client.clone(), root_dir, &path)?;
let mut acc_packet = AccountPacket::init(client.clone(), root_dir, &path, None)?;

let options = FilesUploadOptions {
make_data_public,
Expand Down

0 comments on commit 7693e67

Please sign in to comment.