Skip to content

Commit 393ea04

Browse files
committed
Merge pull request #8 from pantheon-systems/stovak
Added art, site and environment commands; better docs
2 parents d5058bc + e45991b commit 393ea04

19 files changed

+803
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ composer.lock
44
/phpunit.xml
55

66
/builds
7+
8+
/.DS_Store

composer.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "terminus/terminus",
3-
"description": "A command line interface for WordPress",
4-
"keywords": [ "cli", "wordpress" ],
5-
"homepage": "http://terminus.org",
3+
"description": "A command line interface for Pantheon",
4+
"keywords": [ "cli", "pantheon" ],
5+
"homepage": "http://getpantheon.com",
66
"license": "MIT",
77
"bin": [
8-
"bin/wp.bat", "bin/wp"
8+
"bin/terminus.bat", "bin/terminus"
99
],
1010
"require": {
1111
"php": ">=5.3.2",
@@ -16,9 +16,6 @@
1616
"symfony/finder": "~2.3",
1717
"nb/oxymel": "0.1.0"
1818
},
19-
"suggest": {
20-
"psy/psysh": "Enhanced `wp shell` functionality"
21-
},
2219
"autoload": {
2320
"psr-0": { "Terminus": "php" },
2421
"files": [ "php/Spyc.php" ],

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

php/Terminus/Dispatcher/CompositeCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ function find_subcommand( &$args ) {
9494
}
9595
}
9696

97-
if ( !isset( $subcommands[ $name ] ) )
97+
if ( !isset( $subcommands[ $name ] ) ){
9898
return false;
99+
}
99100

100101
return $subcommands[ $name ];
101102
}

php/Terminus/Runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function run() {
144144
}
145145

