summaryrefslogtreecommitdiff
path: root/crates/vcs_data/src/data/vault.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-12-01 21:40:40 +0800
committer魏曹先生 <1992414357@qq.com>2025-12-01 21:40:40 +0800
commit2965ca7dac27c1023a2880e5496fb45a78264d3c (patch)
treecb83e955ba86c2e78d165c4e9f26db5cd0f0183f /crates/vcs_data/src/data/vault.rs
parent209d20973dbc59970bae164d0d8ba3db9760e8c7 (diff)
Refactor vault config to use template-based generation
- Replace direct serialization with template-based config file generation - Add new dependencies: whoami, redox_syscall, wasite, web-sys - Update VaultConfig structure with new enum types and serialization attributes - Modify setup_vault to accept vault_name parameter and use template - Update all test calls to include vault_name parameter
Diffstat (limited to 'crates/vcs_data/src/data/vault.rs')
-rw-r--r--crates/vcs_data/src/data/vault.rs48
1 files changed, 33 insertions, 15 deletions
diff --git a/crates/vcs_data/src/data/vault.rs b/crates/vcs_data/src/data/vault.rs
index 7cbb459..5c71d93 100644
--- a/crates/vcs_data/src/data/vault.rs
+++ b/crates/vcs_data/src/data/vault.rs
@@ -1,11 +1,7 @@
-use std::{
- env::current_dir,
- fs::{self, create_dir_all},
- path::PathBuf,
- sync::Arc,
-};
+use std::{env::current_dir, path::PathBuf, sync::Arc};
use cfg_file::config::ConfigFile;
+use tokio::fs::create_dir_all;
use vcs_docs::docs::READMES_VAULT_README;
use crate::{
@@ -53,7 +49,10 @@ impl Vault {
}
/// Setup vault
- pub async fn setup_vault(vault_path: impl Into<PathBuf>) -> Result<(), std::io::Error> {
+ pub async fn setup_vault(
+ vault_path: impl Into<PathBuf>,
+ vault_name: impl AsRef<str>,
+ ) -> Result<(), std::io::Error> {
let vault_path: PathBuf = vault_path.into();
// Ensure directory is empty
@@ -66,19 +65,36 @@ impl Vault {
// 1. Setup main config
let config = VaultConfig::default();
- VaultConfig::write_to(&config, vault_path.join(SERVER_FILE_VAULT)).await?;
+
+ // NOTE:
+ // Do not use the write_to method provided by the ConfigFile trait to store the Vault configuration file
+ // Instead, use the PROFILES_VAULT content provided by the Documents Repository for writing
+
+ // VaultConfig::write_to(&config, vault_path.join(SERVER_FILE_VAULT)).await?;
+ let config_content = vcs_docs::docs::PROFILES_VAULT
+ .replace("{vault_name}", vault_name.as_ref())
+ .replace("{user_name}", whoami::username().as_str())
+ .replace(
+ "{date_format}",
+ chrono::Local::now()
+ .format("%Y-%m-%d %H:%M")
+ .to_string()
+ .as_str(),
+ )
+ .replace("{vault_uuid}", &config.vault_uuid().to_string());
+ tokio::fs::write(vault_path.join(SERVER_FILE_VAULT), config_content).await?;
// 2. Setup sheets directory
- create_dir_all(vault_path.join(SERVER_PATH_SHEETS))?;
+ create_dir_all(vault_path.join(SERVER_PATH_SHEETS)).await?;
// 3. Setup key directory
- create_dir_all(vault_path.join(SERVER_PATH_MEMBER_PUB))?;
+ create_dir_all(vault_path.join(SERVER_PATH_MEMBER_PUB)).await?;
// 4. Setup member directory
- create_dir_all(vault_path.join(SERVER_PATH_MEMBERS))?;
+ create_dir_all(vault_path.join(SERVER_PATH_MEMBERS)).await?;
// 5. Setup storage directory
- create_dir_all(vault_path.join(SERVER_PATH_VF_ROOT))?;
+ create_dir_all(vault_path.join(SERVER_PATH_VF_ROOT)).await?;
let Some(vault) = Vault::init(config, &vault_path) else {
return Err(std::io::Error::other("Failed to initialize vault"));
@@ -96,14 +112,16 @@ impl Vault {
// Final, generate README.md
let readme_content = READMES_VAULT_README;
- fs::write(vault_path.join(SERVER_FILE_README), readme_content)?;
+ tokio::fs::write(vault_path.join(SERVER_FILE_README), readme_content).await?;
Ok(())
}
/// Setup vault in current directory
- pub async fn setup_vault_current_dir() -> Result<(), std::io::Error> {
- Self::setup_vault(current_dir()?).await?;
+ pub async fn setup_vault_current_dir(
+ vault_name: impl AsRef<str>,
+ ) -> Result<(), std::io::Error> {
+ Self::setup_vault(current_dir()?, vault_name).await?;
Ok(())
}