Skip to content

Commit

Permalink
Add option for disabling shorten Closes dg#110
Browse files Browse the repository at this point in the history
  • Loading branch information
arxeiss committed Aug 18, 2018
1 parent 7a7036a commit a685081
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion src/Deployment/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Deployment/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 26 additions & 2 deletions src/Deployment/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Logger
/** @var resource */
private $file;

/** @var array */
public $fullCliLog = [];

/** @var array */
private $colors = [
'black' => '0;30',
Expand Down Expand Up @@ -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";
Expand All @@ -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;
}
}

0 comments on commit a685081

Please sign in to comment.