Skip to content

Commit

Permalink
Allow passthrough options with single dash; test passthrough option e…
Browse files Browse the repository at this point in the history
…xtraction and removal
  • Loading branch information
mattstauffer committed Jul 5, 2022
1 parent 86e557e commit 9abaa61
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
60 changes: 47 additions & 13 deletions app/Commands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Services;
use App\Shell\Environment;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;

Expand All @@ -26,16 +27,11 @@ public function handle(Environment $environment, Services $services): void
$this->services = $services;
$this->initializeCommand();

$services = $this->removePassthroughOptions($this->argument('serviceNames'));
$services = $this->removeOptions($this->serverArguments());
$passthroughOptions = $this->extractPassthroughOptions($this->serverArguments());

$useDefaults = $this->option('default');

// Extract passthrough options, if provided, passed after "-- "
$passthroughOptions = [];
if (in_array('--', $_SERVER['argv'])) {
$passthroughOptions = array_slice($_SERVER['argv'], array_search('--', $_SERVER['argv']) + 1);
}

if (filled($services)) {
foreach ($services as $service) {
$this->enable($service, $useDefaults, $passthroughOptions);
Expand All @@ -53,17 +49,55 @@ public function handle(Environment $environment, Services $services): void
$this->enable($option, $useDefaults, $passthroughOptions);
}

public function serverArguments(): array
{
if (App::environment() === 'testing') {
$string = array_merge(['takeout', 'enable'], $this->argument('serviceNames'));

if ($this->option('default')) {
$string[] = '--default';
}

return $string;
}

return $_SERVER['argv'];
}

/**
* Extract and return any passthrough options from the parameters list
*
* @param array $arguments
* @return array
*/
public function extractPassthroughOptions(array $arguments): array
{
if (! in_array('--', $arguments)) {
return [];
}

return array_slice($arguments, array_search('--', $arguments) + 1);
}

/**
* Remove any passthrough options from the parameters list
* Remove any options or passthrough options from the parameters list
*
* @param array $serviceNames
* @param array $arguments
* @return array
*/
protected function removePassthroughOptions(array $serviceNames): array
public function removeOptions(array $arguments): array
{
return collect($serviceNames)->reject(function ($item) {
return str_starts_with($item, '--');
})->all();
$arguments = collect($arguments)->reject(fn ($argument) => str_starts_with($argument, '--') && strlen($argument) > 2)->toArray();

$start = array_search('enable', $arguments) + 1;

if (in_array('--', $arguments)) {
$length = array_search('--', $arguments) - $start - 1;

return array_slice($arguments, $start, $length);
}

return array_slice($arguments, $start);
}

private function selectService(): ?string
Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
|
*/

'env' => 'development',
'env' => env('APP_ENV', 'development'),

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<server name="APP_ENV" value="testing"/>
</php>
</phpunit>
21 changes: 21 additions & 0 deletions tests/Feature/EnableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Commands\EnableCommand;
use App\Exceptions\InvalidServiceShortnameException;
use App\Services;
use App\Services\MeiliSearch;
Expand Down Expand Up @@ -172,4 +173,24 @@ function it_displays_error_if_invalid_shortname_passed()
$this->artisan('enable asdfasdfadsfasdfadsf')
->assertExitCode(0);
}

/** @test */
function it_removes_options()
{
$cli = explode(' ', "./takeout enable meilisearch postgresql mysql --default -- -e 'abc' --other-flag");

$command = new EnableCommand;

$this->assertEquals(['meilisearch', 'postgresql', 'mysql'], $command->removeOptions($cli));
}

/** @test */
function it_extracts_passthrough_options()
{
$cli = explode(' ', "./takeout enable meilisearch postgresql mysql --default -- -e 'abc' --other-flag");

$command = new EnableCommand;

$this->assertEquals(['-e', "'abc'", '--other-flag'], $command->extractPassthroughOptions($cli));
}
}

0 comments on commit 9abaa61

Please sign in to comment.