1<?php
2
3final class ArcanistCommand
4  extends Phobject {
5
6  private $logEngine;
7  private $executableFuture;
8  private $resolveOnError = false;
9  private $displayCommand;
10
11  public function setExecutableFuture(PhutilExecutableFuture $future) {
12    $this->executableFuture = $future;
13    return $this;
14  }
15
16  public function getExecutableFuture() {
17    return $this->executableFuture;
18  }
19
20  public function setLogEngine(ArcanistLogEngine $log_engine) {
21    $this->logEngine = $log_engine;
22    return $this;
23  }
24
25  public function getLogEngine() {
26    return $this->logEngine;
27  }
28
29  public function setResolveOnError($resolve_on_error) {
30    $this->resolveOnError = $resolve_on_error;
31    return $this;
32  }
33
34  public function getResolveOnError() {
35    return $this->resolveOnError;
36  }
37
38  public function setDisplayCommand($pattern /* , ... */) {
39    $argv = func_get_args();
40    $command = call_user_func_array('csprintf', $argv);
41    $this->displayCommand = $command;
42    return $this;
43  }
44
45  public function getDisplayCommand() {
46    return $this->displayCommand;
47  }
48
49  public function execute() {
50    $log = $this->getLogEngine();
51    $future = $this->getExecutableFuture();
52
53    $display_command = $this->getDisplayCommand();
54    if ($display_command !== null) {
55      $command = $display_command;
56    } else {
57      $command = $future->getCommand();
58    }
59
60    $log->writeNewline();
61
62    $log->writeStatus(
63      ' $ ',
64      tsprintf('**%s**', phutil_string_cast($command)));
65
66    $log->writeNewline();
67
68    $err = $future->resolve();
69
70    $log->writeNewline();
71
72    if ($err && !$this->getResolveOnError()) {
73      $log->writeError(
74        pht('ERROR'),
75        pht(
76          'Command exited with error code %d.',
77          $err));
78
79      throw new CommandException(
80        pht('Command exited with nonzero error code.'),
81        $command,
82        $err,
83        '',
84        '');
85    }
86
87    return $err;
88  }
89}
90