129 lines
3.6 KiB
PHP
Executable File
129 lines
3.6 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
|
|
require_once __DIR__ . '/../argvParser.php';
|
|
require_once __DIR__ . '/../functions.php';
|
|
|
|
class snips
|
|
{
|
|
public function __construct(
|
|
private readonly string $baseUrl,
|
|
private readonly string $apiKey,
|
|
) {}
|
|
|
|
public function getSnip(int $id): string
|
|
{
|
|
$url = $this->baseUrl . 'snip/' . $id;
|
|
$headers = [
|
|
'X-AUTH-TOKEN: ' . $this->apiKey,
|
|
'Accept: application/json',
|
|
];
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$response = $this->getResponse($response);
|
|
|
|
if (isset($response['success']) && $response['success'] === true) {
|
|
return $response['data']['content'];
|
|
} else {
|
|
throw new Exception('Error fetching snip: ' . json_encode($response));
|
|
}
|
|
// rewrite to curl
|
|
}
|
|
|
|
public function postSnip(int $id, string $content): string
|
|
{
|
|
$url = $this->baseUrl . 'snip/' . $id;
|
|
$headers = [
|
|
'X-AUTH-TOKEN: ' . $this->apiKey,
|
|
'Accept: application/json',
|
|
];
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
$response = $this->getResponse($response);
|
|
|
|
if (isset($response['success']) && $response['success'] === true) {
|
|
return $response['data']['content'];
|
|
} else {
|
|
throw new Exception('Error updating snip: ' . json_encode($response));
|
|
}
|
|
}
|
|
|
|
public function getResponse(bool|string $response): array
|
|
{
|
|
$response = json_decode($response, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new Exception('Error decoding JSON response: ' . json_last_error_msg());
|
|
}
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
$argvParser = new argvParser(['f' => 'File', 'a' => 'Api key'], ['c' => 'Command', 'id' => 'Snip id']);
|
|
$parsed = $argvParser->parseArgv($argv);
|
|
if ($parsed === false) {
|
|
line('Usage: ' . __FILE__ . ' ' . $argvParser->getOptionsHelp());
|
|
exit(1);
|
|
}
|
|
|
|
$command = $parsed->get('c');
|
|
$id = $parsed->get('id');
|
|
$file = $parsed->get('f');
|
|
$apiKey = $parsed->get('a');
|
|
$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');
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$baseUrl = 'http://snips.symfony.localhost/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);
|
|
}
|
|
$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);
|
|
}
|