Skip to content

Commit

Permalink
Clean up most code style warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstauffer committed Apr 26, 2023
1 parent fa689d3 commit 4805518
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 38 deletions.
13 changes: 10 additions & 3 deletions app/Commands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function extractPassthroughOptions(array $arguments): array
*/
public function removeOptions(array $arguments): array
{
$arguments = collect($arguments)->reject(fn ($argument) => str_starts_with($argument, '--') && strlen($argument) > 2)->values()->toArray();
$arguments = collect($arguments)
->reject(fn ($argument) => str_starts_with($argument, '--') && strlen($argument) > 2)
->values()
->toArray();

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

Expand Down Expand Up @@ -215,8 +218,12 @@ public function enableableServicesByCategory(): array
->toArray();
}

public function enable(string $service, bool $useDefaults = false, array $passthroughOptions = [], string $runOptions = null): void
{
public function enable(
string $service,
bool $useDefaults = false,
array $passthroughOptions = [],
string $runOptions = null
): void {
$fqcn = $this->services->get($service);
app($fqcn)->enable($useDefaults, $passthroughOptions, $runOptions);
}
Expand Down
24 changes: 11 additions & 13 deletions app/Services/Traefik.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class Traefik extends BaseService
'shortname' => 'websecure_port',
'prompt' => 'What is the websecure port?',
'default' => '443',
]
],
];

protected $dockerRunTemplate = '-p "${:port}":8080 -p "${:web_port}":80 -p "${:websecure_port}":443 \
-v "${:config_dir}":/etc/traefik \
-v /var/run/docker.sock:/var/run/docker.sock \
"${:organization}"/"${:image_name}":"${:tag}" \
--api.insecure=true --providers.docker=true --entryPoints.web.address=:80 --entryPoints.websecure.address=:443 --providers.file.directory=/etc/traefik/conf --providers.file.watch=true';

--api.insecure=true --providers.docker=true --entryPoints.web.address=:80 --entryPoints.websecure.address=:443 \
--providers.file.directory=/etc/traefik/conf --providers.file.watch=true';

