Skip to content

Commit

Permalink
Merge pull request #8 from pantheon-systems/stovak
Browse files Browse the repository at this point in the history
Added art, site and environment commands; better docs
  • Loading branch information
bensheldon committed Mar 25, 2014
2 parents d5058bc + e45991b commit 393ea04
Show file tree
Hide file tree
Showing 19 changed files with 803 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ composer.lock
/phpunit.xml

/builds

/.DS_Store
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "terminus/terminus",
"description": "A command line interface for WordPress",
"keywords": [ "cli", "wordpress" ],
"homepage": "http://terminus.org",
"description": "A command line interface for Pantheon",
"keywords": [ "cli", "pantheon" ],
"homepage": "http://getpantheon.com",
"license": "MIT",
"bin": [
"bin/wp.bat", "bin/wp"
"bin/terminus.bat", "bin/terminus"
],
"require": {
"php": ">=5.3.2",
Expand All @@ -16,9 +16,6 @@
"symfony/finder": "~2.3",
"nb/oxymel": "0.1.0"
},
"suggest": {
"psy/psysh": "Enhanced `wp shell` functionality"
},
"autoload": {
"psr-0": { "Terminus": "php" },
"files": [ "php/Spyc.php" ],
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion php/Terminus/Dispatcher/CompositeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ function find_subcommand( &$args ) {
}
}

if ( !isset( $subcommands[ $name ] ) )
if ( !isset( $subcommands[ $name ] ) ){
return false;
}

return $subcommands[ $name ];
}
Expand Down
2 changes: 1 addition & 1 deletion php/Terminus/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function run() {
}

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

Expand Down
126 changes: 126 additions & 0 deletions php/class-terminus-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ abstract class Terminus_Command {
public $cache;
public $session;
public $sites;

protected $_func;
protected $_siteInfo;
protected $_bindings;

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

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

return array(
'info' => $info,
'headers' => $headers_text,
'json' => $json,
'data' => json_decode($json)
);
}

protected function _validateSiteUuid($site) {
if (\Terminus\Utils\is_valid_uuid($site) && property_exists($this->sites, $site)){
$this->_siteInfo =& $this->sites[$site];
$this->_siteInfo->site_uuid = $site;
} elseif($this->_siteInfo = $this->fetch_site($site)) {
$site = $this->_siteInfo->site_uuid;
} else {
Terminus::error("Unable to locate the requested site.");
}
return $site;
}

protected function _constructTableForResponse($data) {
$table = new \cli\Table();
if (is_object($data)) {
$data = (array)$data;
}
if (property_exists($this, "_headers") && array_key_exists($this->_func, $this->_headers)) {
$table->setHeaders($this->_headers[$this->_func]);
} else {
$table->setHeaders(array_keys($data));
}
foreach ($data as $row => $row_data) {
$row = array();
foreach($row_data as $key => $value) {
$row[] = $value;
}
$table->addRow($row);
}
$table->display();
}

protected function _handleFuncArg(array &$args = array() , array $assoc_args = array()) {
// backups-delete should execute backups_delete function
if (!empty($args)){
$this->_func = str_replace("-", "_", array_shift($args));
if (!is_callable(array($this, $this->_func), false, $static)) {
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
Terminus::error("I cannot find the requested task to perform it.");
}
}
}

protected function _handleSiteArg(&$args, $assoc_args = array()) {
$uuid = null;
if (array_key_exists("site", $assoc_args)) {
$uuid = $this->_validateSiteUuid($assoc_args["site"]);
} else {
Terminus::error("Please specify the site with --site=<sitename> option.");
}
if (!empty($uuid) && property_exists($this->sites, $uuid)) {
$this->_siteInfo = $this->sites->$uuid;
$this->_siteInfo->site_uuid = $uuid;
} else {
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
Terminus::error("Please specify the site with --site=<sitename> option.");
}
}

protected function _handleEnvArg(&$args, $assoc_args = array()) {
if (array_key_exists("env", $assoc_args)) {
$this->_getEnvBindings($args, $assoc_args);
} else {
Terminus::error("Please specify the site => environment with --env=<environment> option.");
}

if (!is_object($this->_bindings)) {
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
Terminus::error("Unable to obtain the bindings for the requested environment.\n\n");
} else {
if (property_exists($this->_bindings, $assoc_args['env'])) {
$this->_env = $assoc_args['env'];
} else {
Terminus::error("The requested environment either does not exist or you don't have access to it.");
}
}
}

protected function _getEnvBindings(&$args, $assoc_args) {
$b = $this->terminus_request("site", $this->_siteInfo->site_uuid, 'environments/'. $this->_env .'/bindings', "GET");
if (!empty($b) && is_array($b) && array_key_exists("data", $b)) {
$this->_bindings = $b['data'];
}
}

protected function _execute( array $args = array() , array $assoc_args = array() ){
$success = $this->{$this->_func}( $args, $assoc_args);
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
if (!empty($success)){
if (is_array($success) && array_key_exists("data", $success)) {
if (array_key_exists("json", $assoc_args)) {
echo \Terminus\Utils\json_dump($success["data"]);
} else {
$this->_constructTableForResponse($success['data']);
}
} elseif (is_string($success)) {
echo Terminus::line($success);
}
} else {
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
Terminus::error("There was an error attempting to execute the requested task.\n\n");
}
}

protected function _debug($vars) {
Terminus::line(print_r($this, true));
Terminus::line(print_r($vars, true));
}

}

8 changes: 3 additions & 5 deletions php/class-terminus.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static function add_command( $name, $class, $args = array() ) {
while ( !empty( $path ) ) {
$subcommand_name = $path[0];
$subcommand = $command->find_subcommand( $path );

// create an empty container
if ( !$subcommand ) {
$subcommand = new Dispatcher\CompositeCommand( $command, $subcommand_name,
Expand All @@ -122,8 +121,7 @@ static function add_command( $name, $class, $args = array() ) {
if ( ! $command->can_have_subcommands() ) {
throw new Exception( sprintf( "'%s' can't have subcommands.",
implode( ' ' , Dispatcher\get_path( $command ) ) ) );
}

}
$command->add_subcommand( $leaf_name, $leaf_command );
}

Expand Down Expand Up @@ -314,8 +312,8 @@ static function launch_self( $command, $args = array(), $assoc_args = array(), $
);

foreach ( $reused_runtime_args as $key ) {
if ( $value = self::get_runner()->config[ $key ] )
$assoc_args[ $key ] = $value;
if ( array_key_exists( $key, self::get_runner()->config ) )
$assoc_args[ $key ] = self::get_runner()->config[$key];
}

$php_bin = self::get_php_binary();
Expand Down
30 changes: 30 additions & 0 deletions php/commands/art.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Print the pantheon art
*
*/
class Art_Command extends Terminus_Command {

private $works = array(
"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",
"unicorn" => "ICAgICAgIFwKICAgICAgICBcCiAgICAgICAgIFxcCiAgICAgICAgICBcXAogICAgICAgICAgID5cLzcKICAgICAgIF8uLSg2JyAgXAogICAgICAoPV9fXy5fL2AgXAogICAgICAgICAgICkgIFwgfAogICAgICAgICAgLyAgIC8gfAogICAgICAgICAvICAgID4gLwogICAgICAgIGogICAgPCBfXAogICAgXy4tJyA6ICAgICAgYGAuCiAgICBcIHI9Ll9cICAgICAgICBgLgogICA8YFxcXyAgXCAgICAgICAgIC5gLS4KICAgIFwgci03ICBgLS4gLl8gICcgLiAgYFwKICAgICBcYCwgICAgICBgLS5gNyAgNykgICApCiAgICAgIFwvICAgICAgICAgXHwgIFwnICAvIGAtLl8KICAgICAgICAgICAgICAgICB8fCAgICAuJwogICAgICAgICAgICAgICAgICBcXCAgKAogICAgICAgICAgICAgICAgICAgPlwgID4KICAgICAgICAgICAgICAgLC4tJyA+LicKICAgICAgICAgICAgICA8LidfLicnCiAgICAgICAgICAgICAgICA8Jw=="
);

/**
* View Pantheon artwork
*
*/
function __invoke( $args, $assoc_args ) {
$artwork = array_shift($args);

if (!empty($artwork) && array_key_exists($artwork, $this->works)){
echo Terminus::colorize(base64_decode($this->works[$artwork]))."\n";
} else {
Terminus::error("No formula for requested artwork");
}
}

}

Terminus::add_command( 'art', 'Art_Command' );
37 changes: 30 additions & 7 deletions php/commands/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*
*/
class Auth_Command extends Terminus_Command {



/**
* Log in as a user
Expand All @@ -14,6 +16,8 @@ class Auth_Command extends Terminus_Command {
*
* [--password=<value>]
* : Log in non-interactively with this password. Useful for automation.
* [--debug]
* : dump call information when logging in.
*/
public function login( $args, $assoc_args ) {
if ( empty( $args ) ) {
Expand All @@ -36,38 +40,57 @@ public function login( $args, $assoc_args ) {
Terminus::line( "Logging in as $email" );
$data = \Terminus\Login\auth( $email, $password );
if ( $data != FALSE ) {
Terminus::line( "Success!" );
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
//Terminus::line( "Success!" );
$this->cache->put_data('session', $data);
Terminus::launch_self("art", array("fist"));
}
else {
Terminus::line( "Login Failed/" );
Terminus::error( "Login Failed!" );
}
}
else {
Terminus::line( "Error: invalid email address" );
Terminus::error( "Error: invalid email address" );
}
}

/**
* Log yourself out and remove the secret session key.
*/
public function logout() {
$this->line( "Logging out of to Pantheon." );
Terminus::line( "Logging out of Pantheon." );
$this->cache->remove('session');
}

/**
* Find out what user you are logged in as.
*/
public function whoami() {
public function whoami() {
if ($this->session) {
$this->line( "You are authenticated as ". $this->session->email );
Terminus::line( "You are authenticated as ". $this->session->email );
}
else {
$this->line( "You are not logged in." );
Terminus::line( "You are not logged in." );
}
}

private function _checkSession() {
if ((!property_exists($this, "session")) || (!property_exists($this->session, "user_uuid"))) {
return false;
}
$results = $this->terminus_request("user", $this->session->user_uuid, "profile", "GET");
if ($results['info']['http_code'] >= 400){
Terminus::line("Expired Session, please re-authenticate.");
$this->cache->remove('session');
Terminus::launch_self("auth", array("login"));
$this->whoami();
return true;
} else {
return (($results['info']['http_code'] <= 199 )||($results['info']['http_code'] >= 300 ))? false : true;
}
}
}

Terminus::add_command( 'auth', 'Auth_Command' );
Expand Down
4 changes: 2 additions & 2 deletions php/commands/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function info( $_, $assoc_args ) {
* @subcommand param-dump
*/
function param_dump() {
echo json_encode( \Terminus::get_configurator()->get_spec() );
echo \Terminus\Utils\json_dump( \Terminus::get_configurator()->get_spec() );
}

/**
Expand All @@ -83,7 +83,7 @@ function param_dump() {
* @subcommand cmd-dump
*/
function cmd_dump() {
echo json_encode( self::command_to_array( Terminus::get_root_command() ) );
echo \Terminus\Utils\json_dump( self::command_to_array( Terminus::get_root_command() ) );
}

/**
Expand Down
Loading

0 comments on commit 393ea04

Please sign in to comment.