Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/harbormaster/management/HarbormasterManagementWriteLogWorkflow.php
12256 views
1
<?php
2
3
final class HarbormasterManagementWriteLogWorkflow
4
extends HarbormasterManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('write-log')
9
->setExamples('**write-log** --target __id__ [__options__]')
10
->setSynopsis(
11
pht(
12
'Write a new Harbormaster build log. This is primarily intended '.
13
'to make development and testing easier.'))
14
->setArguments(
15
array(
16
array(
17
'name' => 'target',
18
'param' => 'id',
19
'help' => pht('Build Target ID to attach the log to.'),
20
),
21
array(
22
'name' => 'rate',
23
'param' => 'bytes',
24
'help' => pht(
25
'Limit the rate at which the log is written, to test '.
26
'live log streaming.'),
27
),
28
));
29
}
30
31
public function execute(PhutilArgumentParser $args) {
32
$viewer = $this->getViewer();
33
34
$target_id = $args->getArg('target');
35
if (!$target_id) {
36
throw new PhutilArgumentUsageException(
37
pht('Choose a build target to attach the log to with "--target".'));
38
}
39
40
$target = id(new HarbormasterBuildTargetQuery())
41
->setViewer($viewer)
42
->withIDs(array($target_id))
43
->executeOne();
44
if (!$target) {
45
throw new PhutilArgumentUsageException(
46
pht(
47
'Unable to load build target "%s".',
48
$target_id));
49
}
50
51
$log = HarbormasterBuildLog::initializeNewBuildLog($target);
52
$log->openBuildLog();
53
54
echo tsprintf(
55
"%s\n\n __%s__\n\n",
56
pht('Opened a new build log:'),
57
PhabricatorEnv::getURI($log->getURI()));
58
59
echo tsprintf(
60
"%s\n",
61
pht('Reading log content from stdin...'));
62
63
$content = file_get_contents('php://stdin');
64
65
$rate = $args->getArg('rate');
66
if ($rate) {
67
if ($rate <= 0) {
68
throw new Exception(
69
pht(
70
'Write rate must be more than 0 bytes/sec.'));
71
}
72
73
echo tsprintf(
74
"%s\n",
75
pht('Writing log, slowly...'));
76
77
$offset = 0;
78
$total = strlen($content);
79
$pieces = str_split($content, $rate);
80
81
$bar = id(new PhutilConsoleProgressBar())
82
->setTotal($total);
83
84
foreach ($pieces as $piece) {
85
$log->append($piece);
86
$bar->update(strlen($piece));
87
sleep(1);
88
}
89
90
$bar->done();
91
92
} else {
93
$log->append($content);
94
}
95
96
echo tsprintf(
97
"%s\n",
98
pht('Write completed. Closing log...'));
99
100
PhabricatorWorker::setRunAllTasksInProcess(true);
101
102
$log->closeBuildLog();
103
104
echo tsprintf(
105
"%s\n",
106
pht('Done.'));
107
108
return 0;
109
}
110
111
}
112
113