public function __construct(Shell $shell, Environment $environment, Docker $docker)
{
Expand All @@ -57,7 +57,7 @@ public function __construct(Shell $shell, Environment $environment, Docker $dock

$this->prompts = array_map(function ($prompt) use ($home) {
if ($prompt['shortname'] === 'config' && ! empty($home)) {
$prompt['default'] = "$home/.config/traefik";
$prompt['default'] = "{$home}/.config/traefik";
}

return $prompt;
Expand All @@ -66,20 +66,18 @@ public function __construct(Shell $shell, Environment $environment, Docker $dock

protected function homeDirectory(): ?string
{
// Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
// getenv('HOME') isn't set on Windows and generates a Notice.
// Cannot use $_SERVER superglobal since that's empty during
// UnitUnishTestCase; getenv('HOME') isn't set on Windows and generates
// a Notice.
$home = getenv('HOME');

if (! empty($home)) {
// home should never end with a trailing slash.
// Trim the trailing slash
$home = rtrim($home, '/');
}

elseif (! empty($_SERVER['HOMEDRIVE']) && ! empty($_SERVER['HOMEPATH'])) {
// home on windows
} elseif (! empty($_SERVER['HOMEDRIVE']) && ! empty($_SERVER['HOMEPATH'])) {
// home directory on windows
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
// If HOMEPATH is a root directory the path can end with a slash. Make sure
// that doesn't happen.
// Trim the trailing slash
$home = rtrim($home, '\\/');
}

Expand Down
8 changes: 6 additions & 2 deletions app/Shell/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class Docker
protected $networking;
protected $environment;

public function __construct(Shell $shell, DockerFormatter $formatter, DockerNetworking $networking, Environment $environment)
{
public function __construct(
Shell $shell,
DockerFormatter $formatter,
DockerNetworking $networking,
Environment $environment
) {
$this->shell = $shell;
$this->formatter = $formatter;
$this->networking = $networking;
Expand Down
3 changes: 2 additions & 1 deletion app/Shell/DockerNetworking.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function ensureNetworkCreated($name = 'takeout'): void
public function baseAliasExists(string $name): bool
{
$output = $this->shell->execQuietly(
'docker ps --filter "label=com.tighten.takeout.Base_Alias=' . $name . '" --format "table {{.ID}}|{{.Names}}"'
'docker ps --filter "label=com.tighten.takeout.Base_Alias=' . $name . '" --format \n
"table {{.ID}}|{{.Names}}"'
)->getOutput();

$collection = $this->formatter->rawTableOutputToCollection($output);
Expand Down
3 changes: 2 additions & 1 deletion app/Shell/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function portIsAvailable($port): bool
$netstatCmd = $this->netstatCmd();

// Check to see if the system is running a service with the desired port
$process = $this->shell->execQuietly("{$netstatCmd} -vanp tcp | grep '{$portText}' | grep -v 'TIME_WAIT' | grep -v 'CLOSE_WAIT' | grep -v 'FIN_WAIT'");
$process = $this->shell->execQuietly("{$netstatCmd} -vanp tcp \n
| grep '{$portText}' | grep -v 'TIME_WAIT' | grep -v 'CLOSE_WAIT' | grep -v 'FIN_WAIT'");

// A successful netstat command means a port in use was found
return ! $process->isSuccessful();
Expand Down
40 changes: 30 additions & 10 deletions tests/Feature/BaseServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ public function it_enables_services()

app()->instance('console', M::mock(Command::class, function ($mock) use ($service) {
$defaultPort = $service->defaultPort();
$mock->shouldReceive('ask')->with('Which host port would you like meilisearch to use?', $defaultPort)->andReturn(7700);
$mock->shouldReceive('ask')->with('What is the Docker volume name?', 'meili_data')->andReturn('meili_data');
$mock->shouldReceive('ask')->with('Which tag (version) of meilisearch would you like to use?', 'latest')->andReturn('v1.1.1');
$mock->shouldReceive('ask')
->with('Which host port would you like meilisearch to use?', $defaultPort)
->andReturn(7700);
$mock->shouldReceive('ask')
->with('What is the Docker volume name?', 'meili_data')
->andReturn('meili_data');
$mock->shouldReceive('ask')
->with('Which tag (version) of meilisearch would you like to use?', 'latest')
->andReturn('v1.1.1');
$mock->shouldIgnoreMissing();
}));

Expand Down Expand Up @@ -73,9 +79,15 @@ public function it_can_receive_a_custom_image_in_the_tag()

app()->instance('console', M::mock(Command::class, function ($mock) use ($service) {
$defaultPort = $service->defaultPort();
$mock->shouldReceive('ask')->with('Which host port would you like postgres to use?', $defaultPort)->andReturn(5432);
$mock->shouldReceive('ask')->with('Which tag (version) of postgres would you like to use?', 'latest')->andReturn('timescale/timescaledb:latest-pg12');
$mock->shouldReceive('ask')->with('What is the Docker volume name?', 'postgres_data')->andReturn('postgres_data');
$mock->shouldReceive('ask')
->with('Which host port would you like postgres to use?', $defaultPort)
->andReturn(5432);
$mock->shouldReceive('ask')
->with('Which tag (version) of postgres would you like to use?', 'latest')
->andReturn('timescale/timescaledb:latest-pg12');
$mock->shouldReceive('ask')
->with('What is the Docker volume name?', 'postgres_data')
->andReturn('postgres_data');
$mock->shouldIgnoreMissing();
}));

Expand Down Expand Up @@ -120,8 +132,12 @@ public function it_can_receive_container_arguments_via_passthrough()

app()->instance('console', M::mock(Command::class, function ($mock) use ($service) {
$defaultPort = $service->defaultPort();
$mock->shouldReceive('ask')->with('Which host port would you like _test_image to use?', $defaultPort)->andReturn(12345);
$mock->shouldReceive('ask')->with('Which tag (version) of _test_image would you like to use?', 'latest')->andReturn('latest');
$mock->shouldReceive('ask')
->with('Which host port would you like _test_image to use?', $defaultPort)
->andReturn(12345);
$mock->shouldReceive('ask')
->with('Which tag (version) of _test_image would you like to use?', 'latest')
->andReturn('latest');
$mock->shouldIgnoreMissing();
}));

Expand Down Expand Up @@ -169,8 +185,12 @@ public function it_accepts_run_options_and_passthrough_options()

app()->instance('console', M::mock(Command::class, function ($mock) use ($service) {
$defaultPort = $service->defaultPort();
$mock->shouldReceive('ask')->with('Which host port would you like _test_image to use?', $defaultPort)->andReturn(12345);
$mock->shouldReceive('ask')->with('Which tag (version) of _test_image would you like to use?', 'latest')->andReturn('latest');
$mock->shouldReceive('ask')
->with('Which host port would you like _test_image to use?', $defaultPort)
->andReturn(12345);
$mock->shouldReceive('ask')
->with('Which tag (version) of _test_image would you like to use?', 'latest')
->andReturn('latest');
$mock->shouldIgnoreMissing();
}));

Expand Down
10 changes: 8 additions & 2 deletions tests/Feature/EnableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,16 @@ function it_removes_options()
/** @test */
function it_extracts_passthrough_options()
{
$cli = explode(' ', "./takeout enable meilisearch postgresql mysql --default -- -e 'abc' --other-flag -t \"double-quote\"");
$cli = explode(
' ',
"./takeout enable meilisearch postgresql mysql --default -- -e 'abc' --other-flag -t \"double-quote\""
);

$command = new EnableCommand;

$this->assertEquals(['-e', "'abc'", '--other-flag', '-t', '"double-quote"'], $command->extractPassthroughOptions($cli));
$this->assertEquals(
['-e', "'abc'", '--other-flag', '-t', '"double-quote"'],
$command->extractPassthroughOptions($cli)
);
}
}
24 changes: 18 additions & 6 deletions tests/Feature/ServiceEnableDefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ function it_enables_service_when_default_flag_is_provided()
{
app()->instance('console', M::mock(Command::class, function ($mock) {
$defaultPort = app(MailHog::class)->defaultPort();
$mock->shouldReceive('ask')->with('Which host port would you like this service to use?', $defaultPort)->andReturn(7700);
$mock->shouldReceive('ask')->with('Which host port would you like this service to use?', $defaultPort)->andReturn(77001);
$mock->shouldReceive('ask')->with('Which tag (version) of this service would you like to use?', 'latest')->andReturn('latest');
$mock->shouldReceive('ask')
->with('Which host port would you like this service to use?', $defaultPort)
->andReturn(7700);
$mock->shouldReceive('ask')
->with('Which host port would you like this service to use?', $defaultPort)
->andReturn(77001);
$mock->shouldReceive('ask')
->with('Which tag (version) of this service would you like to use?', 'latest')
->andReturn('latest');
$mock->shouldIgnoreMissing();
}));

Expand Down Expand Up @@ -56,9 +62,15 @@ public function can_enable_with_run_options()
$runOptions = '--restart unless-stopped -e "LOREM=IPSUM"';
app()->instance('console', M::mock(Command::class, function ($mock) {
$defaultPort = app(MailHog::class)->defaultPort();
$mock->shouldReceive('ask')->with('Which host port would you like this service to use?', $defaultPort)->andReturn(7700);
$mock->shouldReceive('ask')->with('Which host port would you like this service to use?', $defaultPort)->andReturn(77001);
$mock->shouldReceive('ask')->with('Which tag (version) of this service would you like to use?', 'latest')->andReturn('latest');
$mock->shouldReceive('ask')
->with('Which host port would you like this service to use?', $defaultPort)
->andReturn(7700);
$mock->shouldReceive('ask')
->with('Which host port would you like this service to use?', $defaultPort)
->andReturn(77001);
$mock->shouldReceive('ask')
->with('Which tag (version) of this service would you like to use?', 'latest')
->andReturn('latest');
$mock->shouldIgnoreMissing();
}));

Expand Down

0 comments on commit 4805518

Please sign in to comment.