diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-02-22 22:19:57 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-02-22 22:19:57 -0600 |
commit | f4d54562e5c4f7c61faadfe13701286cce1e3d05 (patch) | |
tree | f0056da257964b8169a8e149cf660e3069222dd5 /zone_zfs/src/zfs.rs | |
parent | 1ea68976f452a35b7aa20119c3eeb8ae14de107e (diff) |
refactor(zfs): use builder pattern for FileSystem
Refactor FileSystem to use a builder pattern to create a zfs dataset
Fix #26
Diffstat (limited to 'zone_zfs/src/zfs.rs')
-rw-r--r-- | zone_zfs/src/zfs.rs | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/zone_zfs/src/zfs.rs b/zone_zfs/src/zfs.rs index c820c1c..0ffba13 100644 --- a/zone_zfs/src/zfs.rs +++ b/zone_zfs/src/zfs.rs @@ -20,15 +20,11 @@ impl ZFS { .map(|config| Self { config }) } - pub fn clone_from_latest(&self, name: PathBuf, parent: PathBuf) -> Result<FileSystem> { - Self::get_file_system(parent)? - .get_latest_snapshot()? - .ok_or_else(|| Error::Snapshot("No snapshot found".into()))? - .clone_into_file_system(name)? - .set_quota(self.config.quota) + pub fn clone_from_latest(&self, _name: PathBuf, _parent: PathBuf) -> Result<FileSystem> { + todo!("implement using feature") } - pub(super) fn get_file_systems() -> Result<Vec<FileSystem>> { + pub fn get_file_systems(&self) -> Result<Vec<FileSystem>> { Command::new("zfs") .arg("list") .arg("-H") @@ -36,16 +32,32 @@ impl ZFS { .arg("name") .output() .map(|o| String::from_utf8(o.stdout))? - .map_err(Error::from)? - .split_whitespace() - .map(FileSystem::try_from) - .collect() + .map_err(Error::from) + .map(|s| { + s.lines() + .into_iter() + .filter_map(|l| l.split_once(" ").map(|s| (s.0.trim(), s.1.trim()))) + .filter_map(|(dataset, mountpoint)| { + let mountpoint = if mountpoint == "none" { + PathBuf::from(mountpoint) + } else { + self.config.mountpoint.to_owned() + }; + + FileSystem::builder() + .mountpoint(mountpoint) + .dataset(dataset) + .to_file_system() + .ok() + }) + .collect() + }) } - fn get_file_system(name: PathBuf) -> Result<FileSystem> { - Self::get_file_systems()? + pub fn get_file_system(&self, name: PathBuf) -> Result<FileSystem> { + self.get_file_systems()? .into_iter() - .find(|fs| PathBuf::from(&fs.value).ends_with(&name)) + .find(|fs| fs.dataset().ends_with(&name)) .ok_or_else(|| Error::FileSystem("No file system found".into())) } } @@ -55,6 +67,6 @@ mod tests { #[test] fn zfs_list() { use super::*; - assert!(ZFS::get_file_systems().is_ok()); + assert!(ZFS::new().unwrap().get_file_systems().is_ok()); } } |