Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/conduit/management/PhabricatorConduitCallManagementWorkflow.php
12256 views
1
<?php
2
3
final class PhabricatorConduitCallManagementWorkflow
4
extends PhabricatorConduitManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('call')
9
->setSynopsis(pht('Call a Conduit method..'))
10
->setArguments(
11
array(
12
array(
13
'name' => 'method',
14
'param' => 'method',
15
'help' => pht('Method to call.'),
16
),
17
array(
18
'name' => 'input',
19
'param' => 'input',
20
'help' => pht(
21
'File to read parameters from, or "-" to read from '.
22
'stdin.'),
23
),
24
array(
25
'name' => 'local',
26
'help' => pht(
27
'Force the request to execute in this process, rather than '.
28
'proxying to another host in the cluster.'),
29
),
30
array(
31
'name' => 'as',
32
'param' => 'username',
33
'help' => pht(
34
'Execute the call as the given user. (If omitted, the call will '.
35
'be executed as an omnipotent user.)'),
36
),
37
));
38
}
39
40
public function execute(PhutilArgumentParser $args) {
41
$viewer = $this->getViewer();
42
43
$method = $args->getArg('method');
44
if (!strlen($method)) {
45
throw new PhutilArgumentUsageException(
46
pht('Specify a method to call with "--method".'));
47
}
48
49
$input = $args->getArg('input');
50
if (!strlen($input)) {
51
throw new PhutilArgumentUsageException(
52
pht('Specify a file to read parameters from with "--input".'));
53
}
54
55
$as = $args->getArg('as');
56
if (strlen($as)) {
57
$actor = id(new PhabricatorPeopleQuery())
58
->setViewer($viewer)
59
->withUsernames(array($as))
60
->executeOne();
61
if (!$actor) {
62
throw new PhutilArgumentUsageException(
63
pht(
64
'No such user "%s" exists.',
65
$as));
66
}
67
68
// Allow inline generation of user caches for the user we're acting
69
// as, since some calls may read user preferences.
70
$actor->setAllowInlineCacheGeneration(true);
71
} else {
72
$actor = $viewer;
73
}
74
75
if ($input === '-') {
76
fprintf(STDERR, tsprintf("%s\n", pht('Reading input from stdin...')));
77
$input_json = file_get_contents('php://stdin');
78
} else {
79
$input_json = Filesystem::readFile($input);
80
}
81
82
$params = phutil_json_decode($input_json);
83
84
$call = id(new ConduitCall($method, $params))
85
->setUser($actor);
86
87
$api_request = $call->getAPIRequest();
88
89
$is_local = $args->getArg('local');
90
if ($is_local) {
91
$api_request->setIsClusterRequest(true);
92
}
93
94
$result = $call->execute();
95
96
$output = array(
97
'result' => $result,
98
);
99
100
echo tsprintf(
101
"%B\n",
102
id(new PhutilJSON())->encodeFormatted($output));
103
104
return 0;
105
}
106
107
}
108
109