PSR12 code standard.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
16
Makefile
16
Makefile
@ -1,12 +1,14 @@
|
||||
analyze-all:
|
||||
@echo PHPMessDetector
|
||||
analyze: analyze-phpmd analyze-phpstan analyze-psalm analyze-phpcs
|
||||
|
||||
analyze-phpmd:
|
||||
-./vendor/bin/phpmd src text cleancode,codesize,controversial,design,naming,unusedcode
|
||||
@echo PHPStan
|
||||
-./vendor/bin/phpstan analyze --level=7 src/ backup
|
||||
@echo Psalm
|
||||
analyze-phpstan:
|
||||
-./vendor/bin/phpstan analyze --level=7 --error-format=raw src/ backup
|
||||
analyze-psalm:
|
||||
-./vendor/bin/psalm
|
||||
@echo PHP_CodeSniffer
|
||||
-./vendor/bin/phpcs src
|
||||
analyze-phpcs:
|
||||
-./vendor/bin/phpcs src backup --report=emacs --standard=PSR12
|
||||
|
||||
install:
|
||||
php composer.phar install --no-dev
|
||||
install-dev:
|
||||
|
10
composer.lock
generated
10
composer.lock
generated
@ -2922,16 +2922,16 @@
|
||||
},
|
||||
{
|
||||
"name": "phpstan/phpstan",
|
||||
"version": "1.10.16",
|
||||
"version": "1.10.18",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/phpstan/phpstan.git",
|
||||
"reference": "352bdbb960bb523e3d71b834862589f910921c23"
|
||||
"reference": "52b6416c579663eebdd2f1d97df21971daf3b43f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/352bdbb960bb523e3d71b834862589f910921c23",
|
||||
"reference": "352bdbb960bb523e3d71b834862589f910921c23",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/52b6416c579663eebdd2f1d97df21971daf3b43f",
|
||||
"reference": "52b6416c579663eebdd2f1d97df21971daf3b43f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -2980,7 +2980,7 @@
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-06-05T08:21:46+00:00"
|
||||
"time": "2023-06-07T22:00:43+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpunit/php-code-coverage",
|
||||
|
@ -6,6 +6,7 @@
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||
findUnusedBaselineEntry="true"
|
||||
findUnusedCode="true"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="src/" />
|
||||
|
30
src/App.php
30
src/App.php
@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* Application class
|
||||
*
|
||||
*
|
||||
* Mostly working as a register pattern for the logging and configuration.
|
||||
*
|
||||
*
|
||||
* @author Jens True <jens.chr.true@gmail.com>
|
||||
* @license https://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @link https://jcktrue.dks
|
||||
@ -20,43 +20,43 @@ use Psr\Log\NullLogger;
|
||||
class App
|
||||
{
|
||||
protected Logger $logger;
|
||||
protected array $config;
|
||||
protected mixed $config;
|
||||
|
||||
/**
|
||||
* Create a new instance providing a config file
|
||||
*
|
||||
*
|
||||
* @param string $configFile Relative or full path to YML config.
|
||||
*/
|
||||
function __construct(string $configFile)
|
||||
public function __construct(string $configFile)
|
||||
{
|
||||
$this->config = Yaml::parseFile($configFile);
|
||||
$parser = new Yaml();
|
||||
$this->config = $parser->parseFile($configFile);
|
||||
$logger = new Logger('app');
|
||||
if (isset($this->config['log'])) {
|
||||
$logger->pushHandler(new StreamHandler($this->getConfig()['log']));
|
||||
}
|
||||
$logger->info("Initialization complete");
|
||||
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full configuration
|
||||
*
|
||||
* @return array Full configuration structure
|
||||
*
|
||||
* @return mixed Full configuration structure
|
||||
*/
|
||||
function getConfig() : array
|
||||
public function getConfig(): mixed
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the logger instance.
|
||||
*
|
||||
* Get the logger instance.
|
||||
*
|
||||
* @return Logger Instance of logger
|
||||
*/
|
||||
function getLogger() : Logger
|
||||
public function getLogger(): Logger
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,52 +1,46 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
|
||||
use DateTime;
|
||||
|
||||
use Ntfy\Server;
|
||||
use Ntfy\Message;
|
||||
use Ntfy\Client;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'backup',
|
||||
description: 'Start backup to assigned buckets.',
|
||||
hidden: false,
|
||||
)]
|
||||
class CommandBackup extends Command
|
||||
{
|
||||
static $defaultName = "backup";
|
||||
static $defaultDescription = "Start backup to assigned buckets";
|
||||
public static $defaultName = "backup";
|
||||
public static $defaultDescription = "Start backup to assigned buckets";
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml");
|
||||
$this->addArgument(
|
||||
'config',
|
||||
InputArgument::OPTIONAL,
|
||||
'Configuration file',
|
||||
"config.yml"
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Start backup process');
|
||||
|
||||
$sio = new SymfonyStyle($input, $output);
|
||||
$sio->title('Start backup process');
|
||||
|
||||
try {
|
||||
$app = new App($input->getArgument('config'));
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
$sio->error('Configuration error:' . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$rclone = new Rclone\Rclone();
|
||||
$rclone->setLogger($app->getLogger()->withName('rclone'));
|
||||
|
||||
@ -57,7 +51,7 @@ class CommandBackup extends Command
|
||||
$twig = new Environment($loader);
|
||||
$twig->addExtension(new Twig\AppExtension());
|
||||
|
||||
foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {
|
||||
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
|
||||
$message = new Message();
|
||||
$message->topic($app->getConfig()['notification']['topic']);
|
||||
$message->title($conf['title']);
|
||||
@ -73,7 +67,7 @@ class CommandBackup extends Command
|
||||
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
|
||||
$template['end'] = new DateTime();
|
||||
|
||||
$message->body($twig->render('notify', $template));
|
||||
$message->body($twig->render('notify', $template));
|
||||
} catch (\Throwable $e) {
|
||||
$message->body($e->getMessage());
|
||||
$message->tags(["warning"]);
|
||||
@ -82,7 +76,7 @@ class CommandBackup extends Command
|
||||
$client->send($message);
|
||||
}
|
||||
|
||||
$io->success("Complete");
|
||||
$sio->success("Complete");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +1,46 @@
|
||||
<?php
|
||||
|
||||
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.',
|
||||
hidden: false,
|
||||
)]
|
||||
class CommandShow extends Command
|
||||
{
|
||||
static $defaultName = "show";
|
||||
static $defaultDescription = "Show all backup entries";
|
||||
public static $defaultName = "show";
|
||||
public static $defaultDescription = "Show all backup entries";
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml");
|
||||
$this->addArgument(
|
||||
'config',
|
||||
InputArgument::OPTIONAL,
|
||||
'Configuration file',
|
||||
"config.yml"
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('List backup entities');
|
||||
$sio = new SymfonyStyle($input, $output);
|
||||
$sio->title('List backup entities');
|
||||
|
||||
try {
|
||||
$app = new App($input->getArgument('config'));
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
$sio->error('Configuration error: ' . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->table(['Description', 'Source', 'Destination'], $app->getConfig()['backup']);
|
||||
$sio->table(
|
||||
['Description', 'Source', 'Destination'],
|
||||
$app->getConfig()['backup']
|
||||
);
|
||||
|
||||
$io->success("Done");
|
||||
$sio->success("Done");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rclone;
|
||||
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Wrapper for the rclone command line utility
|
||||
*
|
||||
*
|
||||
* Installation of rclone is required.
|
||||
* Configuration of the mounts must be done before use.
|
||||
* Tested using rclone v1.53.3-DEV
|
||||
@ -24,76 +23,72 @@ class Rclone
|
||||
protected string $rclonePath;
|
||||
/**
|
||||
* Global options
|
||||
*
|
||||
* @var array<string>
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected array $globalOptions = [];
|
||||
|
||||
protected string $version = "";
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
*
|
||||
* Create a new instance.
|
||||
*
|
||||
* Default it looks for "rclone" on the path.
|
||||
* But the path can be configured to be absolute.
|
||||
*
|
||||
*
|
||||
* @param string $rclonePath Relative or absolute path
|
||||
*/
|
||||
function __construct(string $rclonePath = "rclone")
|
||||
public function __construct(string $rclonePath = "rclone")
|
||||
{
|
||||
$this->rclonePath = $rclonePath;
|
||||
$this->setLogger(new NullLogger);
|
||||
try
|
||||
{
|
||||
$this->setLogger(new NullLogger());
|
||||
|
||||
try {
|
||||
$version = $this->exec('--version');
|
||||
$this->version = explode("\n", $version)[0];
|
||||
}
|
||||
catch(ProcessFailedException $e)
|
||||
{
|
||||
} catch (ProcessFailedException $e) {
|
||||
throw new Exception("Check installation of rclone");
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!\str_contains($this->version, 'rclone')) {
|
||||
throw new Exception("Rclone not recognized");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rclone version
|
||||
*
|
||||
* Get the rclone version.
|
||||
*
|
||||
* @return string Version string
|
||||
*/
|
||||
function getVersion(): string
|
||||
public function getVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the size of a mount/path
|
||||
*
|
||||
* Calculate the size of a mount/path.
|
||||
*
|
||||
* @param string $path mount/path.
|
||||
*
|
||||
*
|
||||
* @return int Size in bytes
|
||||
*/
|
||||
function getSize(string $path): int
|
||||
public function getSize(string $path): int
|
||||
{
|
||||
$output = $this->exec('size', ['--json', $path]);
|
||||
return (int)json_decode($output)->bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from src to dest
|
||||
*
|
||||
* Copy from source to destination.
|
||||
*
|
||||
* @param $src Source mount and path
|
||||
* @param $dest Destination mount and path
|
||||
* @param $bandwidth Bandwidth limit provided as string
|
||||
*
|
||||
*
|
||||
* @return string Stdout from command
|
||||
*/
|
||||
function copy(string $src, string $dest, string $bandwidth = null): string
|
||||
{
|
||||
public function copy(string $src, string $dest, string $bandwidth = null): string
|
||||
{
|
||||
$options = array();
|
||||
|
||||
$options[] = $src;
|
||||
@ -107,14 +102,14 @@ class Rclone
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a command on the rclone binary
|
||||
*
|
||||
* Execute a command on the rclone binary.
|
||||
*
|
||||
* @param string $command Top level Rclone command
|
||||
* @param array<String> $options Array of additional options
|
||||
*
|
||||
*
|
||||
* @return string stdout data.
|
||||
*/
|
||||
protected function exec(string $command, array $options = array()) : string
|
||||
protected function exec(string $command, array $options = array()): string
|
||||
{
|
||||
$process = new Process(
|
||||
array_merge(
|
||||
@ -127,8 +122,8 @@ class Rclone
|
||||
if ($this->logger instanceof LoggerInterface) {
|
||||
$this->logger->info("Execute command", [$process->getCommandLine()]);
|
||||
}
|
||||
|
||||
$process->setTimeout(4*3600);
|
||||
|
||||
$process->setTimeout(4 * 3600);
|
||||
$process->run();
|
||||
|
||||
// executes after the command finishes
|
||||
@ -143,4 +138,4 @@ class Rclone
|
||||
}
|
||||
return $process->getOutput();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
* Twig extension
|
||||
*
|
||||
*
|
||||
* Additional formatters for templates
|
||||
*
|
||||
*
|
||||
* @author Jens True <jens.chr.true@gmail.com>
|
||||
* @license https://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @link https://jcktrue.dk
|
||||
@ -19,10 +19,10 @@ class AppExtension extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* Extend the filters
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return TwigFilter[]
|
||||
*/
|
||||
public function getFilters() : array
|
||||
public function getFilters(): array
|
||||
{
|
||||
return array(
|
||||
new TwigFilter('formatBytes', array($this, 'formatBytes')),
|
||||
@ -31,10 +31,10 @@ class AppExtension extends AbstractExtension
|
||||
|
||||
/**
|
||||
* Format a file size to be human readable
|
||||
*
|
||||
*
|
||||
* @param int $bytes Number of bytes
|
||||
* @param int $precision Precision
|
||||
*
|
||||
*
|
||||
* @return string Formatted string
|
||||
*/
|
||||
public function formatBytes($bytes, $precision = 2)
|
||||
@ -43,5 +43,4 @@ class AppExtension extends AbstractExtension
|
||||
$fact = (int)(floor((strlen((string)$bytes) - 1) / 3));
|
||||
return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user