Use a OutputFormatter instead of hacking into writeln()

This commit is contained in:
Joas Schilling 2015-06-23 17:07:28 +02:00
parent d8b9f6ac23
commit dba5d5e205
2 changed files with 133 additions and 49 deletions

View File

@ -25,6 +25,7 @@
namespace OC\Core\Command;
use OC\Console\TimestampFormatter;
use OC\Updater;
use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
@ -46,11 +47,6 @@ class Upgrade extends Command {
*/
private $config;
/**
* @var bool
*/
private $showTimestamp = false;
/**
* @param IConfig $config
*/
@ -80,12 +76,6 @@ class Upgrade extends Command {
null,
InputOption::VALUE_NONE,
'skips the disable of third party apps'
)
->addOption(
'--show-timestamp',
null,
InputOption::VALUE_NONE,
'show timestamp for each output during upgrade'
);
}
@ -110,9 +100,6 @@ class Upgrade extends Command {
if ($input->getOption('no-app-disable')) {
$skip3rdPartyAppsDisable = true;
}
if ($input->getOption('show-timestamp')) {
$this->showTimestamp = true;
}
if (!$simulateStepEnabled && !$updateStepEnabled) {
$output->writeln(
@ -123,6 +110,12 @@ class Upgrade extends Command {
}
if(\OC::checkUpgrade(false)) {
if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
// Prepend each line with a little timestamp
$timestampFormatter = new TimestampFormatter($this->config, $output->getFormatter());
$output->setFormatter($timestampFormatter);
}
$self = $this;
$updater = new Updater(\OC::$server->getHTTPHelper(),
\OC::$server->getConfig());
@ -132,54 +125,56 @@ class Upgrade extends Command {
$updater->setSkip3rdPartyAppsDisable($skip3rdPartyAppsDisable);
$updater->listen('\OC\Updater', 'maintenanceEnabled', function () use($output) {
$this->writeln($output, '<info>Turned on maintenance mode</info>');
$output->writeln('<info>Turned on maintenance mode</info>');
});
$updater->listen('\OC\Updater', 'maintenanceDisabled', function () use($output) {
$this->writeln($output, '<info>Turned off maintenance mode</info>');
$output->writeln('<info>Turned off maintenance mode</info>');
});
$updater->listen('\OC\Updater', 'maintenanceActive', function () use($output) {
$this->writeln($output, '<info>Maintenance mode is kept active</info>');
$output->writeln('<info>Maintenance mode is kept active</info>');
});
$updater->listen('\OC\Updater', 'updateEnd',
function ($success) use($output, $updateStepEnabled, $self) {
$mode = $updateStepEnabled ? 'Update' : 'Update simulation';
$status = $success ? 'successful' : 'failed' ;
$type = $success ? 'info' : 'error';
$message = "<$type>$mode $status</$type>";
$this->writeln($output, $message);
if ($success) {
$message = "<info>$mode successful</info>";
} else {
$message = "<error>$mode failed</error>";
}
$output->writeln($message);
});
$updater->listen('\OC\Updater', 'dbUpgrade', function () use($output) {
$this->writeln($output, '<info>Updated database</info>');
$output->writeln('<info>Updated database</info>');
});
$updater->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($output) {
$this->writeln($output, '<info>Checked database schema update</info>');
$output->writeln('<info>Checked database schema update</info>');
});
$updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($output) {
$$this->writeln($output, '<info>Disabled incompatible app: ' . $app . '</info>');
$output->writeln('<info>Disabled incompatible app: ' . $app . '</info>');
});
$updater->listen('\OC\Updater', 'thirdPartyAppDisabled', function ($app) use ($output) {
$this->writeln($output, '<info>Disabled 3rd-party app: ' . $app . '</info>');
$output->writeln('<info>Disabled 3rd-party app: ' . $app . '</info>');
});
$updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($output) {
$this->writeln($output, '<info>Update 3rd-party app: ' . $app . '</info>');
$output->writeln('<info>Update 3rd-party app: ' . $app . '</info>');
});
$updater->listen('\OC\Updater', 'repairWarning', function ($app) use($output) {
$this->writeln($output, '<error>Repair warning: ' . $app . '</error>');
$output->writeln('<error>Repair warning: ' . $app . '</error>');
});
$updater->listen('\OC\Updater', 'repairError', function ($app) use($output) {
$this->writeln($output, '<error>Repair error: ' . $app . '</error>');
$output->writeln('<error>Repair error: ' . $app . '</error>');
});
$updater->listen('\OC\Updater', 'appUpgradeCheck', function () use ($output) {
$this->writeln($output, '<info>Checked database schema update for apps</info>');
$output->writeln('<info>Checked database schema update for apps</info>');
});
$updater->listen('\OC\Updater', 'appUpgradeStarted', function ($app, $version) use ($output) {
$output->writeln("<info>Updating <$app> ...</info>");
});
$updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($output) {
$this->writeln($output, "<info>Updated <$app> to $version</info>");
$output->writeln("<info>Updated <$app> to $version</info>");
});
$updater->listen('\OC\Updater', 'failure', function ($message) use($output, $self) {
$this->writeln($output, "<error>$message</error>");
$output->writeln("<error>$message</error>");
});
$success = $updater->upgrade();
@ -222,22 +217,4 @@ class Upgrade extends Command {
);
}
}
/**
* Prints a line to output and adds a timestamp if needed
*
* @param OutputInterface $output
* @param string $line
*/
protected function writeln(OutputInterface $output, $line) {
$t = '';
if($this->showTimestamp) {
$timeZone = $this->config->getSystemValue('logtimezone', null);
$timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
$time = new \DateTime('now', $timeZone);
$t = $time->format($this->config->getSystemValue('logdateformat', 'c')) . ' ';
}
$output->writeln($t . $line);
}
}

View File

@ -0,0 +1,107 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Console;
use OCP\IConfig;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
class TimestampFormatter implements OutputFormatterInterface {
/** @var IConfig */
protected $config;
/**
* @param IConfig $config
* @param OutputFormatterInterface $formatter
*/
public function __construct(IConfig $config, OutputFormatterInterface $formatter) {
$this->config = $config;
$this->formatter = $formatter;
}
/**
* Sets the decorated flag.
*
* @param bool $decorated Whether to decorate the messages or not
*/
public function setDecorated($decorated) {
$this->formatter->setDecorated($decorated);
}
/**
* Gets the decorated flag.
*
* @return bool true if the output will decorate messages, false otherwise
*/
public function isDecorated() {
return $this->formatter->isDecorated();
}
/**
* Sets a new style.
*
* @param string $name The style name
* @param OutputFormatterStyleInterface $style The style instance
*/
public function setStyle($name, OutputFormatterStyleInterface $style) {
$this->formatter->setStyle($name, $style);
}
/**
* Checks if output formatter has style with specified name.
*
* @param string $name
* @return bool
*/
public function hasStyle($name) {
$this->formatter->hasStyle($name);
}
/**
* Gets style options from style with specified name.
*
* @param string $name
* @return OutputFormatterStyleInterface
*/
public function getStyle($name) {
return $this->formatter->getStyle($name);
}
/**
* Formats a message according to the given styles.
*
* @param string $message The message to style
* @return string The styled message, prepended with a timestamp using the
* log timezone and dateformat, e.g. "2015-06-23T17:24:37+02:00"
*/
public function format($message) {
$timeZone = $this->config->getSystemValue('logtimezone', null);
$timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
$time = new \DateTime('now', $timeZone);
$timestampInfo = $time->format($this->config->getSystemValue('logdateformat', 'c'));
return $timestampInfo . ' ' . $this->formatter->format($message);
}
}