Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Stark committed Sep 24, 2021
1 parent 8d5ff8f commit 859d881
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
8 changes: 5 additions & 3 deletions bin/craft-copy-import-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@
$process = \Symfony\Component\Process\Process::fromShellCommandline($cmd);
$process->run();

unlink($credentialsFile);

if ($stderr = $process->getErrorOutput()) {
echo 'ERROR (sql):' . PHP_EOL;
echo substr($stderr, 0, 200);
fwrite(STDERR, 'ERROR (sql):' . PHP_EOL);
fwrite(STDERR, substr($stderr, 0, 200));
exit(1);
}

Expand All @@ -83,5 +85,5 @@
exit(0);
}

echo 'ERROR (unknown)';
fwrite(STDERR, 'ERROR (unknown)');
exit(1);
56 changes: 31 additions & 25 deletions src/Actions/DbUpAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ public function run(?string $stage = null)

if ($plugin->ssh->exec('ls vendor/bin/craft-copy-import-db.php | wc -l')) {
if (trim($plugin->ssh->getOutput()) !== '1') {
$this->errorBlock(
[
'Unable to import database. Deploy code first using this command:',
'php craft copy/code/up',
]
);
return ExitCode::UNSPECIFIED_ERROR;
return $this->printAndExit(new PluginNotInstalledException());
}
}

Expand Down Expand Up @@ -92,12 +86,10 @@ public function run(?string $stage = null)
$plugin->ssh->exec(
"php vendor/bin/craft-copy-import-db.php {$transferFile} --force"
);

$bar->advance();
$bar->setMessage('Database imported');
} catch (RemoteException $e) {
$this->errorBlock([$e->getMessage()]);
return ExitCode::UNSPECIFIED_ERROR;
return $this->printAndExit($e);
}
} else {
// Step 3: Backup the remote database before importing the uploaded dump
Expand All @@ -108,21 +100,8 @@ public function run(?string $stage = null)
try {
$plugin->ssh->exec("php craft copy/db/to-file {$backupFile} --interactive=0");
$bar->advance();
} catch (CraftNotInstalledException $e) {
$this->errorBlock(
[
'Unable to import database. Deploy code first using this command:',
'php craft copy/code/up',
]
);
return ExitCode::UNSPECIFIED_ERROR;
} catch (PluginNotInstalledException $e) {
$this->errorBlock(
[
'The plugin seems not to be installed on fortrabbit. Deploy code first using this command:',
'php craft copy/code/up',
]
);
} catch (RemoteException $e) {
return $this->printAndExit($e);
}

// Step 4: Import on remote
Expand All @@ -147,4 +126,31 @@ public function run(?string $stage = null)

return ExitCode::OK;
}

protected function printAndExit(RemoteException $exception): int
{
if ($exception instanceof CraftNotInstalledException) {
$this->errorBlock(
[
'Unable to import database. Deploy code first using this command:',
'php craft copy/code/up',
]
);
return ExitCode::UNSPECIFIED_ERROR;
}

if ($exception instanceof PluginNotInstalledException) {
$this->errorBlock(
[
'The plugin seems not to be installed on fortrabbit. Deploy code first using this command:',
'php craft copy/code/up',
]
);
return ExitCode::UNSPECIFIED_ERROR;
}

$this->errorBlock([$exception->getMessage()]);

return ExitCode::UNSPECIFIED_ERROR;
}
}
17 changes: 17 additions & 0 deletions src/Exceptions/RemoteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@

namespace fortrabbit\Copy\Exceptions;

use Throwable;
use yii\base\Exception;

class RemoteException extends Exception
{
public function __construct($message = '', $code = 0, ?Throwable $previous = null)
{
parent::__construct($this->cleanMessage($message), $code, $previous);
}

protected function cleanMessage(string $message): string
{
// remove double new lines
$message = preg_replace("/[\r\n]+/", "\n", $message);

// strip after ∙ƒ
if ($endPos = strpos($message, '∙ƒ')) {
$message = substr($message, 0, $endPos);
}
return trim($message);
}
}
4 changes: 3 additions & 1 deletion src/Services/Ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public function exec(string $cmd)
}

if (trim($process->getErrorOutput()) === 'Could not open input file') {
throw new CraftNotInstalledException(trim($process->getErrorOutput()));
throw new CraftNotInstalledException(
trim($process->getErrorOutput())
);
}

if (stristr($process->getErrorOutput(), 'Unknown command')) {
Expand Down

0 comments on commit 859d881

Please sign in to comment.