Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): More dump options #1339

Merged
merged 9 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adjust options; add tar.gz output
  • Loading branch information
aawsome committed Nov 30, 2024
commit fe9e3ffdc534075ac0f94433b9f06b25253d8784
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ open = "5.3.1"
self_update = { version = "=0.39.0", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] } # FIXME: Downgraded to 0.39.0 due to https://github.com/jaemk/self_update/issues/136
tar = "0.4.43"
toml = "0.8"
flate2 = "1.0.34"

[dev-dependencies]
abscissa_core = { version = "0.8.1", default-features = false, features = ["testing"] }
Expand Down
69 changes: 60 additions & 9 deletions src/commands/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{repository::CliIndexedRepo, status_err, Application, RUSTIC_APP};

use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
use flate2::{write::GzEncoder, Compression};
use log::warn;
use rustic_core::{
repofile::{Node, NodeType},
Expand All @@ -21,9 +22,25 @@ pub(crate) struct DumpCmd {
#[clap(value_name = "SNAPSHOT[:PATH]")]
snap: String,

/// Listing options
#[clap(flatten)]
ls_opts: LsOptions,
/// set archive format to use.
#[clap(long, value_name = "FORMAT", value_parser=["auto", "content", "tar", "tar.gz"], default_value = "auto")]
archive: String,

/// Glob pattern to exclude/include (can be specified multiple times)
#[clap(long, help_heading = "Exclude options")]
glob: Vec<String>,

/// Same as --glob pattern but ignores the casing of filenames
#[clap(long, value_name = "GLOB", help_heading = "Exclude options")]
iglob: Vec<String>,

/// Read glob patterns to exclude/include from this file (can be specified multiple times)
#[clap(long, value_name = "FILE", help_heading = "Exclude options")]
glob_file: Vec<String>,

/// Same as --glob-file ignores the casing of filenames in patterns
#[clap(long, value_name = "FILE", help_heading = "Exclude options")]
iglob_file: Vec<String>,
}

impl Runnable for DumpCmd {
Expand All @@ -46,17 +63,51 @@ impl DumpCmd {
let node =
repo.node_from_snapshot_path(&self.snap, |sn| config.snapshot_filter.matches(sn))?;

let mut stdout = std::io::stdout();
if node.is_file() {
repo.dump(&node, &mut stdout)?;
} else {
dump_tar(&repo, &node, &mut stdout, &self.ls_opts)?;
}
let stdout = std::io::stdout();

let ls_opts = LsOptions::default()
.glob(self.glob.clone())
.glob_file(self.glob_file.clone())
.iglob(self.iglob.clone())
.iglob_file(self.iglob_file.clone())
.recursive(true);

let mut w: Box<dyn Write> = Box::new(stdout);

match (self.archive.as_str(), node.is_file()) {
("auto", true) | ("content", _) => dump_content(&repo, &node, &mut w, &ls_opts)?,
("auto", false) | ("tar", _) => dump_tar(&repo, &node, &mut w, &ls_opts)?,
("tar.gz", _) => dump_tar_gz(&repo, &node, &mut w, &ls_opts)?,
_ => {}
};

Ok(())
}
}

fn dump_content(
repo: &CliIndexedRepo,
node: &Node,
w: &mut impl Write,
ls_opts: &LsOptions,
) -> Result<()> {
for item in repo.ls(node, ls_opts)? {
let (_, node) = item?;
repo.dump(&node, w)?;
}
Ok(())
}

fn dump_tar_gz(
repo: &CliIndexedRepo,
node: &Node,
w: &mut impl Write,
ls_opts: &LsOptions,
) -> Result<()> {
let mut w = GzEncoder::new(w, Compression::default());
dump_tar(repo, node, &mut w, ls_opts)
}

fn dump_tar(
repo: &CliIndexedRepo,
node: &Node,
Expand Down