diff options
Diffstat (limited to 'crates/vcs_data')
| -rw-r--r-- | crates/vcs_data/src/data/local.rs | 21 | ||||
| -rw-r--r-- | crates/vcs_data/src/data/vault.rs | 18 | ||||
| -rw-r--r-- | crates/vcs_data/src/data/vault/service.rs | 14 |
3 files changed, 37 insertions, 16 deletions
diff --git a/crates/vcs_data/src/data/local.rs b/crates/vcs_data/src/data/local.rs index c93bd2b..fb43042 100644 --- a/crates/vcs_data/src/data/local.rs +++ b/crates/vcs_data/src/data/local.rs @@ -1,7 +1,7 @@ -use std::{env::current_dir, path::PathBuf}; +use std::{env::current_dir, path::PathBuf, sync::Arc}; use cfg_file::config::ConfigFile; -use tokio::fs; +use tokio::{fs, sync::Mutex}; use crate::{ constants::{CLIENT_FILE_README, CLIENT_FILE_WORKSPACE}, @@ -12,7 +12,7 @@ use crate::{ pub mod config; pub struct LocalWorkspace { - config: LocalConfig, + config: Arc<Mutex<LocalConfig>>, local_path: PathBuf, } @@ -25,13 +25,19 @@ impl LocalWorkspace { /// Initialize local workspace. pub fn init(config: LocalConfig, local_path: impl Into<PathBuf>) -> Option<Self> { let local_path = find_local_path(local_path)?; - Some(Self { config, local_path }) + Some(Self { + config: Arc::new(Mutex::new(config)), + local_path, + }) } /// Initialize local workspace in the current directory. pub fn init_current_dir(config: LocalConfig) -> Option<Self> { let local_path = current_local_path()?; - Some(Self { config, local_path }) + Some(Self { + config: Arc::new(Mutex::new(config)), + local_path, + }) } /// Setup local workspace @@ -92,6 +98,11 @@ Without these credentials, the server will reject all access requests. Ok(()) } + /// Get a reference to the local configuration. + pub fn config(&self) -> Arc<Mutex<LocalConfig>> { + self.config.clone() + } + /// Setup local workspace in current directory pub async fn setup_local_workspace_current_dir() -> Result<(), std::io::Error> { Self::setup_local_workspace(current_dir()?).await?; diff --git a/crates/vcs_data/src/data/vault.rs b/crates/vcs_data/src/data/vault.rs index 80ebe1d..efb4eec 100644 --- a/crates/vcs_data/src/data/vault.rs +++ b/crates/vcs_data/src/data/vault.rs @@ -2,6 +2,7 @@ use std::{ env::current_dir, fs::{self, create_dir_all}, path::PathBuf, + sync::Arc, }; use cfg_file::config::ConfigFile; @@ -22,7 +23,7 @@ pub mod sheets; pub mod virtual_file; pub struct Vault { - config: VaultConfig, + config: Arc<VaultConfig>, vault_path: PathBuf, } @@ -35,13 +36,19 @@ impl Vault { /// Initialize vault pub fn init(config: VaultConfig, vault_path: impl Into<PathBuf>) -> Option<Self> { let vault_path = find_vault_path(vault_path)?; - Some(Self { config, vault_path }) + Some(Self { + config: Arc::new(config), + vault_path, + }) } /// Initialize vault pub fn init_current_dir(config: VaultConfig) -> Option<Self> { let vault_path = current_vault_path()?; - Some(Self { config, vault_path }) + Some(Self { + config: Arc::new(config), + vault_path, + }) } /// Setup vault @@ -144,4 +151,9 @@ Thank you for using `JustEnoughVCS!` Self::setup_vault(current_dir()?).await?; Ok(()) } + + /// Get vault configuration + pub fn config(&self) -> &Arc<VaultConfig> { + &self.config + } } diff --git a/crates/vcs_data/src/data/vault/service.rs b/crates/vcs_data/src/data/vault/service.rs index 22e91d5..3f59c30 100644 --- a/crates/vcs_data/src/data/vault/service.rs +++ b/crates/vcs_data/src/data/vault/service.rs @@ -9,7 +9,7 @@ impl Vault { } /// Check if the current Vault is locked - pub fn is_locked(&self) -> bool { + pub fn is_locked(&self) -> bool { self.lock_file_path().exists() } @@ -19,10 +19,7 @@ impl Vault { return Err(std::io::Error::new( std::io::ErrorKind::AlreadyExists, format!( - "Vault is already locked at {}. \ - To unlock, please stop any running services. \ - If you are certain no services are running, \ - please delete this file", + "Vault is locked! This indicates a service is already running here.\nPlease stop other services or delete the lock file at the vault root directory: {}", self.lock_file_path().display() ), )); @@ -34,9 +31,10 @@ impl Vault { /// Unlock the current Vault pub fn unlock(&self) -> Result<(), std::io::Error> { if let Err(e) = std::fs::remove_file(self.lock_file_path()) - && e.kind() != std::io::ErrorKind::NotFound { - return Err(e); - } + && e.kind() != std::io::ErrorKind::NotFound + { + return Err(e); + } Ok(()) } } |
