CLI: write errors to stderr

This commit is contained in:
Alexander A. Klimov 2020-11-24 16:47:59 +01:00 committed by Johannes Meyer
parent 648e07d270
commit d7e3547c6e
4 changed files with 18 additions and 16 deletions

View File

@ -27,7 +27,7 @@ class StderrWriter extends LogWriter
protected function screen() protected function screen()
{ {
if ($this->screen === null) { if ($this->screen === null) {
$this->screen = Screen::instance(); $this->screen = Screen::instance(STDERR);
} }
return $this->screen; return $this->screen;

View File

@ -55,7 +55,7 @@ abstract class Command
$this->commandName = $commandName; $this->commandName = $commandName;
$this->actionName = $actionName; $this->actionName = $actionName;
$this->params = $app->getParams(); $this->params = $app->getParams();
$this->screen = Screen::instance($app); $this->screen = Screen::instance();
$this->trace = $this->params->shift('trace', false); $this->trace = $this->params->shift('trace', false);
$this->isVerbose = $this->params->shift('verbose', false); $this->isVerbose = $this->params->shift('verbose', false);
$this->isDebugging = $this->params->shift('debug', false); $this->isDebugging = $this->params->shift('debug', false);

View File

@ -103,7 +103,7 @@ class Loader
*/ */
public function fail($msg) public function fail($msg)
{ {
printf("%s: %s\n", $this->screen()->colorize('ERROR', 'red'), $msg); fprintf(STDERR, "%s: %s\n", $this->screen()->colorize('ERROR', 'red'), $msg);
exit(1); exit(1);
} }
@ -173,7 +173,8 @@ class Loader
foreach ($this->lastSuggestions as & $s) { foreach ($this->lastSuggestions as & $s) {
$s = $this->screen()->colorize($s, 'lightblue'); $s = $this->screen()->colorize($s, 'lightblue');
} }
printf( fprintf(
STDERR,
"Did you mean %s?\n", "Did you mean %s?\n",
implode(" or ", $this->lastSuggestions) implode(" or ", $this->lastSuggestions)
); );
@ -197,9 +198,9 @@ class Loader
} }
if (! $found) { if (! $found) {
$msg = "There is no such module or command: '$first'"; $msg = "There is no such module or command: '$first'";
printf("%s: %s\n", $this->screen()->colorize('ERROR', 'red'), $msg); fprintf(STDERR, "%s: %s\n", $this->screen()->colorize('ERROR', 'red'), $msg);
$this->showLastSuggestions(); $this->showLastSuggestions();
echo "\n"; fwrite(STDERR, "\n");
} }
$obj = null; $obj = null;
@ -241,10 +242,10 @@ class Loader
public function dispatch(Params $overrideParams = null) public function dispatch(Params $overrideParams = null)
{ {
if ($this->commandName === null) { if ($this->commandName === null) {
echo $this->docs()->usage($this->moduleName); fwrite(STDERR, $this->docs()->usage($this->moduleName));
return false; return false;
} elseif ($this->actionName === null) { } elseif ($this->actionName === null) {
echo $this->docs()->usage($this->moduleName, $this->commandName); fwrite(STDERR, $this->docs()->usage($this->moduleName, $this->commandName));
return false; return false;
} }
@ -265,7 +266,7 @@ class Loader
return $obj->{$this->actionName . 'Action'}(); return $obj->{$this->actionName . 'Action'}();
} catch (Exception $e) { } catch (Exception $e) {
if ($obj && $obj instanceof Command && $obj->showTrace()) { if ($obj && $obj instanceof Command && $obj->showTrace()) {
echo $this->formatTrace($e->getTrace()); fwrite(STDERR, $this->formatTrace($e->getTrace()));
} }
$this->fail(IcingaException::describe($e)); $this->fail(IcingaException::describe($e));

View File

@ -7,7 +7,7 @@ use Icinga\Cli\AnsiScreen;
class Screen class Screen
{ {
protected static $instance; protected static $instances = [];
protected $isUtf8; protected $isUtf8;
@ -91,15 +91,16 @@ class Screen
return $text; return $text;
} }
public static function instance() public static function instance($output = STDOUT)
{ {
if (self::$instance === null) { if (! isset(self::$instances[(int) $output])) {
if (function_exists('posix_isatty') && posix_isatty(STDOUT)) { if (function_exists('posix_isatty') && posix_isatty($output)) {
self::$instance = new AnsiScreen(); self::$instances[(int) $output] = new AnsiScreen();
} else { } else {
self::$instance = new Screen(); self::$instances[(int) $output] = new Screen();
} }
} }
return self::$instance;
return self::$instances[(int) $output];
} }
} }