From 0f3bbf1f47bf01d049b0630084a3e1bfc3afbd4c Mon Sep 17 00:00:00 2001 From: Jens True Date: Mon, 12 Jun 2023 09:30:10 +0000 Subject: [PATCH] More codestandards. --- composer.lock | 24 +++++++-------- config.example.yml | 8 +++-- src/CommandBackup.php | 34 +++++++++++----------- src/CommandShow.php | 8 +++-- src/Notification/Notification.php | 31 ++++++++++++++++++++ src/Notification/NotificationInterface.php | 8 +++++ src/Notification/Ntfy.php | 32 ++++++++++++++++++++ src/Rclone/Rclone.php | 21 ++++++++----- 8 files changed, 124 insertions(+), 42 deletions(-) create mode 100644 src/Notification/Notification.php create mode 100644 src/Notification/NotificationInterface.php create mode 100644 src/Notification/Ntfy.php diff --git a/composer.lock b/composer.lock index ebb1003..50bb8cb 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config.example.yml b/config.example.yml index 9ea8f84..9f41f19 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,9 +1,11 @@ notification: - domain: https://ntfy.jcktrue.dk - topic: backup + Ntfy: + domain: https://ntfy.jcktrue.dk + topic: backup log: output.log rclone: - bwlimit: 6M + options: + bwlimit: 6M backup: - title: Example source: temp/source diff --git a/src/CommandBackup.php b/src/CommandBackup.php index e10fe9a..68fbd5a 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -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"); diff --git a/src/CommandShow.php b/src/CommandShow.php index f8c7232..921ad2b 100644 --- a/src/CommandShow.php +++ b/src/CommandShow.php @@ -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( diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php new file mode 100644 index 0000000..2443a2f --- /dev/null +++ b/src/Notification/Notification.php @@ -0,0 +1,31 @@ + $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); + } + } +} diff --git a/src/Notification/NotificationInterface.php b/src/Notification/NotificationInterface.php new file mode 100644 index 0000000..7180e71 --- /dev/null +++ b/src/Notification/NotificationInterface.php @@ -0,0 +1,8 @@ +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); + } +} diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index f5ba240..2c2f843 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -81,21 +81,28 @@ class Rclone /** * Copy from source to destination. * - * @param $src Source mount and path - * @param $dest Destination mount and path - * @param $bandwidth Bandwidth limit provided as string + * @param $src Source mount and path + * @param $dest Destination mount and path + * @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);