pack allow empty output

This commit is contained in:
2025-06-04 23:35:45 +08:00
parent 30f9b0f868
commit d28a604e34
3 changed files with 20 additions and 6 deletions

View File

@@ -130,7 +130,7 @@ pub enum Command {
/// Input directory
input: String,
/// Output archive file
output: String,
output: Option<String>,
},
}

View File

@@ -898,7 +898,7 @@ pub fn import_script(
pub fn pack_archive(
input: &str,
output: &str,
output: Option<&str>,
arg: &args::Arg,
config: &types::ExtraConfig,
) -> anyhow::Result<()> {
@@ -926,11 +926,23 @@ pub fn pack_archive(
})
.collect();
let reff = re_files.iter().map(|s| s.as_str()).collect::<Vec<_>>();
let mut archive = scripts::BUILDER
let builder = scripts::BUILDER
.iter()
.find(|b| b.script_type() == typ)
.ok_or_else(|| anyhow::anyhow!("Unsupported script type"))?
.create_archive(output, &reff, get_output_encoding(arg), config)?;
.ok_or_else(|| anyhow::anyhow!("Unsupported script type"))?;
let output = match output {
Some(output) => output.to_string(),
None => {
let mut pb = std::path::PathBuf::from(input);
let ext = builder.extensions().first().unwrap_or(&"unk");
pb.set_extension(ext);
if pb.to_string_lossy() == input {
pb.set_extension(format!("{}.{}", ext, ext));
}
pb.to_string_lossy().into_owned()
}
};
let mut archive = builder.create_archive(&output, &reff, get_output_encoding(arg), config)?;
for (file, name) in files.iter().zip(reff) {
let mut f = match std::fs::File::open(file) {
Ok(f) => f,
@@ -1067,7 +1079,7 @@ fn main() {
}
}
args::Command::Pack { input, output } => {
let re = pack_archive(input, output, &arg, &cfg);
let re = pack_archive(input, output.as_ref().map(|s| s.as_str()), &arg, &cfg);
if let Err(e) = re {
COUNTER.inc_error();
eprintln!("Error packing archive: {}", e);

View File

@@ -23,4 +23,6 @@ lazy_static::lazy_static! {
];
pub static ref ALL_EXTS: Vec<String> =
BUILDER.iter().flat_map(|b| b.extensions()).map(|s| s.to_string()).collect();
pub static ref ARCHIVE_EXTS: Vec<String> =
BUILDER.iter().filter(|b| b.is_archive()).flat_map(|b| b.extensions()).map(|s| s.to_string()).collect();
}