diff options
| author | diogo464 <[email protected]> | 2021-07-09 10:08:13 -0400 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-07-09 10:08:13 -0400 |
| commit | 3c63bf31b0c4f105d155622c84baf91745f5b9c6 (patch) | |
| tree | 7e792aef85448401cf89b0ba168f600832ea2b99 /dotup_cli/src/commands/utils.rs | |
| parent | 21c234735ebf74899ce9a98cb46d9500b3f51e89 (diff) | |
Moved utils module to the root of the crate.
Diffstat (limited to 'dotup_cli/src/commands/utils.rs')
| -rw-r--r-- | dotup_cli/src/commands/utils.rs | 114 |
1 files changed, 0 insertions, 114 deletions
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 @@ | |||
| 1 | use std::path::{Path, PathBuf}; | ||
| 2 | |||
| 3 | use crate::prelude::*; | ||
| 4 | |||
| 5 | const DEFAULT_DEPOT_NAME: &str = "depot.toml"; | ||
| 6 | |||
| 7 | pub fn home_directory() -> anyhow::Result<PathBuf> { | ||
| 8 | match std::env::var("HOME") { | ||
| 9 | Ok(val) => Ok(PathBuf::from(val)), | ||
| 10 | Err(e) => { | ||
| 11 | log::error!("Failed to get home directory from enviornment variable"); | ||
| 12 | Err(e.into()) | ||
| 13 | } | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | pub fn find_archive_path() -> anyhow::Result<PathBuf> { | ||
| 18 | let mut start = PathBuf::new(); | ||
| 19 | while { | ||
| 20 | start.push(DEFAULT_DEPOT_NAME); | ||
| 21 | !start.is_file() | ||
| 22 | } { | ||
| 23 | start.pop(); | ||
| 24 | start.push(".."); | ||
| 25 | } | ||
| 26 | Ok(start.canonicalize()?) | ||
| 27 | } | ||
| 28 | |||
| 29 | pub fn write_archive(path: impl AsRef<Path>, archive: &Archive) -> anyhow::Result<()> { | ||
| 30 | let path = path.as_ref(); | ||
| 31 | log::debug!("Writing archive to {}", path.display()); | ||
| 32 | match dotup::archive_write(path, archive) { | ||
| 33 | Ok(_) => Ok(()), | ||
| 34 | Err(e) => { | ||
| 35 | log::error!( | ||
| 36 | "Failed to write archive to : {}\nError : {}", | ||
| 37 | path.display(), | ||
| 38 | e | ||
| 39 | ); | ||
| 40 | Err(e.into()) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | pub fn write_depot(depot: &Depot) -> anyhow::Result<()> { | ||
| 46 | let write_path = depot.archive_path(); | ||
| 47 | let archive = depot.archive(); | ||
| 48 | match dotup::archive_write(write_path, &archive) { | ||
| 49 | Ok(_) => Ok(()), | ||
| 50 | Err(e) => { | ||
| 51 | log::error!( | ||
| 52 | "Failed to write depot archive to : {}\nError : {}", | ||
| 53 | write_path.display(), | ||
| 54 | e | ||
| 55 | ); | ||
| 56 | Err(e.into()) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub fn read_archive(path: impl AsRef<Path>) -> anyhow::Result<Archive> { | ||
| 62 | let path = path.as_ref(); | ||
| 63 | match dotup::archive_read(path) { | ||
| 64 | Ok(archive) => Ok(archive), | ||
| 65 | Err(e) => { | ||
| 66 | log::error!( | ||
| 67 | "Failed to read archive from : {}\nError : {}", | ||
| 68 | path.display(), | ||
| 69 | e | ||
| 70 | ); | ||
| 71 | Err(e.into()) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | pub fn read_depot(archive_path: impl AsRef<Path>) -> anyhow::Result<Depot> { | ||
| 77 | let archive_path = archive_path.as_ref().to_path_buf(); | ||
| 78 | let archive = read_archive(&archive_path)?; | ||
| 79 | let depot_config = DepotConfig { | ||
| 80 | archive_path, | ||
| 81 | archive, | ||
| 82 | }; | ||
| 83 | let depot = Depot::new(depot_config)?; | ||
| 84 | Ok(depot) | ||
| 85 | } | ||
| 86 | |||
| 87 | pub fn collect_links_by_base_paths( | ||
| 88 | depot: &Depot, | ||
| 89 | paths: impl IntoIterator<Item = impl AsRef<Path>>, | ||
| 90 | ) -> Vec<&Link> { | ||
| 91 | let canonical_paths: Vec<_> = paths | ||
| 92 | .into_iter() | ||
| 93 | .map(|p| p.as_ref().canonicalize().unwrap()) | ||
| 94 | .collect(); | ||
| 95 | |||
| 96 | depot | ||
| 97 | .links() | ||
| 98 | .filter(|&l| { | ||
| 99 | canonical_paths | ||
| 100 | .iter() | ||
| 101 | .any(|p| l.origin_canonical().starts_with(p)) | ||
| 102 | }) | ||
| 103 | .collect() | ||
| 104 | } | ||
| 105 | |||
| 106 | pub fn collect_link_ids_by_base_paths( | ||
| 107 | depot: &Depot, | ||
| 108 | paths: impl IntoIterator<Item = impl AsRef<Path>>, | ||
| 109 | ) -> Vec<LinkID> { | ||
| 110 | collect_links_by_base_paths(depot, paths) | ||
| 111 | .into_iter() | ||
| 112 | .map(|l| l.id()) | ||
| 113 | .collect() | ||
| 114 | } | ||
