Provide a "--local" flag to "bin/conduit call" to force in-process execution
[phabricator/blender.git] / src / applications / conduit / management / PhabricatorConduitCallManagementWorkflow.php
bloba12d1374dcb31463eda3f1128564a14432a5cfa6
1 <?php
3 final class PhabricatorConduitCallManagementWorkflow
4 extends PhabricatorConduitManagementWorkflow {
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.'),
17 array(
18 'name' => 'input',
19 'param' => 'input',
20 'help' => pht(
21 'File to read parameters from, or "-" to read from '.
22 'stdin.'),
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.'),
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.)'),
37 ));
40 public function execute(PhutilArgumentParser $args) {
41 $viewer = $this->getViewer();
43 $method = $args->getArg('method');
44 if (!strlen($method)) {
45 throw new PhutilArgumentUsageException(
46 pht('Specify a method to call with "--method".'));
49 $input = $args->getArg('input');
50 if (!strlen($input)) {
51 throw new PhutilArgumentUsageException(
52 pht('Specify a file to read parameters from with "--input".'));
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));
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;
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);
82 $params = phutil_json_decode($input_json);
84 $call = id(new ConduitCall($method, $params))
85 ->setUser($actor);
87 $api_request = $call->getAPIRequest();
89 $is_local = $args->getArg('local');
90 if ($is_local) {
91 $api_request->setIsClusterRequest(true);
94 $result = $call->execute();
96 $output = array(
97 'result' => $result,
100 echo tsprintf(
101 "%B\n",
102 id(new PhutilJSON())->encodeFormatted($output));
104 return 0;