From a685081af3a75c4281de143a88dfe76cc8333ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Kut=C3=A1=C4=8D?= Date: Sat, 18 Aug 2018 10:42:23 +0200 Subject: [PATCH 1/2] Add option for disabling shorten Closes #110 --- readme.md | 4 ++++ src/Deployment/CliRunner.php | 9 ++++++++- src/Deployment/Deployer.php | 2 +- src/Deployment/Logger.php | 28 ++++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 9d6d0a9a..31c066ee 100644 --- a/readme.md +++ b/readme.md @@ -31,6 +31,10 @@ And what does the `deployment.ini` file contain? **Only the `remote` item is req ; log file (defaults to config file with .log extension) log = ... +; Show full log in command line (Defaults to no) +; Yes to disable shortening or list any of: http, local, remote, upload, exception +fullCliLog = http exception + ; directory for temporary files (defaults to system's temporary directory) tempDir = /temp/deployment diff --git a/src/Deployment/CliRunner.php b/src/Deployment/CliRunner.php index cbdf6704..8615eeeb 100644 --- a/src/Deployment/CliRunner.php +++ b/src/Deployment/CliRunner.php @@ -62,6 +62,7 @@ public function run(): ?int $this->logger = new Logger($config['log']); $this->logger->useColors = (bool) $config['colors']; $this->logger->showProgress = (bool) $config['progress']; + $this->logger->fullCliLog = $config['fullclilog']; if (!is_dir($tempDir = $config['tempdir'])) { $this->logger->log("Creating temporary directory $tempDir"); @@ -185,7 +186,7 @@ private function setupPhp(): void }); set_exception_handler(function ($e) { - $this->logger->log("Error: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n\n$e", 'red'); + $this->logger->log("Error: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n\n$e", 'red', $this->logger->shortenFor("exception")); exit(1); }); } @@ -249,12 +250,18 @@ private function loadConfig(): ?array $config = array_change_key_case($config, CASE_LOWER) + [ 'log' => preg_replace('#\.\w+$#', '.log', $this->configFile), + 'fullclilog' => [], 'tempdir' => sys_get_temp_dir() . '/deployment', 'progress' => true, 'colors' => (PHP_SAPI === 'cli' && ((function_exists('posix_isatty') && posix_isatty(STDOUT)) || getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== false)), ]; $config['progress'] = $options['--no-progress'] ? false : $config['progress']; + if (is_string($config['fullclilog'])) { + $config['fullclilog'] = $config['fullclilog'] === "1" + ? [ "*" ] + : preg_split("/[\s,]+/", $config['fullclilog']); + } return $config; } diff --git a/src/Deployment/Deployer.php b/src/Deployment/Deployer.php index bf2f8b00..311dd4ac 100644 --- a/src/Deployment/Deployer.php +++ b/src/Deployment/Deployer.php @@ -406,7 +406,7 @@ private function runJobs(array $jobs): void } if ($out != null) { // intentionally == - $this->logger->log("-> $out", 'gray'); + $this->logger->log("-> $out", 'gray', $this->logger->shortenFor($m[1])); } if ($err) { throw new \RuntimeException('Job failed, ' . $err); diff --git a/src/Deployment/Logger.php b/src/Deployment/Logger.php index 4a51dc71..321037bc 100644 --- a/src/Deployment/Logger.php +++ b/src/Deployment/Logger.php @@ -25,6 +25,9 @@ class Logger /** @var resource */ private $file; + /** @var array */ + public $fullCliLog = []; + /** @var array */ private $colors = [ 'black' => '0;30', @@ -52,11 +55,11 @@ public function __construct(string $file) } - public function log(string $s, string $color = null, bool $shorten = true): void + public function log(string $s, string $color = null, bool $shorten = null): void { fwrite($this->file, $s . "\n"); - if ($shorten && preg_match('#^\n?.*#', $s, $m)) { + if (($shorten ?? $this->shortenFor("*")) && preg_match('#^\n?.*#', $s, $m)) { $s = $m[0]; } $s .= " \n"; @@ -78,4 +81,25 @@ public function progress(string $message): void echo $message, "\x0D"; } } + + /** + * Check if given action should be shortened + * + * @param string $action + * @return bool + */ + public function shortenFor(string $action) + { + $aliases = [ + "https" => "http" + ]; + if (in_array("*", $this->fullCliLog)) { + return false; + } + if (isset($aliases[$action])) { + $action = $aliases[$action]; + } + // If action is present in list, return false to disable shortening + return in_array($action, $this->fullCliLog) ? false : true; + } } From 52f3a29731efbd5d92171c09f996bb92e3da5a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Kut=C3=A1=C4=8D?= Date: Tue, 9 Oct 2018 18:42:03 +0200 Subject: [PATCH 2/2] Fix coding standart --- src/Deployment/CliRunner.php | 8 ++++---- src/Deployment/Logger.php | 15 ++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Deployment/CliRunner.php b/src/Deployment/CliRunner.php index 8615eeeb..399d4d9e 100644 --- a/src/Deployment/CliRunner.php +++ b/src/Deployment/CliRunner.php @@ -186,7 +186,7 @@ private function setupPhp(): void }); set_exception_handler(function ($e) { - $this->logger->log("Error: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n\n$e", 'red', $this->logger->shortenFor("exception")); + $this->logger->log("Error: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n\n$e", 'red', $this->logger->shortenFor('exception')); exit(1); }); } @@ -258,9 +258,9 @@ private function loadConfig(): ?array ]; $config['progress'] = $options['--no-progress'] ? false : $config['progress']; if (is_string($config['fullclilog'])) { - $config['fullclilog'] = $config['fullclilog'] === "1" - ? [ "*" ] - : preg_split("/[\s,]+/", $config['fullclilog']); + $config['fullclilog'] = $config['fullclilog'] === '1' + ? ['*'] + : preg_split('/[\s,]+/', $config['fullclilog']); } return $config; } diff --git a/src/Deployment/Logger.php b/src/Deployment/Logger.php index 321037bc..a9ff8600 100644 --- a/src/Deployment/Logger.php +++ b/src/Deployment/Logger.php @@ -22,12 +22,12 @@ class Logger /** @var bool */ public $showProgress = true; - /** @var resource */ - private $file; - /** @var array */ public $fullCliLog = []; + /** @var resource */ + private $file; + /** @var array */ private $colors = [ 'black' => '0;30', @@ -59,7 +59,7 @@ public function log(string $s, string $color = null, bool $shorten = null): void { fwrite($this->file, $s . "\n"); - if (($shorten ?? $this->shortenFor("*")) && preg_match('#^\n?.*#', $s, $m)) { + if (($shorten ?? $this->shortenFor('*')) && preg_match('#^\n?.*#', $s, $m)) { $s = $m[0]; } $s .= " \n"; @@ -82,6 +82,7 @@ public function progress(string $message): void } } + /** * Check if given action should be shortened * @@ -91,15 +92,15 @@ public function progress(string $message): void public function shortenFor(string $action) { $aliases = [ - "https" => "http" + 'https' => 'http', ]; - if (in_array("*", $this->fullCliLog)) { + if (in_array('*', $this->fullCliLog, true)) { return false; } if (isset($aliases[$action])) { $action = $aliases[$action]; } // If action is present in list, return false to disable shortening - return in_array($action, $this->fullCliLog) ? false : true; + return in_array($action, $this->fullCliLog, true) ? false : true; } }