From ecfbbcf4ac2ec174c0378cb54e30eca71a07df2e Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Mon, 7 Feb 2022 15:29:57 +0100 Subject: [PATCH] Add ls command --- src/commands/list.rs | 2 +- src/commands/ls.rs | 35 +++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 7 ++++++- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/commands/ls.rs diff --git a/src/commands/list.rs b/src/commands/list.rs index 1c01be6..5a0ce89 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -2,7 +2,7 @@ use anyhow::{bail, Result}; use clap::Parser; use crate::backend::{FileType, ReadBackend}; -use crate::index::{indexfiles::AllIndexFiles}; +use crate::index::indexfiles::AllIndexFiles; #[derive(Parser)] pub(super) struct Opts { diff --git a/src/commands/ls.rs b/src/commands/ls.rs new file mode 100644 index 0000000..f3bd787 --- /dev/null +++ b/src/commands/ls.rs @@ -0,0 +1,35 @@ +use anyhow::{anyhow, bail, Result}; +use clap::Parser; + +use crate::backend::{FileType, MapResult, ReadBackend}; +use crate::blob::{Tree, TreeIterator}; +use crate::id::Id; +use crate::index::{indexfiles::AllIndexFiles, ReadIndex}; +use crate::repo::SnapshotFile; + +#[derive(Parser)] +pub(super) struct Opts { + /// snapshot to ls + id: String, +} + +pub(super) fn execute(be: &impl ReadBackend, opts: Opts) -> Result<()> { + let id = Id::from_hex(&opts.id).or_else(|_| { + // if the given id param is not a full Id, search for a suitable one + let res = be.find_starts_with(FileType::Snapshot, &[&opts.id])?[0]; + match res { + MapResult::Some(id) => Ok(id), + MapResult::None => Err(anyhow!("no suitable id found for {}", &opts.id)), + MapResult::NonUnique => Err(anyhow!("id {} is not unique", &opts.id)), + } + })?; + + let index = AllIndexFiles::new(be.clone()); + let snap = SnapshotFile::from_backend(be, id)?; + + for path_node in TreeIterator::from_id(be.clone(), index, snap.tree) { + println!("{:?} ", path_node.path); + } + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 8cc8341..24e811c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,6 +6,7 @@ use crate::repo; mod cat; mod list; +mod ls; mod snapshots; #[derive(Parser)] @@ -33,6 +34,9 @@ enum Command { /// cat files Snapshots(snapshots::Opts), + + /// ls snapshots + Ls(ls::Opts), } pub fn execute() -> Result<()> { @@ -43,8 +47,9 @@ pub fn execute() -> Result<()> { let dbe = DecryptBackend::new(&be, key); match args.command { - Command::List(opts) => list::execute(&be, opts), + Command::List(opts) => list::execute(&dbe, opts), Command::Cat(opts) => cat::execute(&be, &dbe, opts), Command::Snapshots(opts) => snapshots::execute(&dbe, opts), + Command::Ls(opts) => ls::execute(&dbe, opts), } }