Add ls command

This commit is contained in:
Alexander Weiss 2022-02-07 15:29:57 +01:00
parent 267941ae78
commit ecfbbcf4ac
3 changed files with 42 additions and 2 deletions

View File

@ -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 {

35
src/commands/ls.rs Normal file
View File

@ -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(())
}

View File

@ -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),
}
}