Skip to content

Commit

Permalink
Merge pull request #47 from moderntribe/fix/tric-target-or-fail
Browse files Browse the repository at this point in the history
Require a target for commands that require it
  • Loading branch information
lucatume authored Oct 5, 2020
2 parents bd54153 + 980e3df commit d74c184
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.8] - 2020-09-23
### Changed

- Add a check to ensure a target is set for commands that require it.

## [0.5.7] - 2020-08-28
### Changed

Expand Down
4 changes: 2 additions & 2 deletions src/commands/cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
if ( $is_help ) {
echo "Runs a Codeception command in the stack, the equivalent of <light_cyan>'codecept ...'</light_cyan>.\n";
echo PHP_EOL;
echo colorize( "This command requires a set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "This command requires a use target set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "usage: <light_cyan>{$cli_name} cc [...<commands>]</light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} cc generate:wpunit wpunit Foo</light_cyan>" );
return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

setup_id();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

$command = $args( '...' );
Expand Down
2 changes: 1 addition & 1 deletion src/commands/npm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

$command = $args( '...' );
Expand Down
2 changes: 1 addition & 1 deletion src/commands/npm_lts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

$command = $args( '...' );
Expand Down
3 changes: 2 additions & 1 deletion src/commands/phpcbf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
if ( $is_help ) {
echo "Runs PHP Code Beautifer and Fixer against the current use target.\n";
echo PHP_EOL;
echo colorize( "This command requires a use target set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "usage: <light_cyan>{$cli_name} phpcbf [...<commands>]</light_cyan>\n" );
return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

setup_id();
Expand Down
3 changes: 2 additions & 1 deletion src/commands/phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
if ( $is_help ) {
echo "Runs PHP_CodeSniffer against the current use target.\n";
echo PHP_EOL;
echo colorize( "This command requires a use target set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "usage: <light_cyan>{$cli_name} phpcs [...<commands>]</light_cyan>\n" );
return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

setup_id();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
if ( $is_help ) {
echo colorize( "Runs a Codeception test in the stack, the equivalent of <light_cyan>'codecept run ...'</light_cyan>.\n" );
echo PHP_EOL;
echo colorize( "This command requires a set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "This command requires a use target set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "usage: <light_cyan>{$cli_name} run [...<commands>]</light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} run wpunit</light_cyan>" );

return;
}

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

setup_id();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if ( $is_help ) {
echo "Opens a shell in a stack service, defaults to the 'codeception' one.\n";
echo PHP_EOL;
echo colorize( "This command requires a set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "This command requires a use target set using the <light_cyan>use</light_cyan> command.\n" );
echo colorize( "usage: <light_cyan>{$cli_name} shell [<service>]</light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} shell chrome</light_cyan>\n" );
return;
Expand All @@ -14,7 +14,7 @@
$service_args = args( [ 'service', '...' ], $args( '...' ), 0 );
$service = $service_args( 'service', 'codeception' );

$using = tric_target();
$using = tric_target_or_fail();
echo light_cyan( "Using {$using}\n" );

setup_id();
Expand Down
22 changes: 22 additions & 0 deletions src/tric.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,25 @@ function build_targets_command_pool( array $targets, $base_command, array $comma

return $command_pool;
}

/**
* Returns the current target or exits if no target is set.
*
* @param string|null $reason The colorized reason why the target should be set.
*
* @return string The current target, if set, else the function will exit.
*/
function tric_target_or_fail( $reason = null ) {
$target = tric_target();

if ( empty( $target ) ) {
$reason = $reason
?: magenta( 'This command requires a target set using the ' )
. light_cyan( 'use' )
. magenta( ' command.' );
echo colorize( $reason . PHP_EOL );
exit( 1 );
}

return $target;
}

0 comments on commit d74c184

Please sign in to comment.