From 14fec49b4243d62213c40b2503442f4cc6acc877 Mon Sep 17 00:00:00 2001
From: Ryan Reed <git@ryanreed.net>
Date: Fri, 1 Jul 2022 10:36:17 -0400
Subject: [PATCH] Adding support for 'apply' action

---
 src/transpose/console.py   | 14 +++++++++++++-
 src/transpose/transpose.py | 17 +++++++++++++++++
 tests/test_transpose.py    | 23 ++++++++++++++++++++---
 3 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/src/transpose/console.py b/src/transpose/console.py
index 00f6d74..c56604b 100644
--- a/src/transpose/console.py
+++ b/src/transpose/console.py
@@ -17,7 +17,9 @@ def entry_point() -> None:
         force=args.force,
     )
 
-    if args.action == "restore":
+    if args.action == "apply":
+        t.apply()
+    elif args.action == "restore":
         t.restore()
     elif args.action == "store":
         t.store(name=args.name)
@@ -43,6 +45,16 @@ def parse_arguments():
         help="Transpose Action", dest="action", required=True
     )
 
+    apply_parser = subparsers.add_parser(
+        "apply",
+        help="Recreate the symlink from the cache file (useful after moving store loction)",
+        parents=[base_parser],
+    )
+    apply_parser.add_argument(
+        "target_path",
+        help="The path to the directory to locate the cache file",
+    )
+
     restore_parser = subparsers.add_parser(
         "restore",
         help="Move a transposed directory back to it's original location",
diff --git a/src/transpose/transpose.py b/src/transpose/transpose.py
index 5ce95c0..f03f896 100644
--- a/src/transpose/transpose.py
+++ b/src/transpose/transpose.py
@@ -22,6 +22,23 @@ class Transpose:
 
         self.force = force
 
+    def apply(self) -> None:
+        """
+        Recreate the symlink from an existing cache file
+        """
+        if not self.cache_path.exists():
+            raise TransposeError(
+                f"Cache file does not exist indicating target is not managed by Transpose: {self.cache_path}"
+            )
+
+        cache = get_cache(self.cache_path)
+        original_path = Path(cache["original_path"])
+
+        if original_path.is_symlink():
+            remove(original_path)
+
+        symlink(target_path=self.cache_path.parent, symlink_path=original_path)
+
     def restore(self) -> None:
         """
         Restores a previously Transpose managed directory to it's previous location.
diff --git a/tests/test_transpose.py b/tests/test_transpose.py
index 442678a..54d7050 100644
--- a/tests/test_transpose.py
+++ b/tests/test_transpose.py
@@ -10,7 +10,7 @@ from .utils import STORE_DIR, TARGET_DIR, setup_env
 
 
 @setup_env()
-def test_transpose_init():
+def test_init():
     t = Transpose(
         target_path=TARGET_DIR,
         store_path=STORE_DIR,
@@ -26,7 +26,24 @@ def test_transpose_init():
 
 
 @setup_env()
-def test_transpose_store_restore():
+def test_apply():
+    store_path = Path(STORE_DIR)
+    target_path = Path(TARGET_DIR)
+    store_path.rmdir()
+    target_path.rename(store_path)
+
+    t = Transpose(
+        target_path=STORE_DIR,
+        store_path=STORE_DIR,
+    )
+    t.apply()
+
+    assert store_path.is_dir() and not store_path.is_symlink()
+    assert target_path.is_dir() and target_path.is_symlink()
+
+
+@setup_env()
+def test_store_restore():
     t = Transpose(
         target_path=TARGET_DIR,
         store_path=STORE_DIR,
@@ -52,7 +69,7 @@ def test_transpose_store_restore():
 
 
 @setup_env()
-def test_transpose_restore_force():
+def test_restore_force():
     nonexistent_path = Path(STORE_DIR).joinpath("long/path")
 
     # Overwrite cache file with nonexistent directory