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']);
|
$argvParser = new argvParser(['f' => 'File', 'a' => 'Api key', 's' => 'Shortcut'], ['c' => 'Command', 'id' => 'Snip id']);
|
||||||
$parsed = $argvParser->parseArgv($argv);
|
$parsed = $argvParser->parseArgv($argv);
|
||||||
if ($parsed === false) {
|
if ($parsed === false) {
|
||||||
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
|
lexit('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp(), 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$command = $parsed->get('c');
|
$command = $parsed->get('c');
|
||||||
@ -28,15 +27,13 @@ if (empty($apiKey)) {
|
|||||||
line('Api key stored in config file');
|
line('Api key stored in config file');
|
||||||
}
|
}
|
||||||
if (empty($apiKey)) {
|
if (empty($apiKey)) {
|
||||||
line('Api Key is empty');
|
lexit('Api Key is empty', 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($getShortcut) {
|
if ($getShortcut) {
|
||||||
$shortcut = $config->get('shortcuts')[$id] ?? null;
|
$shortcut = $config->get('shortcuts')[$id] ?? null;
|
||||||
if (empty($shortcut)) {
|
if (empty($shortcut)) {
|
||||||
line('Shortcut ' . $getShortcut . ' not found');
|
lexit('Shortcut ' . $getShortcut . ' not found', 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
$id = $shortcut[0];
|
$id = $shortcut[0];
|
||||||
$file = $shortcut[1];
|
$file = $shortcut[1];
|
||||||
@ -58,18 +55,17 @@ switch ($command) {
|
|||||||
case 'set':
|
case 'set':
|
||||||
if ($file) {
|
if ($file) {
|
||||||
if (!file_exists($file)) {
|
if (!file_exists($file)) {
|
||||||
line('File ' . $file . ' does not exist');
|
lexit('File ' . $file . ' does not exist', 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
$content = file_get_contents($file);
|
$content = file_get_contents($file);
|
||||||
if ($content === false) {
|
if ($content === false) {
|
||||||
line('Error reading file ' . $file);
|
lexit('Error reading file ' . $file, 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$content = $snips->postSnip($id, $content);
|
$content = $snips->postSnip($id, $content);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
line('Unknown command: ' . $command);
|
lexit('Unknown command: ' . $command, 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lexit('Done successfully');
|
@ -12,8 +12,7 @@ $argvParser = new ArgvParser(
|
|||||||
);
|
);
|
||||||
$parsed = $argvParser->parseArgv($argv);
|
$parsed = $argvParser->parseArgv($argv);
|
||||||
if ($parsed === false) {
|
if ($parsed === false) {
|
||||||
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
|
lexit('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp(), 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$cwd = getcwd();
|
$cwd = getcwd();
|
||||||
@ -29,11 +28,10 @@ $config = new config($stowPath);
|
|||||||
$targetPath = $config->get('targetPath', dirname($stowPath, 2));
|
$targetPath = $config->get('targetPath', dirname($stowPath, 2));
|
||||||
|
|
||||||
if (!is_dir($stowPath)) {
|
if (!is_dir($stowPath)) {
|
||||||
line("Directory '$stowName' does not exist");
|
lexit("Directory '$stowName' does not exist", 1);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$stow = new stow($action, $backup, $force, $unwrap);
|
$stow = new stow($action, $backup, $force, $unwrap);
|
||||||
$stow->run($stowPath, $targetPath);
|
$stow->run($stowPath, $targetPath);
|
||||||
|
|
||||||
exit(0);
|
lexit('Done successfully');
|
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
|
|
||||||
function path(...$segments): string
|
function path(...$segments): string
|
||||||
{
|
{
|
||||||
return implode(DIRECTORY_SEPARATOR, $segments);
|
return implode(DIRECTORY_SEPARATOR, $segments);
|
||||||
@ -8,4 +10,10 @@ function path(...$segments): string
|
|||||||
function line(string $line): void
|
function line(string $line): void
|
||||||
{
|
{
|
||||||
echo $line . PHP_EOL;
|
echo $line . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[NoReturn] function lexit(string $line, int $code = 0): void
|
||||||
|
{
|
||||||
|
line($line);
|
||||||
|
exit($code);
|
||||||
}
|
}
|
Reference in New Issue
Block a user