Add config php class and use it for api key an shortcuts in snips
This commit is contained in:
parent
fe50075e2b
commit
ae1c5cc147
@ -1,4 +1,4 @@
|
|||||||
argvParser.php
|
argvParser.php
|
||||||
functions.php
|
functions.php
|
||||||
snips.php
|
snips.php
|
||||||
api_key
|
config.php
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
require_once __DIR__ . '/../argvParser.php';
|
require_once __DIR__ . '/../argvParser.php';
|
||||||
require_once __DIR__ . '/../functions.php';
|
require_once __DIR__ . '/../functions.php';
|
||||||
require_once __DIR__ . '/../snips.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);
|
$parsed = $argvParser->parseArgv($argv);
|
||||||
if ($parsed === false) {
|
if ($parsed === false) {
|
||||||
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
|
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
|
||||||
@ -15,29 +17,39 @@ if ($parsed === false) {
|
|||||||
$command = $parsed->get('c');
|
$command = $parsed->get('c');
|
||||||
$id = $parsed->get('id');
|
$id = $parsed->get('id');
|
||||||
$file = $parsed->get('f');
|
$file = $parsed->get('f');
|
||||||
$apiKey = $parsed->get('a');
|
$apiKey = $parsed->get('k');
|
||||||
|
$getShortcut = $parsed->get('s');
|
||||||
$content = $parsed->getRest();
|
$content = $parsed->getRest();
|
||||||
|
|
||||||
if (empty($apiKey)) {
|
if (empty($apiKey)) {
|
||||||
$apiFile = __DIR__ . '/../api_key';
|
$apiKey = $config->get('apiKey');
|
||||||
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)) {
|
if (empty($apiKey)) {
|
||||||
line('No api key provided');
|
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);
|
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/';
|
$baseUrl = 'https://snips.loken.nl/api/';
|
||||||
$snips = new snips($baseUrl, $apiKey);
|
$snips = new snips($baseUrl, $apiKey);
|
||||||
|
|
||||||
if ($command === 'get') {
|
switch ($command) {
|
||||||
|
case 'get':
|
||||||
$content = $snips->getSnip($id);
|
$content = $snips->getSnip($id);
|
||||||
if ($file) {
|
if ($file) {
|
||||||
file_put_contents($file, $content);
|
file_put_contents($file, $content);
|
||||||
@ -45,7 +57,8 @@ if ($command === 'get') {
|
|||||||
} else {
|
} else {
|
||||||
line($content);
|
line($content);
|
||||||
}
|
}
|
||||||
} elseif ($command === 'set') {
|
break;
|
||||||
|
case 'set':
|
||||||
if ($file) {
|
if ($file) {
|
||||||
if (!file_exists($file)) {
|
if (!file_exists($file)) {
|
||||||
line('File ' . $file . ' does not exist');
|
line('File ' . $file . ' does not exist');
|
||||||
@ -58,7 +71,8 @@ if ($command === 'get') {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$content = $snips->postSnip($id, $content);
|
$content = $snips->postSnip($id, $content);
|
||||||
} else {
|
break;
|
||||||
|
default:
|
||||||
line('Unknown command: ' . $command);
|
line('Unknown command: ' . $command);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -1 +1,62 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__ . '/functions.php';
|
||||||
|
|
||||||
|
readonly class config
|
||||||
|
{
|
||||||
|
private const CONFIG_BASE_PATH = '.config';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private string $name
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(string $key, mixed $default = null): mixed
|
||||||
|
{
|
||||||
|
$config = include $this->getConfigFile();
|
||||||
|
return $config[$key] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(string $key, mixed $value): void
|
||||||
|
{
|
||||||
|
$config = include $this->getConfigFile();
|
||||||
|
$config[$key] = $value;
|
||||||
|
$this->writeConfig($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getConfigFile(): string
|
||||||
|
{
|
||||||
|
return path(
|
||||||
|
$this->getConfigPath(),
|
||||||
|
'config.php'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getConfigPath(): string
|
||||||
|
{
|
||||||
|
return path(
|
||||||
|
getenv('HOME'),
|
||||||
|
self::CONFIG_BASE_PATH,
|
||||||
|
$this->name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function init(): void
|
||||||
|
{
|
||||||
|
if (file_exists($this->getConfigFile())) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
if (!is_dir($this->getConfigPath())) {
|
||||||
|
mkdir($this->getConfigPath(), 0755, true);
|
||||||
|
}
|
||||||
|
$this->writeConfig([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function writeConfig(array $config): void
|
||||||
|
{
|
||||||
|
file_put_contents($this->getConfigFile(), '<?php return ' . var_export($config, true) . ';');
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
function path(...$segments): string
|
function path(...$segments): string
|
||||||
{
|
{
|
||||||
return join(DIRECTORY_SEPARATOR, $segments);
|
return implode(DIRECTORY_SEPARATOR, $segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
function line(string $line): void
|
function line(string $line): void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user