Change sq packet split's prefix argument from a String to a PathBuf

- `sq packet split`'s `--prefix` argument is used to build a
    filename.  Make it a `PathBuf` instead of a `String`.

  - See .
This commit is contained in:
Neal H. Walfield 2023-11-17 10:09:41 +01:00
parent f9362886f4
commit dccae02148
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
3 changed files with 32 additions and 18 deletions

View File

@ -195,7 +195,7 @@ pub struct SplitCommand {
help = "Writes to files with PREFIX \
[defaults: \"FILE-\" if FILE is set, or \"output-\" if read from stdin]",
)]
pub prefix: Option<String>,
pub prefix: Option<PathBuf>,
}
#[derive(Debug, Args)]

View File

@ -5,6 +5,7 @@ use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
use std::time::SystemTime;
use sequoia_net::pks;
@ -747,8 +748,12 @@ impl<'a, 'store> VerificationHelper for VHelper<'a, 'store> {
}
}
pub fn split(input: &mut (dyn io::Read + Sync + Send), prefix: &str)
-> Result<()> {
pub fn split<P>(input: &mut (dyn io::Read + Sync + Send), prefix: P)
-> Result<()>
where P: AsRef<Path>
{
let prefix = prefix.as_ref();
// We (ab)use the mapping feature to create byte-accurate dumps of
// nested packets.
let mut ppr =
@ -760,12 +765,13 @@ pub fn split(input: &mut (dyn io::Read + Sync + Send), prefix: &str)
while let PacketParserResult::Some(pp) = ppr {
if let Some(map) = pp.map() {
let filename = format!(
"{}{}--{}{:?}", prefix,
let mut filename = prefix.as_os_str().to_os_string();
filename.push(
pos.iter().map(|n| format!("{}", n))
.collect::<Vec<String>>().join("-"),
pp.packet.kind().map(|_| "").unwrap_or("Unknown-"),
pp.packet.tag());
.collect::<Vec<String>>().join("-"));
filename.push(pp.packet.kind().map(|_| "").unwrap_or("Unknown-"));
filename.push(format!("{}", pp.packet.tag()));
let mut sink = File::create(filename)
.context("Failed to create output file")?;

View File

@ -1,3 +1,5 @@
use std::ffi::OsString;
use terminal_size::terminal_size;
use sequoia_openpgp as openpgp;
@ -54,20 +56,26 @@ pub fn dispatch(config: Config, command: cli::packet::Command)
cli::packet::Subcommands::Split(command) => {
let mut input = command.input.open()?;
let prefix =
// The prefix is either specified explicitly...
command.prefix.unwrap_or(
// The prefix is either specified explicitly...
command.prefix.map(|p| p.into_os_string())
.unwrap_or_else(|| {
// ... or we derive it from the input file...
command.input.and_then(|x| {
let mut prefix = command.input.and_then(|x| {
// (but only use the filename)
x.file_name().map(|f|
String::from(f.to_string_lossy())
)
x.file_name().map(|f| {
f.to_os_string()
})
})
// ... or we use a generic prefix...
.unwrap_or_else(|| String::from("output"))
// ... finally, add a hyphen to the derived prefix.
+ "-");
// ... or we use a generic prefix.
.unwrap_or_else(|| OsString::from("output"));
// We also add a hyphen to a derived prefix.
prefix.push("-");
prefix
});
commands::split(&mut input, &prefix)?;
},
cli::packet::Subcommands::Join(command) => {