diff --git a/app/Commands/Domain.php b/app/Commands/Domain.php new file mode 100644 index 0000000..03bfe82 --- /dev/null +++ b/app/Commands/Domain.php @@ -0,0 +1,131 @@ +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('Tasks cancelled'); + } + + 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('The following tasks will be executed:'); + + $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(''.$this->hostsAddLineAction()->actionMessage().'...'); + $this->hostsAddLineAction()->run(); + $this->outputInterface->writeln(''); + $this->outputInterface->writeln('Complete!'); + } + +} \ No newline at end of file diff --git a/homeboy b/homeboy index 665dd66..45f07b4 100755 --- a/homeboy +++ b/homeboy @@ -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()); diff --git a/tests/Commands/DomainTest.php b/tests/Commands/DomainTest.php index d408714..f99b000 100644 --- a/tests/Commands/DomainTest.php +++ b/tests/Commands/DomainTest.php @@ -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); @@ -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)); } } \ No newline at end of file