From 94033545f3da63b6c8c8265e0e8dacc3bde2e547 Mon Sep 17 00:00:00 2001 From: sikofitt Date: Thu, 29 Nov 2018 14:47:06 -0800 Subject: [PATCH] Small fixes, added console info to readme --- .gitignore | 6 +-- README.md | 23 ++++++++++- bin/generate-mac | 40 ++++++++++++++----- .../Command/GenerateMacCommand.php | 3 +- src/GenerateMac/Mac.php | 29 ++++++++++---- 5 files changed, 75 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 2152aa2..0114190 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .idea/ - .php_cs.cache - composer.lock -vendor +vendor/ +*~ + diff --git a/README.md b/README.md index e5a632e..b596fa3 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ composer require sikofitt/generate-mac ```php use Sikofitt\GenerateMac\Mac; -$mac = new Mac(); // default is ':' +$mac = new Mac(); // default separator is ':' // or $mac->setSeparator(Mac::SEPARATOR_COLON); $address = $mac->getAddress(); // ab:cd:ef:01:23:45 @@ -65,10 +65,29 @@ var_dump($addresses); // return an array [0 => '32:73:c0:b3:62:27'] ``` +#### Using the console component + +The console script requires [symfony/console](https://symfony.com/doc/current/components/console.html "Symfony Console Component") +```bash +user@localhost:~/generate-mac$ bin/generate-mac --count (int) --output (json|plain|string) --separator (none|colon|dash) +``` + * --count Generate {count} mac addresses + * --output Output in selected format + * string: (default) Outputs the mac address(es) with formatting + * plain: Outputs the mac address(es) with no formatting + * json: Output the mac address(es) in json format + * --separator Outputs with the selected operator + * colon: ':' (default) + * dash: '-' + * none: '' + +_See bin/generate-mac --help_ + + #### Test ```bash -user@localhost:~/generate-mac$ vendor/phpunit +user@localhost:~/generate-mac$ vendor/bin/phpunit ``` #### License diff --git a/bin/generate-mac b/bin/generate-mac index 9e71510..690f0f6 100755 --- a/bin/generate-mac +++ b/bin/generate-mac @@ -25,6 +25,8 @@ use Symfony\Component\Console\{ use Sikofitt\GenerateMac\Command\GenerateMacCommand; +define('GENERATE_MAC_VERSION', 'v0.0.1'); + $autoloadFiles = array( __DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php', @@ -39,26 +41,42 @@ foreach ($autoloadFiles as $autoloadFile) { } if (!defined('GENERATE_MAC')) { - die( - 'You need to set up the project dependencies using the following commands:' . PHP_EOL . - 'curl -s http://getcomposer.org/installer | php' . PHP_EOL . - 'php composer.phar install' . PHP_EOL - ); + + $msg = + PHP_EOL . 'You need to use composer to install the dependencies:' . PHP_EOL . + 'curl -LSs https://getcomposer.org/installer|php' . PHP_EOL . + 'php composer.phar install, or ' . PHP_EOL . + 'php composer.phar install --no-dev && php composer.phar require symfony/console' . PHP_EOL . PHP_EOL + ; + + print $msg; + + throw new \RuntimeException('Missing dependencies', -1); } if(false === class_exists(Command::class)) { - throw new \RuntimeException('Install symfony/console to use the the generate-mac command.'); + $msg = + PHP_EOL . 'You need to use composer to install the dependencies for the console:' . PHP_EOL . + 'php composer.phar require symfony/console' . PHP_EOL . PHP_EOL + ; + print $msg; + + throw new \RuntimeException('Missing package symfony/console.', -1); } -$application = new Application(); -$application->add(new GenerateMacCommand()); -$application->setDefaultCommand('generate-mac', true); -$application->setAutoExit(true); +$application = new Application('Generate mac', GENERATE_MAC_VERSION); + +$application + ->add(new GenerateMacCommand()); + +$application + ->setDefaultCommand('generate-mac', true) + ->setAutoExit(true); try { $application->run(); } catch (Exception $e) { printf('Couldn\'t run the application.%s', PHP_EOL); printf('Error was: %s at %s:%s%s', $e->getMessage(), $e->getFile(), $e->getLine(), PHP_EOL); -} \ No newline at end of file +} diff --git a/src/GenerateMac/Command/GenerateMacCommand.php b/src/GenerateMac/Command/GenerateMacCommand.php index 0d224ab..f24ad4d 100644 --- a/src/GenerateMac/Command/GenerateMacCommand.php +++ b/src/GenerateMac/Command/GenerateMacCommand.php @@ -95,8 +95,7 @@ class GenerateMacCommand extends Command $io->comment(implode(PHP_EOL, $macAddresses)); break; case 'json': - $result = $count > 1 ? $macAddresses : [$macAddresses]; - $io->writeln(\json_encode($result, JSON_PRETTY_PRINT)); + $io->writeln(\json_encode($macAddresses, JSON_PRETTY_PRINT)); break; case 'plain': $io->writeln($macAddresses, SymfonyStyle::OUTPUT_PLAIN | SymfonyStyle::VERBOSITY_NORMAL); diff --git a/src/GenerateMac/Mac.php b/src/GenerateMac/Mac.php index bb8f47c..5d7e5ef 100644 --- a/src/GenerateMac/Mac.php +++ b/src/GenerateMac/Mac.php @@ -55,10 +55,10 @@ class Mac * Reserved mac prefixes for private devices. */ private const AVAILABLE_PREFIXES = [ - 'x2xxxx', - 'x6xxxx', - 'xaxxxx', - 'xexxxx', + 'x2xxxx', + 'x6xxxx', + 'xaxxxx', + 'xexxxx', ]; /** @@ -80,7 +80,7 @@ class Mac /** * Mac constructor. * - * @param int $separator The mac address separator, one of ':', '-', or '' + * @param int $separator The mac address separator, one of self::SEPARATOR_* * @param bool $unique Whether or not we care if we get a non unique prefix. */ public function __construct(int $separator = self::SEPARATOR_COLON, bool $unique = true) @@ -98,8 +98,11 @@ class Mac $template = $this->shuffle(); $prefix = $this->generateString($template); + /** + * @internal + */ if ($this->isTest) { - $prefix = '02bb01'; + $prefix = '02bb01'; // QEMU virtual NIC } if ($this->getUnique()) { @@ -159,8 +162,18 @@ class Mac */ public function setSeparator(int $separator): Mac { - if (!\in_array($separator, [self::SEPARATOR_COLON, self::SEPARATOR_DASH, self::SEPARATOR_NONE], true)) { - throw new \InvalidArgumentException('Separator is invalid. Acceptable values: ":", "-", or ""'); + if (!\in_array( + $separator, + [ + self::SEPARATOR_COLON, + self::SEPARATOR_DASH, + self::SEPARATOR_NONE, + ], + true + )) { + throw new \InvalidArgumentException( + 'Separator is invalid. Acceptable values: One of Mac::SEPARATOR_*' + ); } $this->separator = $separator;