| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cocotte\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cocotte\Console\AbstractCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cocotte\Console\Style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cocotte\DigitalOcean\ApiToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cocotte\DigitalOcean\ApiTokenOptionProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Cocotte\DigitalOcean\HostnameCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Cocotte\DigitalOcean\NetworkingConfigurator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Cocotte\Environment\LazyEnvironment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Darsyn\IP\IP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class NetworkingCommand extends AbstractCommand implements LazyEnvironment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var NetworkingConfigurator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $networkingConfigurator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var EventDispatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var Style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         NetworkingConfigurator $networkingConfigurator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         EventDispatcherInterface $eventDispatcher, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         Style $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->networkingConfigurator = $networkingConfigurator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->style = $style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function lazyEnvironmentValues(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ApiToken::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function optionProviders(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ApiTokenOptionProvider::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function isHidden() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return !getenv('SHOW_HIDDEN_COMMANDS'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     protected function eventDispatcher(): EventDispatcherInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         return $this->eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected function doConfigure(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->setName('networking') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ->setDescription('Configure networking of Digital Ocean') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->addArgument('hostnames', InputArgument::REQUIRED, 'Comma-separated list of hostnames') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ->addOption('ip', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 InputOption::VALUE_REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 'IP to use for hostnames (required without the --remove option)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ->addOption('remove', null, InputOption::VALUE_NONE, 'Remove networking for hostnames'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function doExecute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $hostnames = HostnameCollection::fromString($input->getArgument('hostnames')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if ($input->getOption('remove')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->networkingConfigurator->remove($hostnames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->style->success("Networking successfully removed."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $ip = new IP($input->getOption('ip')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $this->networkingConfigurator->configure($hostnames, $ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $this->style->success("Networking successfully configured."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  | } |