From 3c63bf31b0c4f105d155622c84baf91745f5b9c6 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 9 Jul 2021 10:08:13 -0400 Subject: Moved utils module to the root of the crate. --- dotup_cli/src/commands/mod.rs | 2 - dotup_cli/src/commands/utils.rs | 114 ---------------------------------------- dotup_cli/src/main.rs | 4 +- dotup_cli/src/utils.rs | 114 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 118 deletions(-) delete mode 100644 dotup_cli/src/commands/utils.rs create mode 100644 dotup_cli/src/utils.rs (limited to 'dotup_cli/src') diff --git a/dotup_cli/src/commands/mod.rs b/dotup_cli/src/commands/mod.rs index f372662..ef7fd0f 100644 --- a/dotup_cli/src/commands/mod.rs +++ b/dotup_cli/src/commands/mod.rs @@ -3,9 +3,7 @@ pub mod install; pub mod link; pub mod uninstall; pub mod unlink; -pub mod utils; mod prelude { - pub use super::utils; pub use crate::prelude::*; } diff --git a/dotup_cli/src/commands/utils.rs b/dotup_cli/src/commands/utils.rs deleted file mode 100644 index 706266e..0000000 --- a/dotup_cli/src/commands/utils.rs +++ /dev/null @@ -1,114 +0,0 @@ -use std::path::{Path, PathBuf}; - -use crate::prelude::*; - -const DEFAULT_DEPOT_NAME: &str = "depot.toml"; - -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 find_archive_path() -> anyhow::Result { - let mut start = PathBuf::new(); - while { - start.push(DEFAULT_DEPOT_NAME); - !start.is_file() - } { - start.pop(); - start.push(".."); - } - Ok(start.canonicalize()?) -} - -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() -} diff --git a/dotup_cli/src/main.rs b/dotup_cli/src/main.rs index 0b4bff1..b4339c9 100644 --- a/dotup_cli/src/main.rs +++ b/dotup_cli/src/main.rs @@ -2,17 +2,17 @@ pub mod commands; pub mod config; +pub mod utils; pub use config::Config; pub mod prelude { - pub use super::Config; + pub use super::{utils, Config}; pub use clap::{AppSettings, Clap}; pub use dotup::{Archive, Depot, DepotConfig, Link, LinkCreateParams, LinkID}; } use clap::{AppSettings, Clap}; -use commands::utils; use flexi_logger::Logger; use std::{ collections::HashMap, diff --git a/dotup_cli/src/utils.rs b/dotup_cli/src/utils.rs new file mode 100644 index 0000000..706266e --- /dev/null +++ b/dotup_cli/src/utils.rs @@ -0,0 +1,114 @@ +use std::path::{Path, PathBuf}; + +use crate::prelude::*; + +const DEFAULT_DEPOT_NAME: &str = "depot.toml"; + +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 find_archive_path() -> anyhow::Result { + let mut start = PathBuf::new(); + while { + start.push(DEFAULT_DEPOT_NAME); + !start.is_file() + } { + start.pop(); + start.push(".."); + } + Ok(start.canonicalize()?) +} + +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