Improvements to test.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2023-06-15 10:15:44 +00:00
parent 78c1ea0326
commit 3efa791d5e
7 changed files with 112 additions and 42 deletions

View File

@ -62,17 +62,13 @@ class CommandBackup extends Command
$template['rclone_version'] = $rclone->getVersion();
$template['destination_size_before'] = $rclone->getSize($conf['destination']);
$template['stdout'] = $rclone->copy(
$conf['source'],
$conf['destination'],
$app->getConfig()['rclone']['options']
);
$rclone->copy($conf['source'], $conf['destination'], $app->getConfig()['rclone']['options']);
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
$template['end'] = new DateTime();
$message = $twig->render('notify', $template);
} catch (\Throwable $e) {
} catch (\Exception $e) {
$message = $e->getMessage();
}

View File

@ -43,13 +43,13 @@ class Rclone
$this->rclonePath = $rclonePath;
$this->setLogger(new NullLogger());
try {
$version = $this->exec('--version');
$this->version = explode("\n", $version)[0];
} catch (ProcessFailedException $e) {
$process = $this->exec('--version');
if (!$process->isSuccessful()) {
throw new Exception("Check installation of rclone");
}
$this->version = explode("\n", $process->getOutput())[0];
if (!\str_contains($this->version, 'rclone')) {
throw new Exception("Rclone not recognized");
}
@ -74,8 +74,12 @@ class Rclone
*/
public function getSize(string $path): int
{
$output = $this->exec('size', ['--json', $path]);
return (int)json_decode($output)->bytes;
$process = $this->exec('size', ['--json', $path]);
if (!$process->isSuccessful()) {
throw new Exception($process->getErrorOutput());
}
return (int)json_decode($process->getOutput())->bytes;
}
/**
@ -84,10 +88,8 @@ class Rclone
* @param $src Source mount and path
* @param $dest Destination mount and path
* @param string[] $additionalOptions Additional options
*
* @return string Stdout from command
*/
public function copy(string $src, string $dest, array $additionalOptions = array()): string
public function copy(string $src, string $dest, array $additionalOptions = array()): void
{
$options = array();
@ -95,16 +97,14 @@ class Rclone
$options[] = $dest;
foreach ($additionalOptions as $key => $value) {
if (strlen($key) == 1) {
$options[] = '-' . $key;
$options[] = $value;
} elseif (strlen($key) > 2) {
$options[] = '--' . $key;
$options[] = $value;
}
$options[] = '--' . $key;
$options[] = $value;
}
return $this->exec('copy', $options);
$process = $this->exec('copy', $options);
if (!$process->isSuccessful()) {
throw new Exception($process->getErrorOutput());
}
}
/**
@ -113,9 +113,9 @@ class Rclone
* @param string $command Top level Rclone command
* @param array<String> $options Array of additional options
*
* @return string stdout data.
* @return Process Instance.
*/
protected function exec(string $command, array $options = array()): string
protected function exec(string $command, array $options = array()): Process
{
$process = new Process(
array_merge(
@ -137,11 +137,10 @@ class Rclone
if ($this->logger instanceof LoggerInterface) {
$this->logger->error("Failed execution");
}
throw new ProcessFailedException($process);
}
if ($this->logger instanceof LoggerInterface) {
$this->logger->info("Return code", [$process->getExitCode()]);
}
return $process->getOutput();
return $process;
}
}