From 90751f5bece11770e2fe2c4ea0907e4acb1d96ee Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 19 May 2025 00:26:52 +0200 Subject: [PATCH] Implement stow config file and folder unwrapping Move includes to their own folder, config.php will otherwise be included and cleans up nicely --- php/.nostow | 5 +---- php/bin/snips | 13 +++++-------- php/bin/stow | 16 ++++++++++------ php/{ => includes}/argvParser.php | 0 php/{ => includes}/config.php | 0 php/{ => includes}/functions.php | 0 php/{ => includes}/snips.php | 0 php/{ => includes}/stow.php | 21 +++++++++++++++------ 8 files changed, 31 insertions(+), 24 deletions(-) rename php/{ => includes}/argvParser.php (100%) rename php/{ => includes}/config.php (100%) rename php/{ => includes}/functions.php (100%) rename php/{ => includes}/snips.php (100%) rename php/{ => includes}/stow.php (79%) diff --git a/php/.nostow b/php/.nostow index 2bef8a8..e61ddd7 100644 --- a/php/.nostow +++ b/php/.nostow @@ -1,4 +1 @@ -argvParser.php -functions.php -snips.php -config.php +includes \ No newline at end of file diff --git a/php/bin/snips b/php/bin/snips index 4cae2d0..562e402 100755 --- a/php/bin/snips +++ b/php/bin/snips @@ -1,10 +1,10 @@ #!/usr/bin/env php 'File', 'a' => 'Api key', 's' => 'Shortcut'], ['c' => 'Command', 'id' => 'Snip id']); @@ -17,13 +17,10 @@ if ($parsed === false) { $command = $parsed->get('c'); $id = $parsed->get('id'); $file = $parsed->get('f'); -$apiKey = $parsed->get('k'); +$apiKey = $parsed->get('k', $config->get('apiKey')); $getShortcut = $parsed->get('s'); $content = $parsed->getRest(); -if (empty($apiKey)) { - $apiKey = $config->get('apiKey'); -} if (empty($apiKey)) { line('Input api key:'); $apiKey = trim(fgets(STDIN)); diff --git a/php/bin/stow b/php/bin/stow index 9f76913..ba77ada 100755 --- a/php/bin/stow +++ b/php/bin/stow @@ -1,12 +1,13 @@ #!/usr/bin/env php 'Unstow files', 'b' => 'Backup existing files', 'f' => 'Force overwrite'], + ['u' => 'Unstow files', 'b' => 'Backup existing files', 'f' => 'Force overwrite', 'w' => 'Unwrap folders'], ['stow-directory' => 'The directory to stow'] ); $parsed = $argvParser->parseArgv($argv); @@ -19,17 +20,20 @@ $cwd = getcwd(); $action = $parsed->get('u') ? stow::ACTION_UNSTOW : stow::ACTION_STOW; $backup = $parsed->get('b'); $force = $parsed->get('f'); +$unwrap = $parsed->get('w'); $stowName = $parsed->get('stow-directory'); $stowPath = path($cwd, $stowName); -$targetPath = dirname($stowPath, 2); + +$config = new config($stowPath); +$targetPath = $config->get('targetPath', dirname($stowPath, 2)); if (!is_dir($stowPath)) { line("Directory '$stowName' does not exist"); exit(1); } -$stow = new stow($action, $backup, $force); +$stow = new stow($action, $backup, $force, $unwrap); $stow->run($stowPath, $targetPath); exit(0); \ No newline at end of file diff --git a/php/argvParser.php b/php/includes/argvParser.php similarity index 100% rename from php/argvParser.php rename to php/includes/argvParser.php diff --git a/php/config.php b/php/includes/config.php similarity index 100% rename from php/config.php rename to php/includes/config.php diff --git a/php/functions.php b/php/includes/functions.php similarity index 100% rename from php/functions.php rename to php/includes/functions.php diff --git a/php/snips.php b/php/includes/snips.php similarity index 100% rename from php/snips.php rename to php/includes/snips.php diff --git a/php/stow.php b/php/includes/stow.php similarity index 79% rename from php/stow.php rename to php/includes/stow.php index 6d06357..7309520 100644 --- a/php/stow.php +++ b/php/includes/stow.php @@ -9,6 +9,7 @@ readonly class stow private string $action = self::ACTION_STOW, private bool $backup = false, private bool $force = false, + private bool $unwrap = false, ) {} private function getNoStow(string $path): array @@ -27,7 +28,7 @@ readonly class stow function run(string $sourcePath, string $targetPath): void { $noStow = $this->getNoStow($sourcePath); - $sourceFiles = array_diff(scandir($sourcePath), ['..', '.', '.nostow', ...$noStow]); + $sourceFiles = array_diff(scandir($sourcePath), ['..', '.', 'config.php', '.nostow', ...$noStow]); foreach ($sourceFiles as $sourceFile) { $targetFile = path($targetPath, $sourceFile); @@ -35,10 +36,10 @@ readonly class stow switch ($this->action) { case self::ACTION_STOW: - $this->stow($targetFile, $stowFile); + $this->stow($stowFile, $targetFile); break; case self::ACTION_UNSTOW: - $this->unstow($targetFile, $stowFile); + $this->unstow($stowFile, $targetFile); break; default: line("Unknown action: {$this->action}"); @@ -47,14 +48,22 @@ readonly class stow } } - public function stow(string $targetFile, string $stowFile): void + public function stow(string $stowFile, string $targetFile): void { if (is_link($targetFile)) { $targetLink = readlink($targetFile); if ($targetLink === $stowFile) { line("File $targetFile is already stowed from $stowFile"); } else { - line("File $targetFile is linked from $targetLink"); + if ($this->unwrap) { + line("Unwrapping $targetFile from $targetLink"); + unlink($targetFile); + mkdir($targetFile); + $this->run($targetLink, $targetFile); + $this->run($stowFile, $targetFile); + } else { + line("File $targetFile is linked from $targetLink, ignoring, add -w to unwrap"); + } } return; } @@ -88,7 +97,7 @@ readonly class stow symlink($stowFile, $targetFile); } - public function unstow(string $targetFile, string $stowFile): void + public function unstow(string $stowFile, string $targetFile): void { if (is_link($targetFile) && readlink($targetFile) === $stowFile) { line("Unstow $targetFile from $stowFile");