Skip to content

Commit

Permalink
Add domain command to modify hosts file directly 🤙
Browse files Browse the repository at this point in the history
  • Loading branch information
kilrizzy committed Jul 29, 2017
1 parent 74d0e8e commit 29b9d7a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 3 deletions.
131 changes: 131 additions & 0 deletions app/Commands/Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace App\Commands;

use App\Actions\ComposerCreateProject;
use App\Actions\HomesteadAddDatabase;
use App\Actions\HomesteadMapSite;
use App\Actions\HostsAddLine;
use App\Actions\ProvisionHomestead;
use App\Commands\Options\DatabaseOption;
use App\Commands\Options\DomainOption;
use App\Commands\Options\ProjectNameOption;
use App\Commands\Options\SkipConfirmationOption;
use App\Commands\Options\UseComposerOption;
use App\Commands\Options\UseDefaultsOption;
use App\Configuration\Config;
use App\Formatters\DatabaseNameFormatter;
use App\Formatters\DomainFormatter;
use App\Input\Interrogator;
use App\Support\Traits\HasCommandOptions;
use App\Support\Traits\RequireEnvFile;
use App\Support\Vagrant\Vagrant;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Domain extends Command
{

use RequireEnvFile;
use HasCommandOptions;

private $questionHelper;
private $inputInterface;
private $outputInterface;

private $config;

private $ipAddress;
private $domain;

private $interrogator;

private $vagrant;

public function __construct($name = null, Config $config)
{
$this->config = $config;
parent::__construct($name);
}

protected function configure()
{
$this
->setName('domain')
->setDescription('Update hosts file to map new domain')
->setHelp("");
}

private function init(InputInterface $input, OutputInterface $output){
$this->inputInterface = $input;
$this->outputInterface = $output;
$this->hasDotEnvFile();
$this->questionHelper = $this->getHelper('question');
$this->interrogator = new Interrogator($input, $output, $this->getHelper('question'));
$vagrantAccessDirectoryCommand = 'cd '.$this->config->getHomesteadBoxPath();
if(!empty($this->config->getHomesteadAccessDirectoryCommand())){
$vagrantAccessDirectoryCommand = $this->config->getHomesteadAccessDirectoryCommand();
}
$this->vagrant = new Vagrant($vagrantAccessDirectoryCommand);
}

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

$taskConfirmation = $this->getTaskConfirmationFromQuestion();

if($taskConfirmation){
$this->runTasks();
}else{
$output->writeln('<error>Tasks cancelled</error>');
}

return;

}

private function interrogate(){

$this->domain = $this->interrogator->ask(
'Domain?',
'project-'.time().$this->config->getDomainExtension()
);

$this->ipAddress = $this->interrogator->ask(
'IP Address?',
$this->config->getHostIP()
);

}

private function hostsAddLineAction(){
return new HostsAddLine($this->config->getHostsPath(), $this->ipAddress, $this->domain);
}

private function getTaskConfirmationFromQuestion(){
$this->outputInterface->writeln('<info>The following tasks will be executed:</info>');

$this->outputInterface->writeln('- '.$this->hostsAddLineAction()->confirmationMessage());

$response = $this->interrogator->ask(
'Run tasks?',
'Y'
);
if(strtoupper($response) == 'Y'){
return true;
}
return false;
}

private function runTasks(){

$this->outputInterface->writeln('<info>'.$this->hostsAddLineAction()->actionMessage().'...</info>');
$this->hostsAddLineAction()->run();
$this->outputInterface->writeln('');
$this->outputInterface->writeln('<info>Complete!</info>');
}

}
2 changes: 2 additions & 0 deletions homeboy
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ $setupCommand = new \App\Commands\Setup(null, $config);
$hostCommand = new \App\Commands\Host(null, $config);
$vagrantCommand = new \App\Commands\Vagrant(null, $config);
$fileCommand = new \App\Commands\File(null, $config);
$domainCommand = new \App\Commands\Domain(null, $config);

$application->add($setupCommand);
$application->add($hostCommand);
$application->add($vagrantCommand);
$application->add($fileCommand);
$application->add($domainCommand);

$application->setDefaultCommand($hostCommand->getName());

Expand Down
11 changes: 8 additions & 3 deletions tests/Commands/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DomainTest extends \Tests\AppTestCase\AppTestCase
public function test_add()
{

/*$hostsFileContents = "192.168.10.10 example1.app";
$hostsFileContents = "192.168.10.10 example1.app";

$hostsFile = $this->createTemporaryFile($hostsFileContents);
$hostsFilePath = $this->getTemporaryFilePath($hostsFile);
Expand All @@ -19,12 +19,17 @@ public function test_add()

$command = $application->find('domain');
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
$commandTester->setInputs([
'demo.dev',
'10.10.10.10',
'y'
]);
$commandTester->execute(array(
'command' => $command->getName(),
));
$output = $commandTester->getDisplay();
print_r($output);
//$this->assertContains($hostsFileContents, $output);*/
$expectedHostFileContents = '10.10.10.10 demo.dev';
$this->assertContains($expectedHostFileContents, file_get_contents($hostsFilePath));
}

}

0 comments on commit 29b9d7a

Please sign in to comment.