From e18a928db5916fce43c35dff585072dace0da7e0 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Mon, 3 Jul 2023 21:54:21 +0200 Subject: Added a new "--verbose" option to the list command --- src/git.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/git.rs') diff --git a/src/git.rs b/src/git.rs index c07615d..d92b5a0 100644 --- a/src/git.rs +++ b/src/git.rs @@ -109,6 +109,33 @@ pub struct RepoMetadata { pub description: String, } +pub struct VerboseRepoMetadata { + pub path: PathBuf, + pub description: String, + pub size: u64, +} + +fn get_size(path: impl AsRef) -> Result { + let path_metadata = path.as_ref().symlink_metadata()?; + + if path_metadata.is_dir() { + let mut size_in_bytes = path_metadata.len(); + for entry in path.as_ref().read_dir()? { + let entry = entry?; + let entry_metadata = entry.metadata()?; + + if entry_metadata.is_dir() { + size_in_bytes += get_size(entry.path())?; + } else { + size_in_bytes += entry_metadata.len(); + } + } + Ok(size_in_bytes) + } else { + Ok(path_metadata.len()) + } +} + pub fn list() -> Result, ShackleError> { fn add_from_dir( collection_dir: &Path, @@ -163,6 +190,19 @@ pub fn list() -> Result, ShackleError> { Ok(results) } +pub fn list_verbose() -> Result, ShackleError> { + list()? + .into_iter() + .map(|meta| { + get_size(&meta.path).map(|size| VerboseRepoMetadata { + path: meta.path, + description: meta.description, + size, + }) + }) + .collect() +} + pub fn set_description(directory: &Path, description: &str) -> Result<(), ShackleError> { if !is_valid_git_repo_path(directory)? { return Err(ShackleError::InvalidDirectory); -- cgit v1.2.3