From 7f610dc31a88c530c3737bf9941668259dde1f7d Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 19 May 2025 18:41:49 +0200 Subject: [PATCH] PHP: Add lexit function for easy exiting with text --- php/bin/snips | 20 ++++++++------------ php/bin/stow | 8 +++----- php/includes/functions.php | 8 ++++++++ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/php/bin/snips b/php/bin/snips index 562e402..4a83dba 100755 --- a/php/bin/snips +++ b/php/bin/snips @@ -10,8 +10,7 @@ $config = new homeConfig('snips'); $argvParser = new argvParser(['f' => 'File', 'a' => 'Api key', 's' => 'Shortcut'], ['c' => 'Command', 'id' => 'Snip id']); $parsed = $argvParser->parseArgv($argv); if ($parsed === false) { - line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp()); - exit(1); + lexit('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp(), 1); } $command = $parsed->get('c'); @@ -28,15 +27,13 @@ if (empty($apiKey)) { line('Api key stored in config file'); } if (empty($apiKey)) { - line('Api Key is empty'); - exit(1); + lexit('Api Key is empty', 1); } if ($getShortcut) { $shortcut = $config->get('shortcuts')[$id] ?? null; if (empty($shortcut)) { - line('Shortcut ' . $getShortcut . ' not found'); - exit(1); + lexit('Shortcut ' . $getShortcut . ' not found', 1); } $id = $shortcut[0]; $file = $shortcut[1]; @@ -58,18 +55,17 @@ switch ($command) { case 'set': if ($file) { if (!file_exists($file)) { - line('File ' . $file . ' does not exist'); - exit(1); + lexit('File ' . $file . ' does not exist', 1); } $content = file_get_contents($file); if ($content === false) { - line('Error reading file ' . $file); - exit(1); + lexit('Error reading file ' . $file, 1); } } $content = $snips->postSnip($id, $content); break; default: - line('Unknown command: ' . $command); - exit(1); + lexit('Unknown command: ' . $command, 1); } + +lexit('Done successfully'); \ No newline at end of file diff --git a/php/bin/stow b/php/bin/stow index ba77ada..78af282 100755 --- a/php/bin/stow +++ b/php/bin/stow @@ -12,8 +12,7 @@ $argvParser = new ArgvParser( ); $parsed = $argvParser->parseArgv($argv); if ($parsed === false) { - line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp()); - exit(1); + lexit('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp(), 1); } $cwd = getcwd(); @@ -29,11 +28,10 @@ $config = new config($stowPath); $targetPath = $config->get('targetPath', dirname($stowPath, 2)); if (!is_dir($stowPath)) { - line("Directory '$stowName' does not exist"); - exit(1); + lexit("Directory '$stowName' does not exist", 1); } $stow = new stow($action, $backup, $force, $unwrap); $stow->run($stowPath, $targetPath); -exit(0); \ No newline at end of file +lexit('Done successfully'); \ No newline at end of file diff --git a/php/includes/functions.php b/php/includes/functions.php index 791f33a..5187a70 100644 --- a/php/includes/functions.php +++ b/php/includes/functions.php @@ -1,5 +1,7 @@