From ed0baec0a3f953c99445f6842dadc5566e89cb75 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Thu, 8 Jul 2021 17:11:46 -0400 Subject: Initial commit --- dotup_cli/src/commands/utils.rs | 100 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 dotup_cli/src/commands/utils.rs (limited to 'dotup_cli/src/commands/utils.rs') diff --git a/dotup_cli/src/commands/utils.rs b/dotup_cli/src/commands/utils.rs new file mode 100644 index 0000000..4160078 --- /dev/null +++ b/dotup_cli/src/commands/utils.rs @@ -0,0 +1,100 @@ +use std::path::{Path, PathBuf}; + +use crate::prelude::*; + +pub fn home_directory() -> anyhow::Result { + match std::env::var("HOME") { + Ok(val) => Ok(PathBuf::from(val)), + Err(e) => { + log::error!("Failed to get home directory from enviornment variable"); + Err(e.into()) + } + } +} + +pub fn write_archive(path: impl AsRef, archive: &Archive) -> anyhow::Result<()> { + let path = path.as_ref(); + log::debug!("Writing archive to {}", path.display()); + match dotup::archive_write(path, archive) { + Ok(_) => Ok(()), + Err(e) => { + log::error!( + "Failed to write archive to : {}\nError : {}", + path.display(), + e + ); + Err(e.into()) + } + } +} + +pub fn write_depot(depot: &Depot) -> anyhow::Result<()> { + let write_path = depot.archive_path(); + let archive = depot.archive(); + match dotup::archive_write(write_path, &archive) { + Ok(_) => Ok(()), + Err(e) => { + log::error!( + "Failed to write depot archive to : {}\nError : {}", + write_path.display(), + e + ); + Err(e.into()) + } + } +} + +pub fn read_archive(path: impl AsRef) -> anyhow::Result { + let path = path.as_ref(); + match dotup::archive_read(path) { + Ok(archive) => Ok(archive), + Err(e) => { + log::error!( + "Failed to read archive from : {}\nError : {}", + path.display(), + e + ); + Err(e.into()) + } + } +} + +pub fn read_depot(archive_path: impl AsRef) -> anyhow::Result { + let archive_path = archive_path.as_ref().to_path_buf(); + let archive = read_archive(&archive_path)?; + let depot_config = DepotConfig { + archive_path, + archive, + }; + let depot = Depot::new(depot_config)?; + Ok(depot) +} + +pub fn collect_links_by_base_paths( + depot: &Depot, + paths: impl IntoIterator>, +) -> Vec<&Link> { + let canonical_paths: Vec<_> = paths + .into_iter() + .map(|p| p.as_ref().canonicalize().unwrap()) + .collect(); + + depot + .links() + .filter(|&l| { + canonical_paths + .iter() + .any(|p| l.origin_canonical().starts_with(p)) + }) + .collect() +} + +pub fn collect_link_ids_by_base_paths( + depot: &Depot, + paths: impl IntoIterator>, +) -> Vec { + collect_links_by_base_paths(depot, paths) + .into_iter() + .map(|l| l.id()) + .collect() +} -- cgit