From 7693e678a3ccbcc8ee31770ddfac158e1d4e3038 Mon Sep 17 00:00:00 2001 From: Gabriel Viganotti Date: Tue, 5 Mar 2024 12:06:36 -0300 Subject: [PATCH] feat(cli): init cmd to initialise a directory as root Folder for storing and syncing on/with network --- sn_cli/src/subcommands/acc_packet.rs | 18 ++++++++++++------ sn_cli/src/subcommands/folders.rs | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/sn_cli/src/subcommands/acc_packet.rs b/sn_cli/src/subcommands/acc_packet.rs index e270f0dc57..3ddfeced98 100644 --- a/sn_cli/src/subcommands/acc_packet.rs +++ b/sn_cli/src/subcommands/acc_packet.rs @@ -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 { + /// 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, + ) -> Result { 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. @@ -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 { 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, diff --git a/sn_cli/src/subcommands/folders.rs b/sn_cli/src/subcommands/folders.rs index 004f658232..9f6d8b5363 100644 --- a/sn_cli/src/subcommands/folders.rs +++ b/sn_cli/src/subcommands/folders.rs @@ -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, + }, Upload { /// The location of the file(s) to upload for creating the folder on the network. /// @@ -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")] @@ -93,6 +101,14 @@ 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, @@ -100,7 +116,7 @@ pub(crate) async fn folders_cmds( 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,