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.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/dotup_cli/src/utils.rs b/dotup_cli/src/utils.rs
index fad37ae..d284a22 100644
--- a/dotup_cli/src/utils.rs
+++ b/dotup_cli/src/utils.rs
@@ -1,4 +1,7 @@
1use std::path::{Path, PathBuf}; 1use std::{
2 collections::VecDeque,
3 path::{Path, PathBuf},
4};
2 5
3use crate::prelude::*; 6use crate::prelude::*;
4 7
@@ -123,3 +126,26 @@ pub fn collect_link_ids_by_base_paths(
123 .map(|l| l.id()) 126 .map(|l| l.id())
124 .collect() 127 .collect()
125} 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}