Skip to content

Commit

Permalink
Validate positional arguments against privided token, resolves #79
Browse files Browse the repository at this point in the history
Allow validator to skip the <commands> token to allow wp and drush commands to work as expected
  • Loading branch information
mikevanwinkle committed Jan 29, 2015
1 parent f8e52be commit 3d14e7c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
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");
}

$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 ) );
}
}

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;
}
}

8 changes: 6 additions & 2 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 Down Expand Up @@ -1151,7 +1155,7 @@ public function service_level($args, $assoc_args) {
*
* ## OPTIONS
*
* <action>
* <list|add-member|remove-member>
* : i.e. add or remove
*
* [--site=<site>]
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

0 comments on commit 3d14e7c

Please sign in to comment.