Path: blob/master/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php
12256 views
<?php12final class HarbormasterManagementWriteLogWorkflow3extends HarbormasterManagementWorkflow {45protected function didConstruct() {6$this7->setName('write-log')8->setExamples('**write-log** --target __id__ [__options__]')9->setSynopsis(10pht(11'Write a new Harbormaster build log. This is primarily intended '.12'to make development and testing easier.'))13->setArguments(14array(15array(16'name' => 'target',17'param' => 'id',18'help' => pht('Build Target ID to attach the log to.'),19),20array(21'name' => 'rate',22'param' => 'bytes',23'help' => pht(24'Limit the rate at which the log is written, to test '.25'live log streaming.'),26),27));28}2930public function execute(PhutilArgumentParser $args) {31$viewer = $this->getViewer();3233$target_id = $args->getArg('target');34if (!$target_id) {35throw new PhutilArgumentUsageException(36pht('Choose a build target to attach the log to with "--target".'));37}3839$target = id(new HarbormasterBuildTargetQuery())40->setViewer($viewer)41->withIDs(array($target_id))42->executeOne();43if (!$target) {44throw new PhutilArgumentUsageException(45pht(46'Unable to load build target "%s".',47$target_id));48}4950$log = HarbormasterBuildLog::initializeNewBuildLog($target);51$log->openBuildLog();5253echo tsprintf(54"%s\n\n __%s__\n\n",55pht('Opened a new build log:'),56PhabricatorEnv::getURI($log->getURI()));5758echo tsprintf(59"%s\n",60pht('Reading log content from stdin...'));6162$content = file_get_contents('php://stdin');6364$rate = $args->getArg('rate');65if ($rate) {66if ($rate <= 0) {67throw new Exception(68pht(69'Write rate must be more than 0 bytes/sec.'));70}7172echo tsprintf(73"%s\n",74pht('Writing log, slowly...'));7576$offset = 0;77$total = strlen($content);78$pieces = str_split($content, $rate);7980$bar = id(new PhutilConsoleProgressBar())81->setTotal($total);8283foreach ($pieces as $piece) {84$log->append($piece);85$bar->update(strlen($piece));86sleep(1);87}8889$bar->done();9091} else {92$log->append($content);93}9495echo tsprintf(96"%s\n",97pht('Write completed. Closing log...'));9899PhabricatorWorker::setRunAllTasksInProcess(true);100101$log->closeBuildLog();102103echo tsprintf(104"%s\n",105pht('Done.'));106107return 0;108}109110}111112113