aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-09-04 17:33:37 +0200
committerKatharina Fey <kookie@spacekookie.de>2018-09-04 17:33:37 +0200
commit3e37ff9be9060ed8f26d10eb95c4b70219e0b1e2 (patch)
tree4e25a20836b04f2340f5ba2e8f35dd5e29e320d0
parent0b3e6767864d3c1c7d698d3d4f383de0ae4084b6 (diff)
Now writing the config after vault scaffolding – woops. Making a small example work
-rw-r--r--lockchain-files/examples/create.rs75
-rw-r--r--lockchain-files/src/config.rs21
-rw-r--r--lockchain-files/src/lib.rs10
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<EncryptedBody> = 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 <path> <name> [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<EncryptedBody> = 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 <path> <name> [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<T: Body> {
meta_info: (String, String),
+ config: VaultConfig,
records: HashMap<String, Record<T>>,
metadata: HashMap<String, MetaDomain>,
fs: Filesystem,
@@ -94,15 +95,16 @@ impl<T: Body> DataVault<T> {
/// 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<Box<Self>> {
- 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<T: Body> Vault<T> for DataVault<T> {
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<T: Body> Vault<T> for DataVault<T> {
Self {
meta_info: (name.into(), location.into()),
records: HashMap::new(),
+ config: VaultConfig::new(),
metadata: HashMap::new(),
fs: Filesystem::new(location, name),
}.load()