Defer loading cli option values (#14354)

* Defer loading option values
Otherwise it causes a cli command to be run every single application boot.

* style and lint

* Just return whatever we have it isn't callable
This commit is contained in:
Tony Murray
2022-09-16 04:58:22 -05:00
committed by GitHub
parent 7aeb238b7f
commit 67ad0726b0
3 changed files with 78 additions and 11 deletions

View File

@@ -42,9 +42,9 @@ class DeviceAdd extends LnmsCommand
$this->optionValues = [
'transport' => ['udp', 'udp6', 'tcp', 'tcp6'],
'port-association-mode' => PortAssociationMode::getModes(),
'auth-protocol' => \LibreNMS\SNMPCapabilities::supportedAuthAlgorithms(),
'privacy-protocol' => \LibreNMS\SNMPCapabilities::supportedCryptoAlgorithms(),
'port-association-mode' => [PortAssociationMode::class, 'getModes'],
'auth-protocol' => [\LibreNMS\SNMPCapabilities::class, 'supportedAuthAlgorithms'],
'privacy-protocol' => [\LibreNMS\SNMPCapabilities::class, 'supportedCryptoAlgorithms'],
];
$this->addArgument('device spec', InputArgument::REQUIRED);

View File

@@ -0,0 +1,52 @@
<?php
/**
* DynamicInputOption.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2022 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Console;
use Symfony\Component\Console\Input\InputOption;
class DynamicInputOption extends InputOption
{
/** @var callable|null */
private $valuesCallable;
public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null, ?callable $valuesCallable = null)
{
$this->valuesCallable = $valuesCallable;
parent::__construct($name, $shortcut, $mode, $description, $default);
}
public function getDescription()
{
$description = parent::getDescription();
if (is_callable($this->valuesCallable)) {
$description .= ' [' . implode(', ', call_user_func($this->valuesCallable)) . ']';
}
return $description;
}
}

View File

@@ -36,7 +36,7 @@ abstract class LnmsCommand extends Command
{
protected $developer = false;
/** @var string[][]|null */
/** @var string[][]|callable[][]|null */
protected $optionValues;
/**
@@ -101,11 +101,8 @@ abstract class LnmsCommand extends Command
$description = __('commands.' . $this->getName() . '.options.' . $name);
}
if (isset($this->optionValues[$name])) {
$description .= ' [' . implode(', ', $this->optionValues[$name]) . ']';
}
parent::addOption($name, $shortcut, $mode, $description, $default);
// inject our custom InputOption to allow callable option enums
$this->getDefinition()->addOption(new DynamicInputOption($name, $shortcut, $mode, $description, $default, $this->getValuesCallable($name)));
return $this;
}
@@ -118,8 +115,10 @@ abstract class LnmsCommand extends Command
{
// auto create option value rules if they don't exist
if (isset($this->optionValues)) {
foreach ($this->optionValues as $option => $values) {
if (empty($rules[$option])) {
foreach (array_keys($this->optionValues) as $option) {
$callable = $this->getValuesCallable($option);
if (empty($rules[$option]) && $callable) {
$values = call_user_func($callable);
$rules[$option] = Rule::in($values);
$messages[$option . '.in'] = trans('commands.lnms.validation-errors.optionValue', [
'option' => $option,
@@ -158,4 +157,20 @@ abstract class LnmsCommand extends Command
}
}
}
private function getValuesCallable(string $name): ?callable
{
if (empty($this->optionValues[$name])) {
return null;
}
$values = $this->optionValues[$name];
if (is_callable($values)) {
return $values;
}
return function () use ($values) {
return $values;
};
}
}