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.rs114
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 @@
1use std::path::{Path, PathBuf};
2
3use crate::prelude::*;
4
5const DEFAULT_DEPOT_NAME: &str = "depot.toml";
6
7pub 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
17pub 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
29pub 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
45pub 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
61pub 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
76pub 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
87pub 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
106pub 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}