From 3e37ff9be9060ed8f26d10eb95c4b70219e0b1e2 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Tue, 4 Sep 2018 17:33:37 +0200 Subject: =?UTF-8?q?Now=20writing=20the=20config=20after=20vault=20scaffold?= =?UTF-8?q?ing=20=E2=80=93=20woops.=20Making=20a=20small=20example=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lockchain-files/examples/create.rs | 75 ++++++++++++++++++++------------------ lockchain-files/src/config.rs | 21 ++++++++--- lockchain-files/src/lib.rs | 10 +++-- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/lockchain-files/examples/create.rs b/lockchain-files/examples/create.rs index e9a4994..5ced78a 100644 --- a/lockchain-files/examples/create.rs +++ b/lockchain-files/examples/create.rs @@ -1,36 +1,39 @@ -// extern crate lockchain_core as lcc; -// extern crate lockchain_files as files; - -// use files::DataVault; -// use lcc::traits::Vault; -// use lcc::users::{User, UserStore}; -// use lcc::{EncryptedBody, Payload, Record}; -// use std::env; - -// fn main() { -// if env::args().len() == 3 { -// let path = env::args().nth(1).unwrap(); -// let name = env::args().nth(2).unwrap(); - -// let mut vault: DataVault = DataVault::new(&name, &path); -// let mut store = match ( -// vault.meta_pull_domain("userstore"), -// vault.meta_pull_domain("registry"), -// ) { -// (Some(users), Some(registry)) => (users.clone(), registry.clone()).into(), -// _ => UserStore::default(), -// }; - -// /* Some users of our vault have the same password :S */ -// store.add(User::register("alice", "password")); -// let token = store.get_token(vec!()); - -// let (users, registry) = store.into(); - -// vault.meta_push_domain(users); -// vault.meta_push_domain(registry); -// vault.sync(); -// } else { -// eprintln!("Usage: create [FLAGS] (there are no flags)") -// } -// } +extern crate lockchain_core as lcc; +extern crate lockchain_files as files; + +use files::DataVault; +use lcc::traits::Vault; +use lcc::users::{User, UserStore}; +use lcc::{EncryptedBody, Payload, Record}; +use std::env; + +fn main() { + if env::args().len() == 3 { + let path = env::args().nth(1).unwrap(); + let name = env::args().nth(2).unwrap(); + + let _vault: DataVault = DataVault::new(&name, &path); + + + + // let mut store = match ( + // vault.meta_pull_domain("userstore"), + // vault.meta_pull_domain("registry"), + // ) { + // (Some(users), Some(registry)) => (users.clone(), registry.clone()).into(), + // _ => UserStore::default(), + // }; + + // /* Some users of our vault have the same password :S */ + // store.add(User::register("alice", "password")); + // let token = store.get_token(vec!()); + + // let (users, registry) = store.into(); + + // vault.meta_push_domain(users); + // vault.meta_push_domain(registry); + // vault.sync(); + } else { + eprintln!("Usage: create [FLAGS] (there are no flags)") + } +} diff --git a/lockchain-files/src/config.rs b/lockchain-files/src/config.rs index b2d311a..ffec582 100644 --- a/lockchain-files/src/config.rs +++ b/lockchain-files/src/config.rs @@ -1,7 +1,11 @@ -use std::error::Error; -use std::fmt; -use std::time::SystemTime; -use std::{fs::File, path::PathBuf, io}; +use std::{ + error::Error, + fmt, + fs::{File, OpenOptions as OO}, + io::{self, Write}, + path::PathBuf, + time::SystemTime, +}; use semver::Version; use toml; @@ -33,7 +37,7 @@ impl fmt::Display for ConfigError { impl Error for ConfigError {} /// The configuration describing a file vault -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] pub struct VaultConfig { /// A semver conforming version string pub version: String, @@ -50,8 +54,13 @@ impl VaultConfig { } } - pub fn save(&self) -> Result<(), io::Error> { + pub fn save(&self, vault: &PathBuf) -> Result<(), io::Error> { + let mut cfg_path = vault.clone(); + cfg_path.push("config.toml"); + let t = toml::to_string(self).unwrap(); + let mut f = OO::new().create(true).write(true).open(cfg_path)?; + f.write_all(t.as_bytes())?; Ok(()) } diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs index 965f669..320714b 100644 --- a/lockchain-files/src/lib.rs +++ b/lockchain-files/src/lib.rs @@ -7,7 +7,7 @@ //! This is great if a vault needs to be easily syncable //! or indexable by another tool. //! No clear-text secrets are ever written to disk. -//! But can sometimes be held in memory cache +//! But might be held in memory cache //! for a period of time. //! //! This backend is comparibly slow @@ -85,6 +85,7 @@ pub use config::{VaultConfig, ConfigError}; #[derive(Debug)] pub struct DataVault { meta_info: (String, String), + config: VaultConfig, records: HashMap>, metadata: HashMap, fs: Filesystem, @@ -94,15 +95,16 @@ impl DataVault { /// Small utility function to setup file structure fn initialize(self) -> Self { self.fs.scaffold(); + self.config.save(&self.fs.root).unwrap(); self } fn load(mut self) -> Option> { - let config = match VaultConfig::load(&self.fs.root) { + self.config = match VaultConfig::load(&self.fs.root) { Ok(cfg) => cfg, _ => return None, }; - + Some(Box::new(self)) } } @@ -114,6 +116,7 @@ impl Vault for DataVault { Self { meta_info: (name.into(), location.into()), records: HashMap::new(), + config: VaultConfig::new(), metadata: HashMap::new(), fs: Filesystem::new(location, name), }.initialize() @@ -128,6 +131,7 @@ impl Vault for DataVault { Self { meta_info: (name.into(), location.into()), records: HashMap::new(), + config: VaultConfig::new(), metadata: HashMap::new(), fs: Filesystem::new(location, name), }.load() -- cgit v1.2.3