diff options
Diffstat (limited to 'zone_zfs/src/lib.rs')
-rw-r--r-- | zone_zfs/src/lib.rs | 105 |
1 files changed, 10 insertions, 95 deletions
diff --git a/zone_zfs/src/lib.rs b/zone_zfs/src/lib.rs index c430cba..7c50861 100644 --- a/zone_zfs/src/lib.rs +++ b/zone_zfs/src/lib.rs @@ -1,104 +1,19 @@ -use self::file_system::FileSystem; -use figment::{ - providers::{Env, Format, Serialized, Toml}, - Figment, Metadata, Profile, Provider, -}; -use serde::{Deserialize, Serialize}; -use std::{io, path::PathBuf, result}; +pub use zfs::ZFS; +pub use error::{Error, Result}; +pub use file_system::FileSystem; +pub use config::Config; +pub mod config; +pub mod error; pub mod file_system; - pub mod snapshot; - -type Result<T> = result::Result<T, Error>; - -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("ZFS error")] - ZFS(String), - - #[error("Snapshot Error: {0:?}")] - Snapshot(String), - - #[error("File System Error: {0:?}")] - FileSystem(String), - - #[error("IO Error: Failed to run command")] - IO(#[from] io::Error), -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct Config { - pub quota: String, -} - -impl Default for Config { - fn default() -> Self { - Config { - quota: "16G".to_string(), - } - } -} - -impl Config { - pub fn from<T: Provider>(provider: T) -> result::Result<Config, figment::Error> { - Figment::from(provider).extract() - } - - pub fn figment() -> Figment { - Figment::from(Config::default()) - .merge(Toml::file(Env::var_or("ZFS_CONFIG", "ZFS.toml")).nested()) - .merge(Env::prefixed("ZFS_").global()) - } -} - -impl Provider for Config { - fn metadata(&self) -> Metadata { - Metadata::named("ZFS Config") - } - - fn data( - &self, - ) -> result::Result<figment::value::Map<Profile, figment::value::Dict>, figment::Error> { - Serialized::defaults(Config::default()).data() - } -} - -pub fn create_file_system( - base_fs_name: String, - name: String, - config: &Config, -) -> Result<FileSystem> { - let fs = FileSystem::get_file_systems()? - .into_iter() - .find_map(|fs| match fs.get_name() { - Ok(n) if n == base_fs_name => Some(fs), - _ => None, - }) - .ok_or_else(|| Error::FileSystem("No ".to_string()))?; - - let snapshot = fs - .get_latest_snapshot()? - .ok_or_else(|| Error::Snapshot("No snapshot found".to_string()))?; - - let mut mountpoint = fs.value; - mountpoint.push(name); - - let cloned_fs = snapshot.clone_into_file_system( - snapshot.file_system.get_name()?, - Some(PathBuf::from(mountpoint)), - )?; - - cloned_fs.set_quota(&config.quota)?; - - Ok(cloned_fs) -} +pub mod zfs; #[cfg(test)] mod tests { #[test] - fn zfs_list() { - use super::*; - assert!(FileSystem::get_file_systems().is_ok()); + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); } } |