71 lines
2.0 KiB
PHP
Executable File

#!/usr/bin/env php
<?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']);
$parsed = $argvParser->parseArgv($argv);
if ($parsed === false) {
lexit('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp(), 1);
}
$command = $parsed->get('c');
$id = $parsed->get('id');
$file = $parsed->get('f');
$apiKey = $parsed->get('k', $config->get('apiKey'));
$getShortcut = $parsed->get('s');
$content = $parsed->getRest();
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)) {
lexit('Api Key is empty', 1);
}
if ($getShortcut) {
$shortcut = $config->get('shortcuts')[$id] ?? null;
if (empty($shortcut)) {
lexit('Shortcut ' . $getShortcut . ' not found', 1);
}
$id = $shortcut[0];
$file = $shortcut[1];
}
$baseUrl = 'https://snips.loken.nl/api/';
$snips = new snips($baseUrl, $apiKey);
switch ($command) {
case 'get':
$content = $snips->getSnip($id);
if ($file) {
file_put_contents($file, $content);
line('Snip content saved to ' . $file);
} else {
line($content);
}
break;
case 'set':
if ($file) {
if (!file_exists($file)) {
lexit('File ' . $file . ' does not exist', 1);
}
$content = file_get_contents($file);
if ($content === false) {
lexit('Error reading file ' . $file, 1);
}
}
$content = $snips->postSnip($id, $content);
break;
default:
lexit('Unknown command: ' . $command, 1);
}
lexit('Done successfully');