private function pass_through_pager($out) { if (Utils\isWindows() || in_array(Terminus::getConfig('format'), array('bash', 'json'))) { // No paging for Windows cmd.exe; sorry $this->output()->outputValue($out); return 0; } // convert string to file handle $fd = fopen("php://temp", "r+"); fputs($fd, $out); rewind($fd); $descriptorspec = array(0 => $fd, 1 => STDOUT, 2 => STDERR); return proc_close(proc_open('less -r', $descriptorspec, $pipes)); }
/** * Launch an external process that takes over I/O. * * @param array $arg_options Elements as follow: * string command Command to call * array descriptor_spec How PHP passes descriptor to child process * bool exit_on_error True to exit if the command returns error * @return int The command exit status */ public function launch(array $arg_options = []) { $default_options = ['exit_on_error' => true, 'descriptor_spec' => [STDIN, STDOUT, STDERR]]; $options = array_merge($default_options, $arg_options); $command = $options['command']; if (Utils\isWindows()) { $command = '"' . $command . '"'; } $status = proc_close(proc_open($command, $options['descriptor_spec'], $pipes)); if ((bool) $status && $options['exit_on_error']) { exit($status); } return $status; }
/** * Update a specific plugin * * @param string $arg Plugin name */ private function updatePlugin($arg) { $plugin = $this->getPluginDir($arg); if (is_dir("{$plugin}")) { $windows = Utils\isWindows(); if ($windows) { $slash = '\\\\'; } else { $slash = '/'; } $git_dir = $plugin . $slash . '.git'; $message = "Updating {$arg} plugin..."; $this->log()->notice($message); if (!is_dir("{$git_dir}")) { $messages = array(); $message = "Unable to update {$arg} plugin."; $message .= " Git repository does not exist."; $messages[] = $message; $message = "The recommended way to install plugins"; $message .= " is git clone <URL to plugin Git repository>."; $messages[] = $message; $message = "See https://github.com/pantheon-systems/terminus/"; $message .= "wiki/Plugins."; $messages[] = $message; foreach ($messages as $message) { $this->log()->error($message); } } else { exec("cd \"{$plugin}\" && git pull", $output); foreach ($output as $message) { $this->log()->notice($message); } } } }
/** * Gets input from STDIN silently * By: Troels Knak-Nielsen * From: http://www.sitepoint.com/interactive-cli-password-prompt-in-php/ * * @param array $arg_options Elements as follow: * string message Message to give at prompt * mixed default Returned if user does not select a valid option * @return string * @throws TerminusException */ public function promptSecret(array $arg_options = []) { $default_options = ['message' => '', 'default' => null]; $options = array_merge($default_options, $arg_options); if (Utils\isWindows()) { $vbscript = sys_get_temp_dir() . 'prompt_password.vbs'; file_put_contents($vbscript, 'wscript.echo(InputBox("' . addslashes($options['message']) . '", "", "password here"))'); $command = "cscript //nologo " . escapeshellarg($vbscript); $response = rtrim(shell_exec($command)); unlink($vbscript); } else { $command = "/usr/bin/env bash -c 'echo OK'"; if (rtrim(shell_exec($command)) !== 'OK') { throw new TerminusException("Can't invoke bash", [], 1); } $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($options['message']) . "\" mypassword && echo \$mypassword'"; $response = rtrim(shell_exec($command)); $this->command->output()->line(); } if (empty($response)) { return $options['default']; } return $response; }
/** * Displays the output with Less * * @param string $out Help text to be displayed * @return int Exit status of Less */ private function passThroughPager($out) { if (Utils\isWindows()) { //No paging for Windows cmd.exe. Sorry! $this->output()->outputValue($out); } // Convert string to file handle $fd = fopen('php://temp', 'r+;'); fputs($fd, $out); rewind($fd); $descriptorspec = array(0 => $fd, 1 => STDOUT, 2 => STDERR); $exit_status = proc_close(proc_open('less ', $descriptorspec, $pipes)); return $exit_status; }
/** * Launch an external process that takes over I/O. * * @param string $command Command to call * @param bool $exit_on_error True to exit if the command returns error * @return int The command exit status */ public static function launch($command, $exit_on_error = true) { if (Utils\isWindows()) { $command = '"' . $command . '"'; } $r = proc_close(proc_open($command, array(STDIN, STDOUT, STDERR), $pipes)); if ($r && $exit_on_error) { exit($r); } return $r; }
/** * Displays the output with Less * * @param string $out Help text to be displayed * @return int Exit status of Less */ private function passThroughPager($out) { if (Utils\isWindows()) { //No paging for Windows cmd.exe. Sorry! $this->output()->outputValue($out); } // Convert string to file handle $fd = fopen('php://temp', 'r+;'); fputs($fd, $out); rewind($fd); $descriptor_spec = [$fd, STDOUT, STDERR]; $exit_status = $this->helpers->launch->launch(['command' => 'less ', 'descriptor_spec' => $descriptor_spec]); return $exit_status; }
public function testIsWindows() { $os = shell_exec('uname'); $is_windows = Utils\isWindows(); $this->assertEquals(strpos($os, 'NT') !== false, $is_windows); }
public function testIsWindows() { $os = shell_exec('uname'); $is_windows = Utils\isWindows(); $this->assertEquals(strpos($os, 'NT') !== false, $is_windows); putenv("TERMINUS_TEST_IGNORE=1"); $this->assertTrue(Utils\isWindows()); putenv("TERMINUS_TEST_IGNORE="); }
/** * Gets input from STDIN silently * By: Troels Knak-Nielsen * From: http://www.sitepoint.com/interactive-cli-password-prompt-in-php/ * * @param [string] $message Message to give at prompt * @param [mixed] $default Returned if user does not select a valid option * @return [string] $response */ static function promptSecret($message = '', $default = null) { if (Utils\isWindows()) { $vbscript = sys_get_temp_dir() . 'prompt_password.vbs'; file_put_contents($vbscript, 'wscript.echo(InputBox("' . addslashes($message) . '", "", "password here"))'); $command = "cscript //nologo " . escapeshellarg($vbscript); $response = rtrim(shell_exec($command)); unlink($vbscript); } else { $command = "/usr/bin/env bash -c 'echo OK'"; if (rtrim(shell_exec($command)) !== 'OK') { trigger_error("Can't invoke bash"); return; } $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($message) . "\" mypassword && echo \$mypassword'"; $response = rtrim(shell_exec($command)); echo "\n"; } if (empty($response) && $default) { $response = $default; } return $response; }