PHP: Add lexit function for easy exiting with text
This commit is contained in:
@ -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');
|
@ -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);
|
||||
lexit('Done successfully');
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
function path(...$segments): string
|
||||
{
|
||||
return implode(DIRECTORY_SEPARATOR, $segments);
|
||||
@ -9,3 +11,9 @@ function line(string $line): void
|
||||
{
|
||||
echo $line . PHP_EOL;
|
||||
}
|
||||
|
||||
#[NoReturn] function lexit(string $line, int $code = 0): void
|
||||
{
|
||||
line($line);
|
||||
exit($code);
|
||||
}
|
Reference in New Issue
Block a user