Compare commits
13 Commits
Author | SHA1 | Date |
---|---|---|
R. Eric Wheeler | f66809a46e | |
R. Eric Wheeler | 7d5a7d3140 | |
R. Eric Wheeler | e3362fed11 | |
R. Eric Wheeler | 6ef9053301 | |
Eric | 38beb59fa3 | |
Eric | d5af5a14ba | |
Eric | 461cbe3533 | |
Eric | 0b84512379 | |
Eric | a66771da98 | |
Eric | e2c826fd0d | |
Eric | 99be5563cb | |
Eric | 42b2840aab | |
R. Eric Wheeler | 0b4edb36cb |
|
@ -0,0 +1,38 @@
|
||||||
|
name: PHP Composer
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master, 1.x ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master, 1.x ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Validate composer.json and composer.lock
|
||||||
|
run: composer validate
|
||||||
|
|
||||||
|
- name: Cache Composer packages
|
||||||
|
id: composer-cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: vendor
|
||||||
|
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-php-
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
if: steps.composer-cache.outputs.cache-hit != 'true'
|
||||||
|
run: composer install --prefer-dist --no-progress --no-suggest
|
||||||
|
|
||||||
|
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
|
||||||
|
# Docs: https://getcomposer.org/doc/articles/scripts.md
|
||||||
|
- name: Tests
|
||||||
|
run: vendor/bin/phpunit
|
||||||
|
# - name: Run test suite
|
||||||
|
# run: composer run-script test
|
|
@ -0,0 +1,48 @@
|
||||||
|
name: Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ 0.x ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ 0.x ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
php: ['7.3', '7.4', '8.0']
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Validate composer.json and composer.lock
|
||||||
|
run: composer validate
|
||||||
|
|
||||||
|
- name: Cache Composer packages
|
||||||
|
id: composer-cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: vendor
|
||||||
|
key: ${{ matrix-php }}-composer-${{ hashFiles('**/composer.lock') }}
|
||||||
|
restore-keys: ${{ matrix.php }}-composer-
|
||||||
|
|
||||||
|
- name: Setup PHP
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
coverage: "none"
|
||||||
|
extensions: "json,sodium"
|
||||||
|
php-version: "${{ matrix.php }}"
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
if: steps.composer-cache.outputs.cache-hit != 'true'
|
||||||
|
run: composer install --prefer-dist --no-progress --no-suggest
|
||||||
|
|
||||||
|
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
|
||||||
|
# Docs: https://getcomposer.org/doc/articles/scripts.md
|
||||||
|
- name: Run tests
|
||||||
|
run: vendor/bin/phpunit -v
|
||||||
|
|
||||||
|
# - name: Run test suite
|
||||||
|
# run: composer run-script test
|
|
@ -12,9 +12,7 @@ return PhpCsFixer\Config::create()
|
||||||
[
|
[
|
||||||
'@PSR2' => true,
|
'@PSR2' => true,
|
||||||
'@PHP70Migration' => true,
|
'@PHP70Migration' => true,
|
||||||
'@PHP70Migration:risky' => true,
|
|
||||||
'@PHP71Migration' => true,
|
'@PHP71Migration' => true,
|
||||||
'@PHP71Migration:risky' => true,
|
|
||||||
'header_comment' => ['header' => $header],
|
'header_comment' => ['header' => $header],
|
||||||
'ordered_class_elements' => true,
|
'ordered_class_elements' => true,
|
||||||
'ordered_imports' => true,
|
'ordered_imports' => true,
|
||||||
|
|
|
@ -2,7 +2,13 @@
|
||||||
|
|
||||||
### Small library to generate unique private mac addresses
|
### Small library to generate unique private mac addresses
|
||||||
|
|
||||||
|
[![pipeline status](https://repos.bgemi.net/sikofitt/generate-mac/badges/1.x/pipeline.svg)](https://repos.bgemi.net/sikofitt/generate-mac/-/commits/1.x)
|
||||||
|
[![coverage report](https://repos.bgemi.net/sikofitt/generate-mac/badges/1.x/coverage.svg)](https://repos.bgemi.net/sikofitt/generate-mac/-/commits/1.x)
|
||||||
|
|
||||||
#### Install
|
#### Install
|
||||||
|
|
||||||
|
Requires PHP v8.0, For versions compatible with PHP >= 7.3 use the 0.x branch.
|
||||||
|
|
||||||
[composer](https://getcomposer.org)
|
[composer](https://getcomposer.org)
|
||||||
```bash
|
```bash
|
||||||
composer require sikofitt/generate-mac
|
composer require sikofitt/generate-mac
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"description": "Generates dummy mac addresses",
|
"description": "Generates dummy mac addresses",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.3",
|
"php": "^8.2",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-sodium": "*"
|
"ext-sodium": "*"
|
||||||
},
|
},
|
||||||
|
@ -13,10 +13,11 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"friendsofphp/php-cs-fixer": "^2.18",
|
"composer/xdebug-handler": "^3.0",
|
||||||
"phpunit/phpunit": "^9.5",
|
"friendsofphp/php-cs-fixer": "^3.17",
|
||||||
"squizlabs/php_codesniffer": "^3.5",
|
"phpunit/phpunit": "^9.6",
|
||||||
"symfony/console": "^4.4|^5.0"
|
"squizlabs/php_codesniffer": "^3.7",
|
||||||
|
"symfony/console": "^6.3"
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
|
@ -34,16 +34,20 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
class GenerateMacCommand extends Command
|
class GenerateMacCommand extends Command
|
||||||
{
|
{
|
||||||
|
private const SEPARATOR_NAMES = [
|
||||||
|
'colon',
|
||||||
|
'dash',
|
||||||
|
'none',
|
||||||
|
];
|
||||||
|
|
||||||
public function configure(): void
|
public function configure(): void
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setName('generate-mac')
|
->setName('generate-mac')
|
||||||
->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Generate {count} mac addresses.')
|
->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Generate {count} mac addresses.')
|
||||||
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output in this format instead of a string. [json,plain,string]')
|
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output in this format instead of a string. [json, plain, string]')
|
||||||
->addOption('separator', 's', InputOption::VALUE_REQUIRED, 'The separator to use for mac addresses.')
|
->addOption('separator', 's', InputOption::VALUE_REQUIRED, 'The separator to use for mac addresses. [colon, dash, none]')
|
||||||
;
|
;
|
||||||
|
|
||||||
parent::configure();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,9 +65,9 @@ class GenerateMacCommand extends Command
|
||||||
throw new RuntimeException('$count should be a positive number greater than zero.');
|
throw new RuntimeException('$count should be a positive number greater than zero.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$separator = strtolower($input->getOption('separator') ?? 'colon');
|
$separatorName = strtolower($input->getOption('separator') ?? 'colon');
|
||||||
|
|
||||||
if (!\in_array($separator, ['colon','none','dash'], true)) {
|
if (!\in_array($separatorName, self::SEPARATOR_NAMES, true)) {
|
||||||
throw new InvalidArgumentException('Separator must be one of "colon", "none", or "dash"');
|
throw new InvalidArgumentException('Separator must be one of "colon", "none", or "dash"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,23 +75,21 @@ class GenerateMacCommand extends Command
|
||||||
|
|
||||||
$io = new SymfonyStyle($input, $output);
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
|
||||||
switch ($separator) {
|
$separator = match($separatorName) {
|
||||||
case 'colon':
|
'colon' => Mac::SEPARATOR_COLON,
|
||||||
default:
|
'dash' => Mac::SEPARATOR_DASH,
|
||||||
$separator = Mac::SEPARATOR_COLON;
|
'none' => Mac::SEPARATOR_NONE,
|
||||||
break;
|
default => Mac::SEPARATOR_COLON,
|
||||||
case 'none':
|
};
|
||||||
$separator = Mac::SEPARATOR_NONE;
|
|
||||||
break;
|
|
||||||
case 'dash':
|
|
||||||
$separator = Mac::SEPARATOR_DASH;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$mac = new Mac($separator);
|
$mac = new Mac($separator);
|
||||||
|
|
||||||
$macAddresses = $mac->getMacAddresses($count);
|
$macAddresses = $mac->getMacAddresses($count);
|
||||||
|
|
||||||
|
if(empty($macAddresses)) {
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
switch ($outputFormat) {
|
switch ($outputFormat) {
|
||||||
case 'string':
|
case 'string':
|
||||||
default:
|
default:
|
||||||
|
@ -102,6 +104,6 @@ class GenerateMacCommand extends Command
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ class Mac
|
||||||
public const SEPARATOR_COLON = 0;
|
public const SEPARATOR_COLON = 0;
|
||||||
public const SEPARATOR_DASH = 1;
|
public const SEPARATOR_DASH = 1;
|
||||||
public const SEPARATOR_NONE = 2;
|
public const SEPARATOR_NONE = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private mac address prefixes that are used
|
* Private mac address prefixes that are used
|
||||||
* internally or with virtual machines and containers.
|
* internally or with virtual machines and containers.
|
||||||
|
@ -68,22 +69,10 @@ class Mac
|
||||||
protected $isTest = false;
|
protected $isTest = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int The mac address separator, can be self::SEPARATOR_*
|
|
||||||
*/
|
|
||||||
private $separator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool If we care if we get an already used prefix or not.
|
|
||||||
*/
|
|
||||||
private $unique;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mac constructor.
|
|
||||||
*
|
|
||||||
* @param int $separator The mac address separator, one of self::SEPARATOR_*
|
* @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(private int $separator = self::SEPARATOR_COLON, private bool $unique = true)
|
||||||
{
|
{
|
||||||
$this->setUnique($unique);
|
$this->setUnique($unique);
|
||||||
$this->setSeparator($separator);
|
$this->setSeparator($separator);
|
||||||
|
@ -196,16 +185,12 @@ class Mac
|
||||||
*/
|
*/
|
||||||
public function getSeparatorAsString(): string
|
public function getSeparatorAsString(): string
|
||||||
{
|
{
|
||||||
|
return match ( $this->getSeparator() ) {
|
||||||
switch ($this->getSeparator()) {
|
self::SEPARATOR_COLON => ':',
|
||||||
default:
|
self::SEPARATOR_DASH => '-',
|
||||||
case self::SEPARATOR_COLON:
|
self::SEPARATOR_NONE => '',
|
||||||
return ':';
|
default => ':',
|
||||||
case self::SEPARATOR_DASH:
|
};
|
||||||
return '-';
|
|
||||||
case self::SEPARATOR_NONE:
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -230,11 +215,11 @@ class Mac
|
||||||
*/
|
*/
|
||||||
private function generateString(string $template): string
|
private function generateString(string $template): string
|
||||||
{
|
{
|
||||||
$bytes = sodium_bin2hex(\random_bytes(32));
|
$bytes = \sodium_bin2hex(\random_bytes(32));
|
||||||
|
|
||||||
while (false !== $pos = \strpos($template, 'x')) {
|
while (false !== $pos = \strpos($template, 'x')) {
|
||||||
$replacement = $bytes[\random_int(0, \strlen($bytes) -1)];
|
$replacement = $bytes[\random_int(0, \strlen($bytes) -1)];
|
||||||
$template = substr_replace($template, $replacement, $pos, 1);
|
$template = \substr_replace($template, $replacement, $pos, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $template;
|
return $template;
|
||||||
|
@ -259,6 +244,6 @@ class Mac
|
||||||
*/
|
*/
|
||||||
private function insertSeparator(string $macAddress): string
|
private function insertSeparator(string $macAddress): string
|
||||||
{
|
{
|
||||||
return implode($this->getSeparatorAsString(), str_split($macAddress, 2));
|
return \implode($this->getSeparatorAsString(), \str_split($macAddress, 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue