Skip to content

Commit

Permalink
Add print hosts entries command.
Browse files Browse the repository at this point in the history
  • Loading branch information
kporras07 committed May 9, 2020
1 parent 94a3263 commit c77d6d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bin/chirripo.php
Expand Up @@ -20,6 +20,7 @@
use Console\App\Commands\DbImportCommand;
use Console\App\Commands\PhpModulesCommand;
use Console\App\Commands\PhpInfoCommand;
use Console\App\Commands\PrintHostsEntries;

define('CHIRRIPO_VERSION', '0.3');

Expand Down Expand Up @@ -50,6 +51,7 @@ function chirripo_main() {
$app->add(new DbImportCommand());
$app->add(new PhpModulesCommand());
$app->add(new PhpInfoCommand());
$app->add(new PrintHostsEntries());

$app->run();
}
66 changes: 66 additions & 0 deletions src/App/Commands/PrintHostsEntries.php
@@ -0,0 +1,66 @@
<?php

namespace Console\App\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

/**
* Url Command class.
*/
class PrintHostsEntries extends Command
{
use ChirripoCommandTrait;

protected $availableServices;

protected function configure()
{
$this->availableServices = [
'nginx',
'varnish',
'solr',
'mailhog',
];
$this->setName('hosts')
->setDescription('Get hosts entries if you are using proxy');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setupEnv();

$hosts_entry = '';

if (!empty($_ENV['VIRTUAL_HOST'])) {
$hosts_entry = '127.0.0.1 ';
$hosts = [];
foreach ($this->availableServices as $service) {
if ($service === 'nginx') {
$hosts[] = $_ENV['VIRTUAL_HOST'];
} else {
$hosts[] = $service . '.' . $_ENV['VIRTUAL_HOST'];
}
}
$hosts_entry .= implode(' ', $hosts);
}
if (!empty($_ENV['OTHER_VIRTUAL_HOSTS'])) {
$hosts_entry .= ' ' . $_ENV['OTHER_VIRTUAL_HOSTS'];
}

if ($hosts_entry) {
$output->writeln(sprintf(
"Add the following line to your hosts file (/etc/hosts for Linux and Mac)\n\n%s",
$hosts_entry
));
} else {
$output->writeln(sprintf('You need to define VIRTUAL_HOST and optionally OTHER_VIRTUAL_HOSTS
to use domain names'));
exit(1);
}
}
}
3 changes: 2 additions & 1 deletion src/App/Commands/UrlCommand.php
Expand Up @@ -29,7 +29,7 @@ protected function configure()
$this->setName('url')
->setDescription('Get url for given service')
->setHelp(sprintf('Available services:%s', implode(', ', array_keys($this->availableServices))))
->addArgument('service', InputArgument::OPTIONAL, 'Pass the service to lookup the logs for.', 'nginx');
->addArgument('service', InputArgument::OPTIONAL, 'Pass the service to lookup the url for.', 'nginx');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -45,6 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (isset($this->availableServices[$service])) {
$url .= $this->availableServices[$service];
$output->writeln(sprintf('%s', $url));
$output->writeln(sprintf('If proxy is in use, you could also access this service at %s', $domain_url));
} else {
$output->writeln(sprintf(
"Service %s not defined",
Expand Down

0 comments on commit c77d6d4

Please sign in to comment.