From 282375ad177cf2707e9f833f27d26db0aadb62d6 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 22 May 2019 18:37:26 +0200 Subject: [PATCH] #2783 --- .../Console/Commands/MagicCubistCommand.php | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/app/Console/Commands/MagicCubistCommand.php b/src/app/Console/Commands/MagicCubistCommand.php index 9425997..79394a2 100644 --- a/src/app/Console/Commands/MagicCubistCommand.php +++ b/src/app/Console/Commands/MagicCubistCommand.php @@ -3,12 +3,14 @@ namespace Cubist\Backpack\app\Console\Commands; -use Backpack\Base\app\Console\Commands\Install; use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; use Cubist\Util\Files\Files; use Cubist\Util\PHP; +use Illuminate\Console\Command; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Process; -class MagicCubistCommand extends Install +class MagicCubistCommand extends Command { /** * The name and signature of the console command. @@ -58,4 +60,54 @@ class MagicCubistCommand extends Install } } } + + /** + * Run a SSH command. + * + * @param string $command The SSH command that needs to be run + * @param bool $beforeNotice Information for the user before the command is run + * @param bool $afterNotice Information for the user after the command is run + * + * @return mixed Command-line output + */ + public function executeProcess($command, $beforeNotice = false, $afterNotice = false) + { + $this->echo('info', $beforeNotice ? ' '.$beforeNotice : $command); + + $process = new Process($command, null, null, null, $this->option('timeout'), null); + $process->run(function ($type, $buffer) { + if (Process::ERR === $type) { + $this->echo('comment', $buffer); + } else { + $this->echo('line', $buffer); + } + }); + + // executes after the command finishes + if (!$process->isSuccessful()) { + throw new ProcessFailedException($process); + } + + if ($afterNotice) { + $this->echo('info', $afterNotice); + } + } + + /** + * Write text to the screen for the user to see. + * + * @param [string] $type line, info, comment, question, error + * @param [string] $content + */ + public function echo($type, $content) + { + if ($this->option('debug') == false) { + return; + } + + // skip empty lines + if (trim($content)) { + $this->{$type}($content); + } + } } -- 2.39.5