Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Bugfixes #80

Merged
merged 8 commits into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions php/Terminus/Dispatcher/Subcommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ private function validate_args( $args, $assoc_args, $extra_args ) {
exit(1);
}

$unknown_positionals = $validator->unknown_positionals( $args );
$invalid = $validator->invalid_positionals($args);
if($invalid) {
\Terminus::error("Invalid positional value: $invalid");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the '' notation? We don't use that in php/class-terminus-command.php, should they be consistent?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

$unknown_positionals = $validator->unknown_positionals($args);
if ( !empty( $unknown_positionals ) ) {
\Terminus::error( 'Too many positional arguments: ' .
implode( ' ', $unknown_positionals ) );
Expand Down Expand Up @@ -220,4 +225,3 @@ function invoke( $args, $assoc_args, $extra_args ) {
call_user_func( $this->when_invoked, $args, array_merge( $extra_args, $assoc_args ) );
}
}

10 changes: 7 additions & 3 deletions php/Terminus/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public function diffstat() {
/**
* @param $element sting code,file,backup
*/
public function backups($element = null) {
if (null === $this->backups ) {
public function backups($element = null, $latest_only = false) {
if (null === $this->backups) {
$path = sprintf("environments/%s/backups/catalog", $this->name);
$response = \Terminus_Command::request('sites', $this->site->getId(), $path, 'GET');
$this->backups = $response['data'];
}
$backups = (array) $this->backups;
ksort($backups);
if ($element) {
foreach ($this->backups as $id => $backup) {
if (!isset($backup->filename)) {
Expand All @@ -66,6 +67,9 @@ public function backups($element = null) {
}
}
}
if ($latest_only) {
return array(array_pop($backups));
}
return $backups;
}

Expand Down Expand Up @@ -149,7 +153,7 @@ public function lock($username, $password) {
'headers' => array('Content-type'=>'application/json')
);
$response = \Terminus_Command::request("sites", $this->site->getId(), 'environments/' . $this->name . '/lock', "PUT", $options);
$response['data'];
return $response['data'];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions php/Terminus/Loggers/Regular.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ static function notify( $message ) {
}

static function redLine($message = " ") {
$cli = new Self('%1%K');
$cli = new self('%1%K');
$message = \cli\Colors::colorize( "%1%K$message%n", $cli->in_color );
$cli->write( STDOUT, "$message\n");
}

static function greenLine($message = " ") {
$cli = new Self('%2%K');
$cli = new self('%2%K');
$message = \cli\Colors::colorize( "%2%K$message%n", $cli->in_color );
$cli->write( STDOUT, "$message\n");
}

static function coloredOutput($message = "", $print = true) {
$cli = new Self('');
$cli = new self('');
// we're not using regex here because simple str_replace is faster. However,
// we may need to go that route if this function gets too complex
$message = str_replace('</M>','%n', str_replace('<M>','%M',$message) );
Expand Down
27 changes: 27 additions & 0 deletions php/Terminus/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,42 @@ public static function getByIndex( $index ) {
return $products[$index];
}

/**
* Search available products by $id
* @param $id string - expects valid uuid-format. i.e. e8fe8550-1ab9-4964-8838-2b9abdccf4bf
*
* @return product array
*/
public static function getById($id) {
$products = self::get(TRUE);
foreach ($products as $product) {
if ($product['id'] == $id) {
return $product;
}
}
return false;
}

/**
* Search available products by $id
* @param $id_or_name string - can be $id or name
*
* @return product array
*/
public static function getByIdOrName($id_or_name) {
$products = self::get(TRUE);
foreach ($products as $product) {
if ($product['id'] == $id_or_name) {
return $product;
}
if (strtolower($product['longname']) == strtolower($id_or_name)) {
return $product;
}
}
return false;
}


public static function instance() {
static $instance;
if ( null === $instance ) {
Expand Down
27 changes: 26 additions & 1 deletion php/Terminus/SynopsisValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Terminus;

use Terminus\Loggers\Regular as Logger;

/**
* Checks if the list of parameters matches the specification defined in the synopsis.
*/
Expand All @@ -28,6 +30,30 @@ public function enough_positionals( $args ) {
return count( $args ) >= count( $positional );
}


public function invalid_positionals($args) {
$positionals = $this->query_spec( array(
'type' => 'positional',
));

for ($i=0;$i<count($args);$i++) {
$token = preg_replace('#\<([a-zA-Z].*)\>.*#s', '$1', $positionals[$i]['token']);
if ("commands" == trim($token)){
// we exit here because the wp and drush commands need to not have validation running since their commands are dependent on their respective code bases.
return false;
}
$regex = "#^($token)$#s";
if (\Terminus::get_config('debug')) {
Logger::coloredOutput("<Y>Positional match $regex</Y>");
}

if (!preg_match($regex, $args[$i])) {
return $args[$i];
}
}
return false;
}

public function unknown_positionals( $args ) {
$positional_repeating = $this->query_spec( array(
'type' => 'positional',
Expand Down Expand Up @@ -125,4 +151,3 @@ private function query_spec( $args, $operator = 'AND' ) {
return $filtered;
}
}

3 changes: 2 additions & 1 deletion php/class-terminus-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ public static function request($realm, $uuid, $path = FALSE, $method = 'GET', $o
// @todo need cache "groups"
$cachekey = md5( Session::getValue('user_uuid').$realm.$path );
$data = $cache->get_data($cachekey);

// check the request cache
if ("GET" == $method AND !Terminus::get_config('nocache') AND !getenv('CLI_TEST_MODE') AND !empty($data)) {
if (Terminus::get_config('debug')) {
Logger::coloredOutput('<Y>Cached Request</Y>');
Logger::coloredOutput('<Y>Cached Request. Key: '.$cachekey.'</Y>');
}
return (array) $data;
}
Expand Down
2 changes: 1 addition & 1 deletion php/commands/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function session_dump() {
* @subcommand cache-clear
*/
public function cache_clear($cache = null) {
$this->cache->flush($cache);
$this->cache->flush($cache,'session');
}

}
Expand Down
38 changes: 26 additions & 12 deletions php/commands/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ public function organizations($args, $assoc_args) {
$orgs = $site->memberships();
break;
}
if (empty($data)) {
Terminus::error("No organizations");
}

// format the data
foreach ($orgs as $org) {
$data[] = array(
Expand All @@ -339,7 +343,7 @@ public function organizations($args, $assoc_args) {
*
* ## OPTIONS
*
* <action>
* <get|load|create>
* : function to run - get,load,or create
*
* [--site=<site>]
Expand All @@ -354,6 +358,9 @@ public function organizations($args, $assoc_args) {
* [--to-directory=<directory>]
* : Download the file if set
*
* [--latest]
* : if set no the latest backup will be selected automatically
*
* ## EXAMPLES
*
*/
Expand All @@ -371,15 +378,14 @@ public function backup($args, $assoc_args) {
if (!in_array($element,array('code','files','database'))) {
Terminus::error("Invalid backup element specified.");
}

$backups = $site->environment($env)->backups($element);
$latest = Input::optional('latest',$assoc_args,false);
$backups = $site->environment($env)->backups($element, $latest);
if (empty($backups)) {
\Terminus::error('No backups available.');
}
$menu = $folders = array();

// build a menu for selecting back ups

foreach( $backups as $folder => $backup ) {
if (!isset($backup->filename)) continue;
if (!isset($backup->folder)) $backup->folder = $folder;
Expand All @@ -388,10 +394,13 @@ public function backup($args, $assoc_args) {
}

if (empty($menu)) {
Terminus::error("No backups available. Create one with `terminus site backup create --site=%s`", array($site->getName()));
Terminus::error("No backups available. Create one with `terminus site backup create --site=%s --env=%s`", array($site->getName(),$env));
}

$index = Terminus::menu($menu, null, "Select backup");
$index = 0;
if (!$latest) {
$index = Terminus::menu($menu, null, "Select backup");
}
$bucket = $buckets[$index];
$filename = $menu[$index];

Expand Down Expand Up @@ -865,7 +874,7 @@ function lock($args, $assoc_args) {
$password = $assoc_args['password'];
}
$data = $site->environment($env)->lock($email, $password);
if ( property_exists($data,'id') ) {
if ( $data AND property_exists($data,'id') ) {
$this->waitOnWorkflow('sites',$data->site_id, $data->id);
}
Terminus::success('Success');
Expand Down Expand Up @@ -1028,7 +1037,11 @@ public function mount($args, $assoc_args) {
public function new_relic($args, $assoc_args) {
$site = SiteFactory::instance(Input::site($assoc_args));
$data = $site->newRelic();
$this->handleDisplay($data->account,$assoc_args,array('Key','Value'));
if($data) {
$this->handleDisplay($data->account,$assoc_args,array('Key','Value'));
} else {
Logger::coloredOutput("%YNew Relic is not enabled.%n");
}
}

/**
Expand Down Expand Up @@ -1138,9 +1151,10 @@ public function redis($args, $assoc_args) {
public function service_level($args, $assoc_args) {
$site = SiteFactory::instance(Input::site($assoc_args));
$info = $site->info('service_level');
if (@$assoc_args['set']) {
$data = $site->updateServiceLevel($assoc_args['set']);
Logger::coloredOutput("%2<K>Service Level has been updated to '$info'%n");
if (isset($assoc_args['set'])) {
$set = $assoc_args['set'];
$data = $site->updateServiceLevel($set);
Logger::coloredOutput("%2<K>Service Level has been updated to '$set'%n");
}
Logger::coloredOutput("%2<K>Service Level is '$info'%n");
return true;
Expand All @@ -1151,7 +1165,7 @@ public function service_level($args, $assoc_args) {
*
* ## OPTIONS
*
* <action>
* <list|add-member|remove-member>
* : i.e. add or remove
*
* [--site=<site>]
Expand Down
30 changes: 20 additions & 10 deletions php/commands/sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ public function show($args, $assoc_args) {
*
* [--product=<productid>]
* : Specify the product to create
*
* [--name=<name>]
* : Name of the site to create (machine-readable)
*
* [--label=<label>]
* : Label for the site
*
* [--org=<org>]
* : UUID of organization to add this site to
*
* [--import=<url>]
* : A url to import a valid archive from
*/
Expand All @@ -77,23 +81,27 @@ public function create($args, $assoc_args) {
}
require_once __DIR__.'/products.php';
if (isset($assoc_args['product'])) {
$product = Products::getById($assoc_args['product']);
$product = Products::getByIdOrName($assoc_args['product']);
if (!$product) {
Terminus::error("Couldn't find product: %s", array($assoc_args['product']));
}
} else {
$product = Terminus::menu( Products::selectList() );
$product = Products::getByIndex($product);
}

Terminus::line( sprintf( "Creating new %s installation ... ", $product['longname'] ) );
$data['product'] = $product['id'];
$options = array( 'body' => json_encode($data) , 'headers'=>array('Content-type'=>'application/json') );
$response = \Terminus_Command::request( "users", Session::getValue('user_uuid'), "sites", 'POST', $options );
// if we made it this far we need to query the work flow to wait for response
$site = $response['data'];
$workflow_id = $site->id;
$result = $this->waitOnWorkFlow( 'sites', $site->site_id, $workflow_id );
$result = $this->waitOnWorkFlow('sites', $site->site_id, $workflow_id);

if( $result ) {
Terminus::success("Pow! You created a new site!");
$this->cache->flush('sites');
$this->cache->flush(null,'session');
}

if (isset($assoc_args['import'])) {
Expand Down Expand Up @@ -142,15 +150,18 @@ public function import($args, $assoc_args) {
}

/**
* Delete site
* Delete a site from pantheon
*
* ## OPTIONS
* --site=<site>
* : Id of the site you want to delete

* : Id of the site you want to delete
*
* [--all]
* : Just kidding ... we won't let you do that.
* : Just kidding ... we won't let you do that.
*
* [--force]
* : to skip the confirmations
* : to skip the confirmations
*
*/
function delete($args, $assoc_args) {
$site_to_delete = SiteFactory::instance(@$assoc_args['site']);
Expand All @@ -164,13 +175,12 @@ function delete($args, $assoc_args) {
$site_to_delete = $sites[$index];
}

if (!isset($assoc_args['force'])) {
if (!isset($assoc_args['force']) AND !Terminus::get_config('yes')) {
// if the force option isn't used we'll ask you some annoying questions
Terminus::confirm( sprintf( "Are you sure you want to delete %s?", $site_to_delete->information->name ));
Terminus::confirm( "Are you really sure?" );
}
Terminus::line( sprintf( "Deleting %s ...", $site_to_delete->information->name ) );

$response = \Terminus_Command::request( 'sites', $site_to_delete->id, '', 'DELETE' );

Terminus::launch_self("sites",array('show'),array('nocache'=>1));
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fi

cd code

if [ -f .git/config ]; then
if [ -e .git/config ]; then
git pull origin master
else
git clone $GIT_REMOTE .
Expand Down