Skip to content

Commit

Permalink
⚗️ Add experimental subcommand chunk for debug archive
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Nov 21, 2024
1 parent 079112a commit 1b21f02
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod acl;
pub mod append;
mod chmod;
mod chown;
mod chunk;
mod commons;
pub mod complete;
pub(crate) mod concat;
Expand Down
63 changes: 63 additions & 0 deletions cli/src/command/chunk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::command::Command;
use clap::Parser;
use pna::prelude::*;
use std::{fs, io, path::PathBuf};
use tabled::{builder::Builder as TableBuilder, settings::Style as TableStyle};

#[derive(Parser, Clone, Eq, PartialEq, Hash, Debug)]
#[command(args_conflicts_with_subcommands = true, arg_required_else_help = true)]
pub(crate) struct ChunkCommand {
#[command(subcommand)]
command: ChunkCommands,
}

impl Command for ChunkCommand {
#[inline]
fn execute(self) -> io::Result<()> {
match self.command {
ChunkCommands::List(cmd) => cmd.execute(),
}
}
}

#[derive(Parser, Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) enum ChunkCommands {
#[command(about = "List chunks")]
List(ListCommand),
}

#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[clap(disable_help_flag = true)]
pub(crate) struct ListCommand {
#[arg(short, long, help = "Display chunk body")]
pub(crate) long: bool,
#[arg(short, long, help = "Add a header row to each column")]
pub(crate) header: bool,
#[arg()]
pub(crate) archive: PathBuf,
#[arg(long, action = clap::ArgAction::Help)]
help: Option<bool>,
}

impl Command for ListCommand {
#[inline]
fn execute(self) -> io::Result<()> {
list_archive_chunks(self)
}
}

fn list_archive_chunks(args: ListCommand) -> io::Result<()> {
let archive = fs::File::open(args.archive)?;
let mut builder = TableBuilder::new();
if args.header {
builder.push_record(["type", "size"])
}
for chunk in pna::read_as_chunks(archive)? {
let chunk = chunk?;
builder.push_record([chunk.ty().to_string(), chunk.length().to_string()]);
}
let mut table = builder.build();
table.with(TableStyle::empty());
println!("{}", table);
Ok(())
}
3 changes: 3 additions & 0 deletions cli/src/command/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Command for ExperimentalCommand {
ExperimentalCommands::Xattr(cmd) => cmd.execute(),
ExperimentalCommands::Acl(cmd) => cmd.execute(),
ExperimentalCommands::Migrate(cmd) => cmd.execute(),
ExperimentalCommands::Chunk(cmd) => cmd.execute(),
}
}
}
Expand All @@ -43,4 +44,6 @@ pub(crate) enum ExperimentalCommands {
Acl(command::acl::AclCommand),
#[command(about = "Migrate old format to latest format")]
Migrate(command::migrate::MigrateCommand),
#[command(about = "Chunk level operation")]
Chunk(command::chunk::ChunkCommand),
}

0 comments on commit 1b21f02

Please sign in to comment.