refactored argument handling

This commit is contained in:
tracer 2022-09-17 18:56:20 +02:00
parent 61b24edfdf
commit b48758bf82
1 changed files with 63 additions and 47 deletions

View File

@ -12,6 +12,7 @@ define(constant_name: 'COLOR_WHITE', value: "\033[37m");
define(constant_name: 'COLOR_DEFAULT', value: "\033[39m");
use App\Entity\Domain;
use App\Entity\DynDNS;
use App\Entity\Nameserver;
use App\Entity\Panel;
use App\Repository\ApikeyRepository;
@ -20,6 +21,7 @@ use App\Repository\DynDNSRepository;
use App\Repository\NameserverRepository;
use App\Repository\PanelRepository;
use Arubacao\TldChecker\Validator;
use Exception;
use LucidFrame\Console\ConsoleTable;
if (php_sapi_name() !== 'cli') {
@ -32,11 +34,10 @@ if (php_sapi_name() !== 'cli') {
*/
class CLIController
{
private int $argumentsCount;
private array $arguments;
/**
* @throws \Exception
* @throws Exception
*/
public function __construct(
private readonly ApiController $apiController,
@ -77,24 +78,25 @@ class CLIController
// TODO encrypt the password in the config file, key in config
function runCommand(int $argumentsCount, array $arguments): void
function runCommand(array $arguments): void
{
$this->argumentsCount = $argumentsCount;
$this->arguments = $arguments;
$this->logger->debug(message: "runCommand()");
if ($this->argumentsCount < 1) {
if (count($arguments) < 1) {
$this->showUsage();
exit(0);
}
if (str_contains(haystack: $this->arguments[0], needle: ':')) {
[$command, $subcommand] = explode(separator: ':', string: $this->arguments[0]);
$this->logger->debug(message: "runCommand()");
if (str_contains(haystack: $arguments[0], needle: ':')) {
[$command, $subcommand] = explode(separator: ':', string: $arguments[0]);
} else {
$command = $this->arguments[0];
$command = $arguments[0];
$subcommand = '';
}
$this->arguments = $this->parseArguments(arguments: $arguments);
match ($command) {
'check' => $this->handleChecks(subcommand: $subcommand),
'panels' => $this->handlePanels(subcommand: $subcommand),
@ -215,7 +217,7 @@ class CLIController
function handleCheckSetup(): void
{
if ($this->argumentsCount < 2) {
if (count($this->arguments) < 2) {
echo 'You need to supply a username.' . PHP_EOL;
exit(1);
}
@ -482,8 +484,7 @@ class CLIController
break;
case 404:
echo COLOR_RED . ' ' . $result['header'] . COLOR_DEFAULT;
$arguments = $this->parseArguments();
if (!empty($arguments['fix']) && $arguments['fix'] == 'yes') {
if (!empty($this->arguments['fix']) && $this->arguments['fix'] == 'yes') {
echo ' trying to fix …';
$body = [
'name' => $domainName,
@ -525,22 +526,26 @@ class CLIController
}
/**
* @param array $arguments
* @return array
*/
public function parseArguments(): array
public function parseArguments(array $arguments): array
{
$this->logger->debug(message: "parseArguments()");
$arguments = [];
foreach ($this->arguments as $argument) {
$parsedArguments = [];
$parseCount = 0;
foreach ($arguments as $argument) {
if (str_contains(haystack: $argument, needle: '=')) {
[$key, $value] = explode(separator: '=', string: $argument);
$arguments[strtolower(string: $key)] = $value;
$parsedArguments[strtolower(string: $key)] = $value;
$parsedArguments[$parseCount++] = $value;
} else {
$arguments[strtolower(string: $argument)] = $argument;
$parsedArguments[strtolower(string: $argument)] = $argument;
$parsedArguments[$parseCount++] = $argument;
}
}
return $arguments;
return $parsedArguments;
}
/**
@ -583,16 +588,14 @@ class CLIController
exit(1);
}
$arguments = $this->parseArguments();
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
$a = $this->arguments['a'] ?? '';
$aaaa = $this->arguments['aaaa'] ?? '';
if (empty($a) && empty($aaaa)) {
echo 'At least one IP address is required.' . PHP_EOL;
exit(0);
}
$apikey = $arguments['apikey'] ?? '';
$self = intval(value: $arguments['self'] ?? 0);
$apikey = $this->arguments['apikey'] ?? '';
$self = intval(value: $this->arguments['self'] ?? 0);
if ($this->panelRepository->findByName(name: $name)) {
@ -649,15 +652,14 @@ class CLIController
{
$this->logger->debug(message: "handlePanelsUpdate()");
$arguments = $this->parseArguments();
$id = intval(value: $this->arguments[1] ?? 0);
$name = $arguments['name'] ?? '';
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
$apikey = $arguments['apikey'] ?? '';
$self = intval(value: $arguments['self'] ?? 0);
$name = $this->arguments['name'] ?? '';
$a = $this->arguments['a'] ?? '';
$aaaa = $this->arguments['aaaa'] ?? '';
$apikey = $this->arguments['apikey'] ?? '';
$self = intval(value: $this->arguments['self'] ?? 0);
// a workaround for 0 being equal to false …
if ($self == 0) {
$self = -1;
}
@ -865,8 +867,7 @@ class CLIController
*/
function handleApikeysCreate(): void
{
$arguments = $this->parseArguments();
$name = $arguments['name'] ?? '';
$name = $this->arguments['name'] ?? '';
$result = $this->apikeyRepository->create(name: $name);
echo 'API key ' . COLOR_YELLOW . $result['row'] . COLOR_DEFAULT . ' has been generated. Store it in a save place, it cannot be recovered.' . PHP_EOL;
@ -904,10 +905,10 @@ class CLIController
*/
function handleApikeysUpdate(): void
{
$arguments = $this->parseArguments();
// TODO check for use of id instead of number, mind for all occurences
$id = $this->arguments[1] ?? 0;
$name = $arguments['name'] ?? '';
$name = $this->arguments['name'] ?? '';
if ($id == 0) {
echo 'An ID is required' . PHP_EOL;
@ -1021,7 +1022,7 @@ class CLIController
*/
function handleDynDnsCreate(): void
{
$name = $this->arguments[1] ?? "";
$name = $this->arguments[1] ?? '';
if (empty($name)) {
echo 'You need to supply the FQDN (hostname).' . PHP_EOL;
exit(1);
@ -1035,9 +1036,11 @@ class CLIController
exit(1);
}
// TODO remove
echo 'fqdn: ' . $name;
$password = $this->arguments[2] ?? '';
var_dump($this->arguments);
die();
$domainParts = explode(separator: '.', string: $name);
$reversedParts = array_reverse(array: $domainParts);
$testDomain = '';
@ -1060,12 +1063,25 @@ class CLIController
}
if ($this->configController->getConfig(configKey: 'verbose')) {
echo "Found domain: " . $foundDomain . PHP_EOL;
echo "Found domain: " . COLOR_YELLOW . $foundDomain . COLOR_DEFAULT . PHP_EOL;
}
// get host
// inset in db
if ($this->dynDNSRepository->findByName(name: $name)) {
if ($this->configController->getConfig(configKey: 'verbose')) {
echo "DynDNS host " . COLOR_YELLOW . $name . COLOR_DEFAULT . "already exists.". PHP_EOL;
exit(0);
}
} else {
if ($this->configController->getConfig(configKey: 'verbose')) {
echo "DynDNS host " . COLOR_YELLOW . $name . COLOR_DEFAULT . "will be created.". PHP_EOL;
// insert in db
$dyndnsHost = new DynDNS();
$dyndnsHost->setName($name);
}
}
// check on NS
// => add if missing