Small fixes, added console info to readme
This commit is contained in:
parent
44efbeaf31
commit
94033545f3
|
@ -1,7 +1,7 @@
|
|||
.idea/
|
||||
|
||||
.php_cs.cache
|
||||
|
||||
composer.lock
|
||||
vendor
|
||||
vendor/
|
||||
*~
|
||||
|
||||
|
||||
|
|
23
README.md
23
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
|
||||
|
|
|
@ -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,22 +41,38 @@ 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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue