Skip to content

Commit

Permalink
Merge pull request #294 from tighten/mes/pass-params-along
Browse files Browse the repository at this point in the history
Allow passing through additional options after the enable command
  • Loading branch information
mattstauffer authored Jul 12, 2022
2 parents 1247c60 + f7d6a19 commit 191c6a0
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 6,783 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.phpunit.result.cache
/storage
phpunit.xml
composer.lock
69 changes: 64 additions & 5 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,13 +27,14 @@ public function handle(Environment $environment, Services $services): void
$this->services = $services;
$this->initializeCommand();

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

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

if (filled($services)) {
foreach ($services as $service) {
$this->enable($service, $useDefaults);
$this->enable($service, $useDefaults, $passthroughOptions);
}

return;
Expand All @@ -44,7 +46,64 @@ public function handle(Environment $environment, Services $services): void
return;
}

$this->enable($option, $useDefaults);
$this->enable($option, $useDefaults, $passthroughOptions);
}

/**
* Since we're pulling the *full* list of server arguments, not just relying on
* $this->argument, we have to do our own manual overriding for testing scenarios,
* because pulling $_SERVER['argv'] won't give the right results in testing.
*/
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 options or passthrough options from the parameters list, returning
* just the parameters passed to `enable`
*
* @param array $arguments
* @return array
*/
public function removeOptions(array $arguments): array
{
$arguments = collect($arguments)->reject(fn ($argument) => str_starts_with($argument, '--') && strlen($argument) > 2)->values()->toArray();

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

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

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

return array_slice($arguments, $start);
}

private function selectService(): ?string
Expand Down Expand Up @@ -146,9 +205,9 @@ public function enableableServicesByCategory(): array
->toArray();
}

public function enable(string $service, bool $useDefaults = false): void
public function enable(string $service, bool $useDefaults = false, array $passthroughOptions = []): void
{
$fqcn = $this->services->get($service);
app($fqcn)->enable($useDefaults);
app($fqcn)->enable($useDefaults, $passthroughOptions);
}
}
13 changes: 11 additions & 2 deletions app/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function name(): string
return static::$displayName ?? Str::afterLast(static::class, '\\');
}

public function enable(bool $useDefaults = false): void
public function enable(bool $useDefaults = false, array $passthroughOptions = []): void
{
$this->useDefaults = $useDefaults;

Expand All @@ -79,7 +79,7 @@ public function enable(bool $useDefaults = false): void

try {
$this->docker->bootContainer(
$this->sanitizeDockerRunTemplate($this->dockerRunTemplate),
$this->sanitizeDockerRunTemplate($this->dockerRunTemplate) . $this->buildPassthroughOptionsString($passthroughOptions),
$this->buildParameters()
);

Expand Down Expand Up @@ -225,4 +225,13 @@ public function sanitizeDockerRunTemplate($dockerRunTemplate): string

return $dockerRunTemplate;
}

public function buildPassthroughOptionsString(array $passthroughOptions): string
{
if (empty($passthroughOptions)) {
return '';
}

return ' ' . join(' ', $passthroughOptions);
}
}
Loading

0 comments on commit 191c6a0

Please sign in to comment.