From 86e5dd502d8bee4e084b68ece8a138c7b3c6366b Mon Sep 17 00:00:00 2001
From: Truman Kilen <t@kilen.me>
Date: Sun, 6 Aug 2023 14:15:07 -0500
Subject: [PATCH] Allow --strip-prefix to be specified for list command

---
 repak_cli/src/main.rs | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/repak_cli/src/main.rs b/repak_cli/src/main.rs
index 576ded5..9c663de 100644
--- a/repak_cli/src/main.rs
+++ b/repak_cli/src/main.rs
@@ -21,6 +21,10 @@ struct ActionList {
     /// Input .pak path
     #[arg(index = 1)]
     input: String,
+
+    /// Prefix to strip from entry path
+    #[arg(short, long, default_value = "../../../")]
+    strip_prefix: String,
 }
 
 #[derive(Parser, Debug)]
@@ -101,7 +105,7 @@ enum Action {
     /// Print .pak info
     Info(ActionInfo),
     /// List .pak files
-    List(ActionInfo),
+    List(ActionList),
     /// Unpack .pak file
     Unpack(ActionUnpack),
     /// Pack directory into .pak file
@@ -164,11 +168,32 @@ fn info(aes_key: Option<aes::Aes256>, action: ActionInfo) -> Result<(), repak::E
     Ok(())
 }
 
-fn list(aes_key: Option<aes::Aes256>, action: ActionInfo) -> Result<(), repak::Error> {
+fn list(aes_key: Option<aes::Aes256>, action: ActionList) -> Result<(), repak::Error> {
     let pak = repak::PakReader::new_any(BufReader::new(File::open(action.input)?), aes_key)?;
-    for f in pak.files() {
-        println!("{f}");
+
+    let mount_point = PathBuf::from(pak.mount_point());
+    let prefix = Path::new(&action.strip_prefix);
+
+    let full_paths = pak
+        .files()
+        .into_iter()
+        .map(|f| mount_point.join(f))
+        .collect::<Vec<_>>();
+    let stripped = full_paths
+        .iter()
+        .map(|f| {
+            f.strip_prefix(prefix)
+                .map_err(|_| repak::Error::PrefixMismatch {
+                    path: f.to_string_lossy().to_string(),
+                    prefix: prefix.to_string_lossy().to_string(),
+                })
+        })
+        .collect::<Result<Vec<_>, _>>()?;
+
+    for f in stripped {
+        println!("{}", f.display());
     }
+
     Ok(())
 }