Small fixes, added console info to readme

This commit is contained in:
R. Eric Wheeler 2018-11-29 14:47:06 -08:00
parent 44efbeaf31
commit 94033545f3
5 changed files with 75 additions and 26 deletions

6
.gitignore vendored
View File

@ -1,7 +1,7 @@
.idea/ .idea/
.php_cs.cache .php_cs.cache
composer.lock composer.lock
vendor vendor/
*~

View File

@ -12,7 +12,7 @@ composer require sikofitt/generate-mac
```php ```php
use Sikofitt\GenerateMac\Mac; use Sikofitt\GenerateMac\Mac;
$mac = new Mac(); // default is ':' $mac = new Mac(); // default separator is ':'
// or // or
$mac->setSeparator(Mac::SEPARATOR_COLON); $mac->setSeparator(Mac::SEPARATOR_COLON);
$address = $mac->getAddress(); // ab:cd:ef:01:23:45 $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'] // 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 #### Test
```bash ```bash
user@localhost:~/generate-mac$ vendor/phpunit user@localhost:~/generate-mac$ vendor/bin/phpunit
``` ```
#### License #### License

View File

@ -25,6 +25,8 @@ use Symfony\Component\Console\{
use Sikofitt\GenerateMac\Command\GenerateMacCommand; use Sikofitt\GenerateMac\Command\GenerateMacCommand;
define('GENERATE_MAC_VERSION', 'v0.0.1');
$autoloadFiles = array( $autoloadFiles = array(
__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php', __DIR__ . '/../../../autoload.php',
@ -39,22 +41,38 @@ foreach ($autoloadFiles as $autoloadFile) {
} }
if (!defined('GENERATE_MAC')) { if (!defined('GENERATE_MAC')) {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL . $msg =
'curl -s http://getcomposer.org/installer | php' . PHP_EOL . PHP_EOL . 'You need to use composer to install the dependencies:' . PHP_EOL .
'php composer.phar install' . 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)) 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 = new Application('Generate mac', GENERATE_MAC_VERSION);
$application->add(new GenerateMacCommand());
$application->setDefaultCommand('generate-mac', true); $application
$application->setAutoExit(true); ->add(new GenerateMacCommand());
$application
->setDefaultCommand('generate-mac', true)
->setAutoExit(true);
try { try {
$application->run(); $application->run();

View File

@ -95,8 +95,7 @@ class GenerateMacCommand extends Command
$io->comment(implode(PHP_EOL, $macAddresses)); $io->comment(implode(PHP_EOL, $macAddresses));
break; break;
case 'json': case 'json':
$result = $count > 1 ? $macAddresses : [$macAddresses]; $io->writeln(\json_encode($macAddresses, JSON_PRETTY_PRINT));
$io->writeln(\json_encode($result, JSON_PRETTY_PRINT));
break; break;
case 'plain': case 'plain':
$io->writeln($macAddresses, SymfonyStyle::OUTPUT_PLAIN | SymfonyStyle::VERBOSITY_NORMAL); $io->writeln($macAddresses, SymfonyStyle::OUTPUT_PLAIN | SymfonyStyle::VERBOSITY_NORMAL);

View File

@ -80,7 +80,7 @@ class Mac
/** /**
* Mac constructor. * 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. * @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) public function __construct(int $separator = self::SEPARATOR_COLON, bool $unique = true)
@ -98,8 +98,11 @@ class Mac
$template = $this->shuffle(); $template = $this->shuffle();
$prefix = $this->generateString($template); $prefix = $this->generateString($template);
/**
* @internal
*/
if ($this->isTest) { if ($this->isTest) {
$prefix = '02bb01'; $prefix = '02bb01'; // QEMU virtual NIC
} }
if ($this->getUnique()) { if ($this->getUnique()) {
@ -159,8 +162,18 @@ class Mac
*/ */
public function setSeparator(int $separator): Mac public function setSeparator(int $separator): Mac
{ {
if (!\in_array($separator, [self::SEPARATOR_COLON, self::SEPARATOR_DASH, self::SEPARATOR_NONE], true)) { if (!\in_array(
throw new \InvalidArgumentException('Separator is invalid. Acceptable values: ":", "-", or ""'); $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; $this->separator = $separator;