Add config php class and use it for api key an shortcuts in snips
This commit is contained in:
@ -4,8 +4,10 @@
|
||||
require_once __DIR__ . '/../argvParser.php';
|
||||
require_once __DIR__ . '/../functions.php';
|
||||
require_once __DIR__ . '/../snips.php';
|
||||
require_once __DIR__ . '/../config.php';
|
||||
|
||||
$argvParser = new argvParser(['f' => 'File', 'a' => 'Api key'], ['c' => 'Command', 'id' => 'Snip id']);
|
||||
$config = new config('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());
|
||||
@ -15,50 +17,62 @@ if ($parsed === false) {
|
||||
$command = $parsed->get('c');
|
||||
$id = $parsed->get('id');
|
||||
$file = $parsed->get('f');
|
||||
$apiKey = $parsed->get('a');
|
||||
$apiKey = $parsed->get('k');
|
||||
$getShortcut = $parsed->get('s');
|
||||
$content = $parsed->getRest();
|
||||
|
||||
if (empty($apiKey)) {
|
||||
$apiFile = __DIR__ . '/../api_key';
|
||||
if (!file_exists($apiFile)) {
|
||||
line('Input api key:');
|
||||
$apiKey = trim(fgets(STDIN));
|
||||
file_put_contents($apiFile, $apiKey);
|
||||
line('Api key saved to ' . $apiFile);
|
||||
} else {
|
||||
$apiKey = file_get_contents($apiFile);
|
||||
}
|
||||
if (empty($apiKey)) {
|
||||
line('No api key provided');
|
||||
$apiKey = $config->get('apiKey');
|
||||
}
|
||||
if (empty($apiKey)) {
|
||||
line('Input api key:');
|
||||
$apiKey = trim(fgets(STDIN));
|
||||
$config->set('apiKey', $apiKey);
|
||||
line('Api key stored in config file');
|
||||
}
|
||||
if (empty($apiKey)) {
|
||||
line('Api Key is empty');
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if ($getShortcut) {
|
||||
$shortcut = $config->get('shortcuts')[$id] ?? null;
|
||||
if (empty($shortcut)) {
|
||||
line('Shortcut ' . $getShortcut . ' not found');
|
||||
exit(1);
|
||||
}
|
||||
$id = $shortcut[0];
|
||||
$file = $shortcut[1];
|
||||
}
|
||||
|
||||
$baseUrl = 'https://snips.loken.nl/api/';
|
||||
$snips = new snips($baseUrl, $apiKey);
|
||||
|
||||
if ($command === 'get') {
|
||||
$content = $snips->getSnip($id);
|
||||
if ($file) {
|
||||
file_put_contents($file, $content);
|
||||
line('Snip content saved to ' . $file);
|
||||
} else {
|
||||
line($content);
|
||||
}
|
||||
} elseif ($command === 'set') {
|
||||
if ($file) {
|
||||
if (!file_exists($file)) {
|
||||
line('File ' . $file . ' does not exist');
|
||||
exit(1);
|
||||
switch ($command) {
|
||||
case 'get':
|
||||
$content = $snips->getSnip($id);
|
||||
if ($file) {
|
||||
file_put_contents($file, $content);
|
||||
line('Snip content saved to ' . $file);
|
||||
} else {
|
||||
line($content);
|
||||
}
|
||||
$content = file_get_contents($file);
|
||||
if ($content === false) {
|
||||
line('Error reading file ' . $file);
|
||||
exit(1);
|
||||
break;
|
||||
case 'set':
|
||||
if ($file) {
|
||||
if (!file_exists($file)) {
|
||||
line('File ' . $file . ' does not exist');
|
||||
exit(1);
|
||||
}
|
||||
$content = file_get_contents($file);
|
||||
if ($content === false) {
|
||||
line('Error reading file ' . $file);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
$content = $snips->postSnip($id, $content);
|
||||
} else {
|
||||
line('Unknown command: ' . $command);
|
||||
exit(1);
|
||||
$content = $snips->postSnip($id, $content);
|
||||
break;
|
||||
default:
|
||||
line('Unknown command: ' . $command);
|
||||
exit(1);
|
||||
}
|
||||
|
Reference in New Issue
Block a user