5 use pharext\Cli\Args
as CliArgs
;
7 require_once "pharext/Version.php";
12 * Command line arguments
13 * @var pharext\CliArgs
19 * @see \pharext\Command::getArgs()
21 public function getArgs() {
26 * Output pharext vX.Y.Z header
29 printf("pharext v%s (c) Michael Wallner <mike@php.net>\n\n",
35 * @see \pharext\Command::info()
37 public function info($fmt) {
38 if (!$this->args
->quiet
) {
39 vprintf($fmt, array_slice(func_get_args(), 1));
45 * @see \pharext\Command::error()
47 public function error($fmt) {
48 if (!$this->args
->quiet
) {
51 $arg = error_get_last()["message"];
53 $arg = array_slice(func_get_args(), 1);
55 vfprintf(STDERR
, "ERROR: $fmt", $arg);
60 * Output command line help message
63 public function help($prog) {
64 printf("Usage:\n\n \$ %s", $prog);
69 foreach ($this->args
->getSpec() as $spec) {
70 if ($spec[3] & CliArgs
::REQARG
) {
71 if ($spec[3] & CliArgs
::REQUIRED
) {
82 printf(" [-%s]", implode("", array_column($flags, 0)));
84 foreach ($required as $req) {
85 printf(" -%s <arg>", $req[0]);
88 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
91 $spc = $this->args
->getSpec();
92 $max = max(array_map("strlen", array_column($spc, 1)));
94 foreach ($spc as $spec) {
95 if (isset($spec[0])) {
96 printf(" -%s|", $spec[0]);
100 printf("--%s ", $spec[1]);
101 if ($spec[3] & CliArgs
::REQARG
) {
103 } elseif ($spec[3] & CliArgs
::OPTARG
) {
108 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+
3*!isset($spec[0])), $spec[2]);
109 if ($spec[3] & CliArgs
::REQUIRED
) {
110 printf(" (REQUIRED)");
112 if (isset($spec[4])) {
113 printf(" [%s]", $spec[4]);
121 * Create temporary file/directory name
122 * @param string $prefix
123 * @param string $suffix
125 private function tempname($prefix, $suffix = null) {
126 if (!isset($suffix)) {
129 return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix);
133 * Create a new temp directory
134 * @param string $prefix
137 private function newtemp($prefix) {
138 $temp = $this->tempname($prefix);
139 if (!is_dir($temp)) {
140 if (!mkdir($temp, 0700, true)) {
152 private function rm($dir) {
153 foreach (scandir($dir) as $entry) {
154 if ($entry === "." ||
$entry === "..") {
156 } elseif (is_dir("$dir/$entry")) {
157 $this->rm("$dir/$entry");
158 } elseif (!unlink("$dir/$entry")) {
168 * Execute a program with escalated privileges handling interactive password prompt
169 * @param string $command
170 * @param string $output
173 private function sudo($command, &$output) {
174 if (!($proc = proc_open($command, [STDIN
,["pipe","w"],["pipe","w"]], $pipes))) {
179 while (!feof($stdout)) {
180 $R = [$stdout]; $W = []; $E = [];
181 if (!stream_select($R, $W, $E, null)) {
184 $data = fread($stdout, 0x1000);
185 /* only check a few times */
186 if ($passwd++
< 10) {
187 if (stristr($data, "password")) {
188 printf("\n%s", $data);
193 return proc_close($proc);
197 * Execute a system command
198 * @param string $name pretty name
199 * @param string $command command
200 * @param array $args command arguments
201 * @param bool $sudo whether the command may need escalated privileges
203 private function exec($name, $command, array $args = null, $sudo = false) {
204 $exec = escapeshellcmd($command);
206 $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args));
209 if ($this->args
->verbose
) {
210 $this->info("Running %s ...\n", $exec);
212 $this->info("Running %s ... ", $name);
215 if ($sudo && isset($this->args
->sudo
)) {
216 $retval = $this->sudo(sprintf($this->args
->sudo
." 2>&1", $exec), $output);
217 } elseif ($this->args
->verbose
) {
218 passthru($exec ." 2>&1", $retval);
220 exec($exec ." 2>&1", $output, $retval);
221 $output = implode("\n", $output);
225 $this->error("Command %s failed with (%s)\n", $command, $retval);
226 if (isset($output) && !$this->args
->quiet
) {
227 printf("%s\n", $output);
231 if (!$this->args
->verbose
) {
232 // we already have a bunch of output