146146
// First try at showing man page
147-
if ( 'help' === $this->arguments[0] && ( isset( $this->arguments[1] ) || !$this->wp_exists() ) ) {
147+
if ( 'help' === $this->arguments[0] && ( isset( $this->arguments[1] ) ) ) {
148148
$this->_run_command();
149149
}
150150

php/class-terminus-command.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ abstract class Terminus_Command {
1010
public $cache;
1111
public $session;
1212
public $sites;
13+
14+
protected $_func;
15+
protected $_siteInfo;
16+
protected $_bindings;
1317

1418
public function __construct() {
1519
# Load commonly used data from cache.
@@ -156,6 +160,7 @@ public function terminus_request($realm, $uuid, $path = FALSE, $method = 'GET',
156160

157161
$info = curl_getinfo($ch);
158162
if ($info['http_code'] > 399) {
163+
$this->_debug(get_defined_vars());
159164
\Terminus::error('Request failed');
160165
// Expired session. Really don't like the string comparison.
161166
if ($info['http_code'] == 403 && $json == '"Session not found."') {
@@ -166,10 +171,131 @@ public function terminus_request($realm, $uuid, $path = FALSE, $method = 'GET',
166171
}
167172

168173
return array(
174+
'info' => $info,
169175
'headers' => $headers_text,
170176
'json' => $json,
171177
'data' => json_decode($json)
172178
);
173179
}
180+
181+
protected function _validateSiteUuid($site) {
182+
if (\Terminus\Utils\is_valid_uuid($site) && property_exists($this->sites, $site)){
183+
$this->_siteInfo =& $this->sites[$site];
184+
$this->_siteInfo->site_uuid = $site;
185+
} elseif($this->_siteInfo = $this->fetch_site($site)) {
186+
$site = $this->_siteInfo->site_uuid;
187+
} else {
188+
Terminus::error("Unable to locate the requested site.");
189+
}
190+
return $site;
191+
}
192+
193+
protected function _constructTableForResponse($data) {
194+
$table = new \cli\Table();
195+
if (is_object($data)) {
196+
$data = (array)$data;
197+
}
198+
if (property_exists($this, "_headers") && array_key_exists($this->_func, $this->_headers)) {
199+
$table->setHeaders($this->_headers[$this->_func]);
200+
} else {
201+
$table->setHeaders(array_keys($data));
202+
}
203+
foreach ($data as $row => $row_data) {
204+
$row = array();
205+
foreach($row_data as $key => $value) {
206+
$row[] = $value;
207+
}
208+
$table->addRow($row);
209+
}
210+
$table->display();
211+
}
212+
213+
protected function _handleFuncArg(array &$args = array() , array $assoc_args = array()) {
214+
// backups-delete should execute backups_delete function
215+
if (!empty($args)){
216+
$this->_func = str_replace("-", "_", array_shift($args));
217+
if (!is_callable(array($this, $this->_func), false, $static)) {
218+
if (array_key_exists("debug", $assoc_args)){
219+
$this->_debug(get_defined_vars());
220+
}
221+
Terminus::error("I cannot find the requested task to perform it.");
222+
}
223+
}
224+
}
225+
226+
protected function _handleSiteArg(&$args, $assoc_args = array()) {
227+
$uuid = null;
228+
if (array_key_exists("site", $assoc_args)) {
229+
$uuid = $this->_validateSiteUuid($assoc_args["site"]);
230+
} else {
231+
Terminus::error("Please specify the site with --site=<sitename> option.");
232+
}
233+
if (!empty($uuid) && property_exists($this->sites, $uuid)) {
234+
$this->_siteInfo = $this->sites->$uuid;
235+
$this->_siteInfo->site_uuid = $uuid;
236+
} else {
237+
if (array_key_exists("debug", $assoc_args)){
238+
$this->_debug(get_defined_vars());
239+
}
240+
Terminus::error("Please specify the site with --site=<sitename> option.");
241+
}
242+
}
243+
244+
protected function _handleEnvArg(&$args, $assoc_args = array()) {
245+
if (array_key_exists("env", $assoc_args)) {
246+
$this->_getEnvBindings($args, $assoc_args);
247+
} else {
248+
Terminus::error("Please specify the site => environment with --env=<environment> option.");
249+
}
250+
251+
if (!is_object($this->_bindings)) {
252+
if (array_key_exists("debug", $assoc_args)){
253+
$this->_debug(get_defined_vars());
254+
}
255+
Terminus::error("Unable to obtain the bindings for the requested environment.\n\n");
256+
} else {
257+
if (property_exists($this->_bindings, $assoc_args['env'])) {
258+
$this->_env = $assoc_args['env'];
259+
} else {
260+
Terminus::error("The requested environment either does not exist or you don't have access to it.");
261+
}
262+
}
263+
}
264+
265+
protected function _getEnvBindings(&$args, $assoc_args) {
266+
$b = $this->terminus_request("site", $this->_siteInfo->site_uuid, 'environments/'. $this->_env .'/bindings', "GET");
267+
if (!empty($b) && is_array($b) && array_key_exists("data", $b)) {
268+
$this->_bindings = $b['data'];
269+
}
270+
}
271+
272+
protected function _execute( array $args = array() , array $assoc_args = array() ){
273+
$success = $this->{$this->_func}( $args, $assoc_args);
274+
if (array_key_exists("debug", $assoc_args)){
275+
$this->_debug(get_defined_vars());
276+
}
277+
if (!empty($success)){
278+
if (is_array($success) && array_key_exists("data", $success)) {
279+
if (array_key_exists("json", $assoc_args)) {
280+
echo \Terminus\Utils\json_dump($success["data"]);
281+
} else {
282+
$this->_constructTableForResponse($success['data']);
283+
}
284+
} elseif (is_string($success)) {
285+
echo Terminus::line($success);
286+
}
287+
} else {
288+
if (array_key_exists("debug", $assoc_args)){
289+
$this->_debug(get_defined_vars());
290+
}
291+
Terminus::error("There was an error attempting to execute the requested task.\n\n");
292+
}
293+
}
294+
295+
protected function _debug($vars) {
296+
Terminus::line(print_r($this, true));
297+
Terminus::line(print_r($vars, true));
298+
}
299+
174300
}
175301

php/class-terminus.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ static function add_command( $name, $class, $args = array() ) {
106106
while ( !empty( $path ) ) {
107107
$subcommand_name = $path[0];
108108
$subcommand = $command->find_subcommand( $path );
109-
110109
// create an empty container
111110
if ( !$subcommand ) {
112111
$subcommand = new Dispatcher\CompositeCommand( $command, $subcommand_name,
@@ -122,8 +121,7 @@ static function add_command( $name, $class, $args = array() ) {
122121
if ( ! $command->can_have_subcommands() ) {
123122
throw new Exception( sprintf( "'%s' can't have subcommands.",
124123
implode( ' ' , Dispatcher\get_path( $command ) ) ) );
125-
}
126-
124+
}
127125
$command->add_subcommand( $leaf_name, $leaf_command );
128126
}
129127

@@ -314,8 +312,8 @@ static function launch_self( $command, $args = array(), $assoc_args = array(), $
314312
);
315313

316314
foreach ( $reused_runtime_args as $key ) {
317-
if ( $value = self::get_runner()->config[ $key ] )
318-
$assoc_args[ $key ] = $value;
315+
if ( array_key_exists( $key, self::get_runner()->config ) )
316+
$assoc_args[ $key ] = self::get_runner()->config[$key];
319317
}
320318

321319
$php_bin = self::get_php_binary();

php/commands/art.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Print the pantheon art
5+
*
6+
*/
7+
class Art_Command extends Terminus_Command {
8+
9+
private $works = array(
10+
"fist" => "CiAgICAgICAgLisuCiAgICAgICAgLis/OgogICAgICAgICAuKz8/LgogICAgICAgICAgID8/PyAuCiAgICAgICAgICAgKz8/Py4KICAgICAgKz8/Pz8/Pz8/Pz0uCiAgICAgIC4/Pz8/Pz8/Pz8/Py4KICAgICAgLj8/Pz8/Pz8/Pz8/Py4KCiAgICAgIyMjIyMjIyMjIyMgIyMjIyMjIyMKICAgICAjIyMjIyMjIyMjIyMuIyMjIyMjIy4KICAgICAjIyMjIyMjICMjIyMgIC4uLi4uLi4KICAgICAjIyMjIyMjIyAjIyMjICMjIyMjIyMgICAgICAgICAgICAgICAgNTAgNDEgNEUgNTQgNDggNDUgNEYgNEUKICAgICAjIyMjIyMjIyMuIyMjIy4jIyMjIyMgICAgICAgIF9fX19fX19fX19fX18gIF9fICBfX19fX19fXyAgX19fXyAgX19fX19fCiAgICAgIyMjIyMjICAuLi4gICAgICAgICAgICAgICAgIC9fICBfXy8gX18vIF8gXC8gIHwvICAvICBfLyB8LyAvIC8gLyAvIF9fLwogICAgICMjIyMjIyMuPz8uIyMjIyMjIyMjIyAgICAgICAgLyAvIC8gXy8vICwgXy8gL3xfLyAvLyAvLyAgICAvIC9fLyAvXCBcCiAgICAgIyMjIyMjI34rPz8uIyMjIyMjIyMjICAgICAgIC9fLyAvX19fL18vfF8vXy8gIC9fL19fXy9fL3xfL1xfX19fL19fXy8KICAgICAjIyMjIyMjIy4/Py4uCiAgICAgIyMjIyMjIyMjLj8/LiMjIyMjIyMuCiAgICAgIyMjIyMjIyMjLis/PyAjIyMjIyMuCiAgICAgICAgICAgICAgIC4rPy4KICAgICAgICAgLj8/Pz8/Pz8/Pz8/Py4KICAgICAgICAgICArPz8/Pz8/Pz8/PywKICAgICAgICAgICAgLj8/Pz8rKysrKysuCiAgICAgICAgICAgICAgPz8/Py4KICAgICAgICAgICAgICAuPz8/LAogICAgICAgICAgICAgICAufj8/LgogICAgICAgICAgICAgICAgIC4/PwogICAgICAgICAgICAgICAgICAuPywu",
11+
"unicorn" => "ICAgICAgIFwKICAgICAgICBcCiAgICAgICAgIFxcCiAgICAgICAgICBcXAogICAgICAgICAgID5cLzcKICAgICAgIF8uLSg2JyAgXAogICAgICAoPV9fXy5fL2AgXAogICAgICAgICAgICkgIFwgfAogICAgICAgICAgLyAgIC8gfAogICAgICAgICAvICAgID4gLwogICAgICAgIGogICAgPCBfXAogICAgXy4tJyA6ICAgICAgYGAuCiAgICBcIHI9Ll9cICAgICAgICBgLgogICA8YFxcXyAgXCAgICAgICAgIC5gLS4KICAgIFwgci03ICBgLS4gLl8gICcgLiAgYFwKICAgICBcYCwgICAgICBgLS5gNyAgNykgICApCiAgICAgIFwvICAgICAgICAgXHwgIFwnICAvIGAtLl8KICAgICAgICAgICAgICAgICB8fCAgICAuJwogICAgICAgICAgICAgICAgICBcXCAgKAogICAgICAgICAgICAgICAgICAgPlwgID4KICAgICAgICAgICAgICAgLC4tJyA+LicKICAgICAgICAgICAgICA8LidfLicnCiAgICAgICAgICAgICAgICA8Jw=="
12+
);
13+
14+
/**
15+
* View Pantheon artwork
16+
*
17+
*/
18+
function __invoke( $args, $assoc_args ) {
19+
$artwork = array_shift($args);
20+
21+
if (!empty($artwork) && array_key_exists($artwork, $this->works)){
22+
echo Terminus::colorize(base64_decode($this->works[$artwork]))."\n";
23+
} else {
24+
Terminus::error("No formula for requested artwork");
25+
}
26+
}
27+
28+
}
29+
30+
Terminus::add_command( 'art', 'Art_Command' );

php/commands/auth.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*
55
*/
66
class Auth_Command extends Terminus_Command {
7+
8+
79

810
/**
911
* Log in as a user
@@ -14,6 +16,8 @@ class Auth_Command extends Terminus_Command {
1416
*
1517
* [--password=<value>]
1618
* : Log in non-interactively with this password. Useful for automation.
19+
* [--debug]
20+
* : dump call information when logging in.
1721
*/
1822
public function login( $args, $assoc_args ) {
1923
if ( empty( $args ) ) {
@@ -36,38 +40,57 @@ public function login( $args, $assoc_args ) {
3640
Terminus::line( "Logging in as $email" );
3741
$data = \Terminus\Login\auth( $email, $password );
3842
if ( $data != FALSE ) {
39-
Terminus::line( "Success!" );
43+
if (array_key_exists("debug", $assoc_args)){
44+
$this->_debug(get_defined_vars());
45+
}
46+
//Terminus::line( "Success!" );
4047
$this->cache->put_data('session', $data);
48+
Terminus::launch_self("art", array("fist"));
4149
}
4250
else {
43-
Terminus::line( "Login Failed/" );
51+
Terminus::error( "Login Failed!" );
4452
}
4553
}
4654
else {
47-
Terminus::line( "Error: invalid email address" );
55+
Terminus::error( "Error: invalid email address" );
4856
}
4957
}
5058

5159
/**
5260
* Log yourself out and remove the secret session key.
5361
*/
5462
public function logout() {
55-
$this->line( "Logging out of to Pantheon." );
63+
Terminus::line( "Logging out of Pantheon." );
5664
$this->cache->remove('session');
5765
}
5866

5967
/**
6068
* Find out what user you are logged in as.
6169
*/
62-
public function whoami() {
70+
public function whoami() {
6371
if ($this->session) {
64-
$this->line( "You are authenticated as ". $this->session->email );
72+
Terminus::line( "You are authenticated as ". $this->session->email );
6573
}
6674
else {
67-
$this->line( "You are not logged in." );
75+
Terminus::line( "You are not logged in." );
6876
}
6977
}
7078

79+
private function _checkSession() {
80+
if ((!property_exists($this, "session")) || (!property_exists($this->session, "user_uuid"))) {
81+
return false;
82+
}
83+
$results = $this->terminus_request("user", $this->session->user_uuid, "profile", "GET");
84+
if ($results['info']['http_code'] >= 400){
85+
Terminus::line("Expired Session, please re-authenticate.");
86+
$this->cache->remove('session');
87+
Terminus::launch_self("auth", array("login"));
88+
$this->whoami();
89+
return true;
90+
} else {
91+
return (($results['info']['http_code'] <= 199 )||($results['info']['http_code'] >= 300 ))? false : true;
92+
}
93+
}
7194
}
7295

7396
Terminus::add_command( 'auth', 'Auth_Command' );

php/commands/cli.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function info( $_, $assoc_args ) {
7474
* @subcommand param-dump
7575
*/
7676
function param_dump() {
77-
echo json_encode( \Terminus::get_configurator()->get_spec() );
77+
echo \Terminus\Utils\json_dump( \Terminus::get_configurator()->get_spec() );
7878
}
7979

8080
/**
@@ -83,7 +83,7 @@ function param_dump() {
8383
* @subcommand cmd-dump
8484
*/
8585
function cmd_dump() {
86-
echo json_encode( self::command_to_array( Terminus::get_root_command() ) );
86+
echo \Terminus\Utils\json_dump( self::command_to_array( Terminus::get_root_command() ) );
8787
}
8888

8989
/**

0 commit comments

Comments
 (0)