More codestandards.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Jens True 2023-06-12 09:30:10 +00:00
parent d0270b00ca
commit 0f3bbf1f47
8 changed files with 124 additions and 42 deletions

24
composer.lock generated

@ -1528,16 +1528,16 @@
},
{
"name": "twig/twig",
"version": "v3.6.0",
"version": "v3.6.1",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "106c170d08e8415d78be2d16c3d057d0d108262b"
"reference": "7e7d5839d4bec168dfeef0ac66d5c5a2edbabffd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/106c170d08e8415d78be2d16c3d057d0d108262b",
"reference": "106c170d08e8415d78be2d16c3d057d0d108262b",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/7e7d5839d4bec168dfeef0ac66d5c5a2edbabffd",
"reference": "7e7d5839d4bec168dfeef0ac66d5c5a2edbabffd",
"shasum": ""
},
"require": {
@ -1583,7 +1583,7 @@
],
"support": {
"issues": "https://github.com/twigphp/Twig/issues",
"source": "https://github.com/twigphp/Twig/tree/v3.6.0"
"source": "https://github.com/twigphp/Twig/tree/v3.6.1"
},
"funding": [
{
@ -1595,7 +1595,7 @@
"type": "tidelift"
}
],
"time": "2023-05-03T19:06:57+00:00"
"time": "2023-06-08T12:52:13+00:00"
},
{
"name": "verifiedjoseph/ntfy-php-library",
@ -3304,16 +3304,16 @@
},
{
"name": "phpunit/phpunit",
"version": "10.2.1",
"version": "10.2.2",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "599b33294350e8f51163119d5670512f98b0490d"
"reference": "1ab521b24b88b88310c40c26c0cc4a94ba40ff95"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/599b33294350e8f51163119d5670512f98b0490d",
"reference": "599b33294350e8f51163119d5670512f98b0490d",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1ab521b24b88b88310c40c26c0cc4a94ba40ff95",
"reference": "1ab521b24b88b88310c40c26c0cc4a94ba40ff95",
"shasum": ""
},
"require": {
@ -3385,7 +3385,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.2.1"
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.2.2"
},
"funding": [
{
@ -3401,7 +3401,7 @@
"type": "tidelift"
}
],
"time": "2023-06-05T05:15:51+00:00"
"time": "2023-06-11T06:15:20+00:00"
},
{
"name": "sebastian/cli-parser",

@ -1,8 +1,10 @@
notification:
Ntfy:
domain: https://ntfy.jcktrue.dk
topic: backup
log: output.log
rclone:
options:
bwlimit: 6M
backup:
- title: Example

@ -2,6 +2,7 @@
namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
@ -10,15 +11,15 @@ use Symfony\Component\Console\Style\SymfonyStyle;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use DateTime;
use Ntfy\Server;
use Ntfy\Message;
use Ntfy\Client;
use App\Notification\Notification;
use App\Rclone\Rclone;
#[AsCommand(
name: 'backup',
description: 'Start backup to assigned buckets',
)]
class CommandBackup extends Command
{
public static $defaultName = "backup";
public static $defaultDescription = "Start backup to assigned buckets";
protected function configure(): void
{
$this->addArgument(
@ -41,39 +42,38 @@ class CommandBackup extends Command
return Command::FAILURE;
}
$rclone = new Rclone\Rclone();
$rclone = new Rclone();
$rclone->setLogger($app->getLogger()->withName('rclone'));
$server = new Server($app->getConfig()['notification']['domain']);
$client = new Client($server);
$notification = new Notification();
$notification->loadMany($app->getConfig()['notification']);
$loader = new ArrayLoader($app->getConfig()['templates']);
$twig = new Environment($loader);
$twig->addExtension(new Twig\AppExtension());
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
$message = new Message();
$message->topic($app->getConfig()['notification']['topic']);
$message->title($conf['title']);
$title = $conf['title'];
$message = "";
try {
$template = array();
$template['config'] = $conf;
$template['start'] = new DateTime();
$template['source_size'] = $rclone->getSize($conf['source']);
$template['rclone_version'] = $rclone->getVersion();
$template['destination_size_before'] = $rclone->getSize($conf['destination']);
$rclone->copy($conf['source'], $conf['destination'], "6M");
$template['stdout'] = $rclone->copy($conf['source'], $conf['destination'], $app->getConfig()['rclone']['options']);
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
$template['end'] = new DateTime();
$message->body($twig->render('notify', $template));
$message = $twig->render('notify', $template);
} catch (\Throwable $e) {
$message->body($e->getMessage());
$message->tags(["warning"]);
$message = $e->getMessage();
}
$client->send($message);
$notification->send($title, $message);
}
$sio->success("Complete");

@ -2,17 +2,19 @@
namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'show',
description: 'Show all backup entries.',
)]
class CommandShow extends Command
{
public static $defaultName = "show";
public static $defaultDescription = "Show all backup entries";
protected function configure(): void
{
$this->addArgument(

@ -0,0 +1,31 @@
<?php
namespace App\Notification;
class Notification
{
/**
* @type NotificationInterface[] $notifiers
*/
public array $notifiers = array();
public function loadMany(array $config): void
{
foreach ($config as $key => $conf) {
$this->loadSingle($key, $conf);
}
}
public function loadSingle(string $key, array $config): void
{
$class = "\App\Notification\\" . $key;
$this->notifiers[$key] = new $class($config);
}
public function send(string $title, string $message): void
{
foreach ($this->notifiers as $notifier) {
$notifier->send($title, $message);
}
}
}

@ -0,0 +1,8 @@
<?php
namespace App\Notification;
interface NotificationInterface
{
public function send(string $title, string $message): void;
}

32
src/Notification/Ntfy.php Normal file

@ -0,0 +1,32 @@
<?php
namespace App\Notification;
use Ntfy\Server;
use Ntfy\Message;
use Ntfy\Client;
class Ntfy implements NotificationInterface
{
private array $config;
private \Ntfy\Server $server;
private \Ntfy\Client $client;
public function __construct(array $config)
{
$this->config = $config;
$this->server = new Server($config['domain']);
$this->client = new Client($this->server);
}
public function send(string $title, string $message): void
{
$msg = new Message();
$msg->topic($this->config['topic']);
$msg->title($title);
$msg->body($message);
$this->client->send($msg);
}
}

@ -83,19 +83,26 @@ class Rclone
*
* @param $src Source mount and path
* @param $dest Destination mount and path
* @param $bandwidth Bandwidth limit provided as string
* @param $additionalOptions strings[] Bandwidth limit provided as string
*
* @return string Stdout from command
*/
public function copy(string $src, string $dest, string $bandwidth = null): string
public function copy(string $src, string $dest, array $additionalOptions = array()): string
{
$options = array();
$options[] = $src;
$options[] = $dest;
if ($bandwidth) {
$options[] = "--bwlimit";
$options[] = $bandwidth;
foreach ($additionalOptions as $key => $value) {
if (strlen($key) == 1) {
$options[] = '-' . $key;
$options[] = $value;
} elseif (strlen($key) > 2) {
$options[] = '--' . $key;
$options[] = $value;
} else {
$options[] = $value;
}
}
return $this->exec('copy', $options);