From 01297a81f866e3c88b880a0ae9b867de2a7dbf68 Mon Sep 17 00:00:00 2001 From: Truman Kilen Date: Wed, 22 Feb 2023 17:35:19 -0600 Subject: [PATCH] Add get command --- repak_cli/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/repak_cli/src/main.rs b/repak_cli/src/main.rs index 593c2f5..12e8d89 100644 --- a/repak_cli/src/main.rs +++ b/repak_cli/src/main.rs @@ -88,6 +88,25 @@ struct ActionPack { verbose: bool, } +#[derive(Parser, Debug)] +struct ActionGet { + /// Input .pak path + #[arg(index = 1)] + input: String, + + /// Path to file to read to stdout + #[arg(index = 2)] + file: String, + + /// Prefix to strip from entry path + #[arg(short, long, default_value = "../../../")] + strip_prefix: String, + + /// Base64 encoded AES encryption key if the pak is encrypted + #[arg(short, long)] + aes_key: Option, +} + #[derive(Subcommand, Debug)] enum Action { /// Print .pak info @@ -98,6 +117,8 @@ enum Action { Unpack(ActionUnpack), /// Pack directory into .pak file Pack(ActionPack), + /// Reads a single file to stdout + Get(ActionGet), } #[derive(Parser, Debug)] @@ -118,6 +139,7 @@ fn main() -> Result<(), repak::Error> { Action::List(args) => list(args), Action::Unpack(args) => unpack(args), Action::Pack(args) => pack(args), + Action::Get(args) => get(args), } } @@ -259,3 +281,22 @@ fn pack(args: ActionPack) -> Result<(), repak::Error> { Ok(()) } + +fn get(args: ActionGet) -> Result<(), repak::Error> { + let mut reader = BufReader::new(File::open(&args.input)?); + let pak = repak::PakReader::new_any( + &mut reader, + args.aes_key.map(|k| aes_key(k.as_str())).transpose()?, + )?; + let mount_point = PathBuf::from(pak.mount_point()); + let prefix = Path::new(&args.strip_prefix); + + let full_path = mount_point.join(args.file); + let file = full_path + .strip_prefix(&prefix) + .map_err(|_| repak::Error::Other("prefix does not match"))?; + + use std::io::Write; + std::io::stdout().write_all(&pak.get(&file.to_string_lossy(), &mut reader)?)?; + Ok(()) +}