|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Kiwicom\Loopbind\Commands; |
| 4 | + |
| 5 | +use Kiwicom\Loopbind\Config\Config; |
| 6 | +use Kiwicom\Loopbind\Config\ConfigLoader; |
| 7 | +use Kiwicom\Loopbind\Constants\ExitCodes; |
| 8 | +use Kiwicom\Loopbind\Exceptions\InvalidHostnameException; |
| 9 | +use Kiwicom\Loopbind\Exceptions\InvalidIPAddressException; |
| 10 | +use Kiwicom\Loopbind\Trait\ConfigurationPrinter; |
| 11 | +use Nette\Utils\Json; |
| 12 | +use Nette\Utils\JsonException; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Console\Question\Question; |
| 18 | +use function file_put_contents; |
| 19 | +use function filter_var; |
| 20 | +use function in_array; |
| 21 | +use const FILTER_FLAG_HOSTNAME; |
| 22 | +use const FILTER_FLAG_IPV4; |
| 23 | +use const FILTER_VALIDATE_DOMAIN; |
| 24 | +use const FILTER_VALIDATE_IP; |
| 25 | + |
| 26 | +final class InitCommand extends Command |
| 27 | +{ |
| 28 | + use ConfigurationPrinter; |
| 29 | + |
| 30 | + protected function configure(): void |
| 31 | + { |
| 32 | + $this->setName('init') |
| 33 | + ->setDescription('Init localhost configuration from config in current working directory.'); |
| 34 | + } |
| 35 | + |
| 36 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 37 | + { |
| 38 | + $configLoader = new ConfigLoader(); |
| 39 | + if ($configLoader->exists('.loopbind.json')) { |
| 40 | + return ExitCodes::CONFIG_ALREADY_EXISTS; |
| 41 | + } |
| 42 | + |
| 43 | + $questionHelper = new QuestionHelper(); |
| 44 | + |
| 45 | + $valid = false; |
| 46 | + do { |
| 47 | + $IPAddress = $questionHelper->ask($input, $output, new Question("<question>IPv4 address from local block:</question>\n")); |
| 48 | + if (!is_string($IPAddress) || filter_var($IPAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
| 49 | + $output->writeln('<error>Invalid IPv4 address. Please try again.</error>'); |
| 50 | + } else { |
| 51 | + $valid = true; |
| 52 | + } |
| 53 | + } while (!$valid); |
| 54 | + |
| 55 | + $askNext = true; |
| 56 | + $hostnames = []; |
| 57 | + do { |
| 58 | + $hostname = $questionHelper->ask($input, $output, new Question("<question>Hostname (leave empty to continue):</question>\n")); |
| 59 | + if ($hostname === null) { |
| 60 | + $askNext = false; |
| 61 | + continue; |
| 62 | + } |
| 63 | + if (!is_string($hostname)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (filter_var($hostname, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) { |
| 67 | + $output->writeln('<error>Invalid hostname. Please try again.</error>'); |
| 68 | + } elseif (in_array($hostname, $hostnames, true)) { |
| 69 | + $output->writeln('<error>This hostname was already provided. Please try again.</error>'); |
| 70 | + } else { |
| 71 | + $hostnames[] = $hostname; |
| 72 | + } |
| 73 | + } while (count($hostnames) === 0 || $askNext); |
| 74 | + |
| 75 | + try { |
| 76 | + /** @var string $IPAddress */ |
| 77 | + $config = new Config($IPAddress, $hostnames); |
| 78 | + $encoded = Json::encode($config, Json::PRETTY); |
| 79 | + } catch (InvalidIPAddressException | InvalidHostnameException | JsonException) { |
| 80 | + return ExitCodes::NEW_CONFIG_INVALID; |
| 81 | + } |
| 82 | + |
| 83 | + file_put_contents('.loopbind.json', $encoded); |
| 84 | + |
| 85 | + $output->writeln('<fg=green>New config file `.loopbind.json` was created.</>'); |
| 86 | + |
| 87 | + return ExitCodes::SUCCESS; |
| 88 | + } |
| 89 | +} |
0 commit comments