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