aboutsummaryrefslogtreecommitdiff
path: root/dotup_cli/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dotup_cli/src/utils.rs')
-rw-r--r--dotup_cli/src/utils.rs182
1 files changed, 0 insertions, 182 deletions
diff --git a/dotup_cli/src/utils.rs b/dotup_cli/src/utils.rs
deleted file mode 100644
index b9a76a7..0000000
--- a/dotup_cli/src/utils.rs
+++ /dev/null
@@ -1,182 +0,0 @@
1use std::{
2 collections::VecDeque,
3 path::{Path, PathBuf},
4};
5
6use crate::prelude::*;
7
8const DEFAULT_DEPOT_NAME: &str = "depot.toml";
9
10pub fn home_directory() -> anyhow::Result<PathBuf> {
11 match std::env::var("HOME") {
12 Ok(val) => Ok(PathBuf::from(val)),
13 Err(e) => {
14 log::error!("Failed to get home directory from enviornment variable");
15 Err(e.into())
16 }
17 }
18}
19
20pub fn find_archive_path() -> anyhow::Result<PathBuf> {
21 let cwd = std::env::current_dir()?;
22 let compn = cwd.components().count();
23 let mut start = PathBuf::new();
24 for _ in 0..=compn {
25 start.push(DEFAULT_DEPOT_NAME);
26 if dotup::archive_exists(&start) {
27 return Ok(start);
28 }
29 start.pop();
30 start.push("..");
31 }
32 Ok(PathBuf::from(DEFAULT_DEPOT_NAME))
33}
34
35pub fn write_archive(path: impl AsRef<Path>, archive: &Archive) -> anyhow::Result<()> {
36 let path = path.as_ref();
37 log::debug!("Writing archive to {}", path.display());
38 match dotup::archive_write(path, archive) {
39 Ok(_) => Ok(()),
40 Err(e) => {
41 log::error!(
42 "Failed to write archive to : {}\nError : {}",
43 path.display(),
44 e
45 );
46 Err(e.into())
47 }
48 }
49}
50
51pub fn write_depot(depot: &Depot) -> anyhow::Result<()> {
52 let write_path = depot.archive_path();
53 let archive = depot.archive();
54 match dotup::archive_write(write_path, &archive) {
55 Ok(_) => Ok(()),
56 Err(e) => {
57 log::error!(
58 "Failed to write depot archive to : {}\nError : {}",
59 write_path.display(),
60 e
61 );
62 Err(e.into())
63 }
64 }
65}
66
67pub fn read_archive(path: impl AsRef<Path>) -> anyhow::Result<Archive> {
68 let path = path.as_ref();
69 match dotup::archive_read(path) {
70 Ok(archive) => Ok(archive),
71 Err(e) => {
72 log::error!(
73 "Failed to read archive from : {}\nError : {}",
74 path.display(),
75 e
76 );
77 Err(e.into())
78 }
79 }
80}
81
82pub fn read_depot(archive_path: impl AsRef<Path>) -> anyhow::Result<Depot> {
83 let archive_path = archive_path.as_ref().to_path_buf();
84 let archive = read_archive(&archive_path)?;
85 let depot_config = DepotConfig {
86 archive: Default::default(),
87 archive_path,
88 };
89 let mut depot = Depot::new(depot_config)?;
90
91 for archive_link in archive.links {
92 let link_params = LinkCreateParams::from(archive_link);
93 if let Err(e) = depot.create_link(link_params) {
94 log::warn!("Error while adding link : {}", e);
95 }
96 }
97
98 Ok(depot)
99}
100
101pub fn collect_links_by_base_paths(
102 depot: &Depot,
103 paths: impl IntoIterator<Item = impl AsRef<Path>>,
104) -> Vec<&Link> {
105 let canonical_paths: Vec<_> = paths
106 .into_iter()
107 .map(|p| p.as_ref().canonicalize().unwrap())
108 .collect();
109
110 depot
111 .links()
112 .filter(|&l| {
113 canonical_paths
114 .iter()
115 .any(|p| l.origin_canonical().starts_with(p))
116 })
117 .collect()
118}
119
120pub fn collect_link_ids_by_base_paths(
121 depot: &Depot,
122 paths: impl IntoIterator<Item = impl AsRef<Path>>,
123) -> Vec<LinkID> {
124 collect_links_by_base_paths(depot, paths)
125 .into_iter()
126 .map(|l| l.id())
127 .collect()
128}
129
130/// Returns a list of canonical paths to all the files in `dir`. This includes files in
131/// subdirectories.
132/// Fails if dir isnt a directory or if there is some other io error.
133pub fn collect_files_in_dir(dir: impl Into<PathBuf>) -> anyhow::Result<Vec<PathBuf>> {
134 let mut paths = Vec::new();
135 let mut dirs = VecDeque::new();
136 dirs.push_back(dir.into());
137
138 while let Some(dir) = dirs.pop_front() {
139 for entry in std::fs::read_dir(dir)? {
140 let entry = entry?;
141 let filetype = entry.file_type()?;
142 if filetype.is_dir() {
143 dirs.push_back(entry.path());
144 } else {
145 paths.push(entry.path());
146 }
147 }
148 }
149
150 Ok(paths)
151}
152
153/// Collects the result of std::fs::read_dir into two vecs
154/// The first one contains all the directories and the second one all the files
155pub fn collect_read_dir_split(
156 dir: impl AsRef<Path>,
157) -> anyhow::Result<(Vec<PathBuf>, Vec<PathBuf>)> {
158 Ok(std::fs::read_dir(dir)?
159 .filter_map(|e| e.ok())
160 .map(|e| e.path())
161 .partition(|p| p.is_dir()))
162}
163
164/// Checks if `path` is inside a git repository
165pub fn path_is_in_git_repo(path: &Path) -> bool {
166 let mut path = if !path.is_absolute() {
167 dbg!(dotup::utils::weakly_canonical(path))
168 } else {
169 path.to_owned()
170 };
171 let recurse = path.pop();
172 path.push(".git");
173 if path.is_dir() {
174 return true;
175 }
176 if recurse {
177 path.pop();
178 return path_is_in_git_repo(&path);
179 } else {
180 return false;
181 }
182}