diff --git a/src/args.rs b/src/args.rs index b02ee03..b614c50 100644 --- a/src/args.rs +++ b/src/args.rs @@ -130,7 +130,7 @@ pub enum Command { /// Input directory input: String, /// Output archive file - output: String, + output: Option, }, } diff --git a/src/main.rs b/src/main.rs index 0c02fc1..9d1fcae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::>(); - 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); diff --git a/src/scripts/mod.rs b/src/scripts/mod.rs index bf6b86a..7671a31 100644 --- a/src/scripts/mod.rs +++ b/src/scripts/mod.rs @@ -23,4 +23,6 @@ lazy_static::lazy_static! { ]; pub static ref ALL_EXTS: Vec = BUILDER.iter().flat_map(|b| b.extensions()).map(|s| s.to_string()).collect(); + pub static ref ARCHIVE_EXTS: Vec = + BUILDER.iter().filter(|b| b.is_archive()).flat_map(|b| b.extensions()).map(|s| s.to_string()).collect(); }