Skip to content

Commit

Permalink
Add support for host aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
gbalduzzi committed May 24, 2023
1 parent 6294854 commit 810e173
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": [ "laravel", "docker", "cli" ],
"type": "library",
"license": "MIT",
"version": "0.1.1",
"version": "0.2.0",
"bin": [
"bin/skipper"
],
Expand Down
28 changes: 28 additions & 0 deletions src/Commands/ProjectCmds/EditCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ protected function handle(): int
return Command::SUCCESS;
}

$hostAlias = $this->validateHostAliases();

$composeFile = $this->validateComposeFile();
if (empty($composeFile)) {
return Command::SUCCESS;
Expand All @@ -45,6 +47,7 @@ protected function handle(): int
path: $this->project->path,
name: $name,
host: $host,
hostAlias: $hostAlias,
composeFile: $composeFile,
envFile: $envFile,
httpContainer: $httpContainer,
Expand Down Expand Up @@ -143,6 +146,31 @@ private function validateHost(): string|null
return $host;
}

private function validateHostAliases(): array
{
$hostAliases = $this->io->ask('Host aliases (do not include http/https). Separate each host with a space', implode(' ', $this->project->hostAlias));

$hostAliases = explode(' ', trim($hostAliases));

foreach ($hostAliases as $hostAlias) {
if (str_starts_with($hostAlias, 'http')) {
$this->io->error([
'Alias should not contain protocol http',
$hostAlias,
]);

return [];
}
}

if (!empty($hostAliases)) {
$this->io->writeln('<comment>Be sure not to use as alias a host already in use by a different project</comment>');
$this->io->writeln('<comment>You need to manually add the aliases to the host file using command <info>skipper host [alias]</info></comment>');
}

return $hostAliases;
}

private function validateComposeFile(): string|null
{
$composeFile = $this->io->ask('Relative path to the docker-compose file', $this->project->composeFile);
Expand Down
4 changes: 4 additions & 0 deletions src/Config/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public string $path,
public string $name,
public string $host,
public array $hostAlias = [],
public string $composeFile = 'docker/docker-compose.yml',
public string $envFile = 'docker/.env',
public string $httpContainer = 'nginx',
Expand All @@ -24,6 +25,7 @@ public static function from(array $data): self
path: $data['path'],
name: $data['name'],
host: $data['host'],
hostAlias: $data['hostAlias'] ?? [],
composeFile: $data['composeFile'] ?? 'docker/docker-compose.yml',
envFile: $data['envFile'] ?? 'docker/.env',
httpContainer: $data['httpContainer'] ?? 'nginx',
Expand Down Expand Up @@ -97,6 +99,7 @@ public function definitionList(): array
return [
['name' => $this->name],
['host' => $this->host],
['hostAlias' => implode(', ', $this->hostAlias)],
['path' => $this->path],
['composeFile' => $this->composeFile],
['envFile' => $this->envFile ?: ''],
Expand Down Expand Up @@ -125,6 +128,7 @@ public function toArray(): array
'path' => $this->path,
'name' => $this->name,
'host' => $this->host,
'hostAlias' => $this->hostAlias,
'composeFile' => $this->composeFile,
'envFile' => $this->envFile,
'httpContainer' => $this->httpContainer,
Expand Down
13 changes: 13 additions & 0 deletions src/Config/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function writeCaddyfile(Config $config): void
EOD;

foreach ($config->projects as $project) {

$fileContent .= <<<EOD
{$project->host}:443 {
Expand All @@ -50,6 +51,18 @@ public function writeCaddyfile(Config $config): void
}
EOD;
foreach ($project->hostAlias as $alias) {
$fileContent .= <<<EOD
{$alias}:443 {
encode gzip
reverse_proxy {$project->name}-{$project->httpContainer}-1:80 {
header_up X-Real-IP {remote_host}
}
}
EOD;
}
}

file_put_contents($this->caddyfilePath(), $fileContent);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/HostFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private function findHostLine(array $lines): int|null

private function writeFile(array $lines): int
{
$this->io->writeln('We need <info>sudo</info> permissions to create a backup copy of your hosts file and to remove the host');
$this->io->writeln('We need <info>sudo</info> permissions to create a backup copy of your hosts file');
$this->io->writeln('🔐 You may be prompted for your password');

if (Execute::onShell(['sudo', 'cp', '/etc/hosts', '/etc/hosts.bkp'], false) !== Command::SUCCESS) {
Expand Down

0 comments on commit 810e173

Please sign in to comment.