diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2023-07-03 21:54:21 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2023-07-03 21:54:21 +0200 |
commit | e18a928db5916fce43c35dff585072dace0da7e0 (patch) | |
tree | d33c8d6c08a779b0f28ddff83ce540b3821160e4 /src/git.rs | |
parent | ff05e4ab91b3b84ffb04b3d8089052112e7bd51c (diff) |
Added a new "--verbose" option to the list command
Diffstat (limited to 'src/git.rs')
-rw-r--r-- | src/git.rs | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -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<Path>) -> Result<u64, ShackleError> { + 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<Vec<RepoMetadata>, ShackleError> { fn add_from_dir( collection_dir: &Path, @@ -163,6 +190,19 @@ pub fn list() -> Result<Vec<RepoMetadata>, ShackleError> { Ok(results) } +pub fn list_verbose() -> Result<Vec<VerboseRepoMetadata>, 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); |