Implement stow config file and folder unwrapping

Move includes to their own folder, config.php will otherwise be included
and cleans up nicely
This commit is contained in:
Tim 2025-05-19 00:26:52 +02:00
parent b7e5ee3b0e
commit 90751f5bec
8 changed files with 31 additions and 24 deletions

View File

@ -1,4 +1 @@
argvParser.php
functions.php
snips.php
config.php
includes

View File

@ -1,10 +1,10 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../argvParser.php';
require_once __DIR__ . '/../functions.php';
require_once __DIR__ . '/../snips.php';
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/argvParser.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/snips.php';
require_once __DIR__ . '/../includes/config.php';
$config = new homeConfig('snips');
$argvParser = new argvParser(['f' => '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));

View File

@ -1,12 +1,13 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../argvParser.php';
require_once __DIR__ . '/../functions.php';
require_once __DIR__ . '/../stow.php';
require_once __DIR__ . '/../includes/argvParser.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/stow.php';
require_once __DIR__ . '/../includes/config.php';
$argvParser = new ArgvParser(
['u' => '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);

View File

@ -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");