ls: Add option --recursive

This commit is contained in:
Alexander Weiss 2023-04-06 23:41:39 +02:00
parent 75985f1626
commit 82daadc1c6
3 changed files with 24 additions and 5 deletions

View File

@ -1,6 +1,7 @@
Changes in version x.x.x:
Breaking changes:
- ls: Added option `--recursive`, note: default is now non-recursive if a path is given
Bugs fixed:
- Fixed compilation on OpenBSD.
@ -9,3 +10,4 @@ Bugs fixed:
New features:
- REST backend: Set User-Agent header
- ls: Added option `--recursive`

View File

@ -21,7 +21,7 @@ use super::{Metadata, Node, NodeType};
#[derive(Clone, Debug, Serialize, Deserialize, Getters)]
pub struct Tree {
#[serde(deserialize_with = "deserialize_null_default")]
nodes: Vec<Node>,
pub nodes: Vec<Node>,
}
fn deserialize_null_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>

View File

@ -14,6 +14,10 @@ pub(super) struct Opts {
#[clap(flatten, help_heading = "SNAPSHOT FILTER OPTIONS (when using latest)")]
filter: SnapshotFilter,
/// recursively list the dir (default when no PATH is given)
#[clap(long)]
recursive: bool,
/// Snapshot/path to list
#[clap(value_name = "SNAPSHOT[:PATH]")]
snap: String,
@ -26,15 +30,28 @@ pub(super) fn execute(
) -> Result<()> {
config_file.merge_into("snapshot-filter", &mut opts.filter)?;
let be = &repo.dbe;
let mut recursive = opts.recursive;
let (id, path) = opts.snap.split_once(':').unwrap_or((&opts.snap, ""));
let (id, path) = opts.snap.split_once(':').unwrap_or_else(|| {
recursive = true;
(&opts.snap, "")
});
let snap = SnapshotFile::from_str(be, id, |sn| sn.matches(&opts.filter), progress_counter(""))?;
let index = IndexBackend::new(be, progress_counter(""))?;
let node = Tree::node_from_path(&index, snap.tree, Path::new(path))?;
for item in NodeStreamer::new(index, &node)? {
let (path, _) = item?;
println!("{path:?} ");
if recursive {
for item in NodeStreamer::new(index, &node)? {
let (path, _) = item?;
println!("{path:?} ");
}
} else if node.is_dir() {
let tree = Tree::from_backend(&index, node.subtree.unwrap())?.nodes;
for node in tree {
println!("{:?} ", node.name());
}
} else {
println!("{:?} ", node.name());
}
Ok(())