Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

189 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2019-02-15 09:00:07 -06:00
<?php
/**
* LnmsCommand.php
*
* Convenience class for common command code
*
* 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
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-15 09:00:07 -06:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2019-02-15 09:00:07 -06:00
* @copyright 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Console;
use Illuminate\Console\Command;
2022-03-12 16:14:32 -06:00
use Illuminate\Validation\Rule;
2019-02-15 09:00:07 -06:00
use Illuminate\Validation\ValidationException;
2021-11-17 19:23:55 -06:00
use LibreNMS\Util\Debug;
2019-02-15 09:00:07 -06:00
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Validator;
abstract class LnmsCommand extends Command
{
protected $developer = false;
2022-09-19 16:03:55 +02:00
/** @var string[][]|callable[]|null */
2022-03-12 16:14:32 -06:00
protected $optionValues;
2022-09-19 16:03:55 +02:00
/** @var string[][]|callable[]|null */
protected $optionDefaults;
2022-03-12 16:14:32 -06:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->setDescription(__('commands.' . $this->getName() . '.description'));
}
2021-11-17 19:23:55 -06:00
public function isHidden(): bool
2019-02-15 09:00:07 -06:00
{
$env = $this->getLaravel() ? $this->getLaravel()->environment() : getenv('APP_ENV');
2020-09-21 14:54:51 +02:00
return $this->hidden || ($this->developer && $env !== 'production');
2019-02-15 09:00:07 -06:00
}
/**
* Adds an argument. If $description is null, translate commands.command-name.arguments.name
* If you want the description to be empty, just set an empty string
*
2021-09-08 23:35:56 +02:00
* @param string $name The argument name
* @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
* @param string $description A description text
* @param string|string[]|null $default The default value (for InputArgument::OPTIONAL mode only)
2021-09-10 20:09:53 +02:00
* @return $this
2019-02-15 09:00:07 -06:00
*
* @throws InvalidArgumentException When argument mode is not valid
*/
2023-04-17 13:51:35 +02:00
public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null): static
2019-02-15 09:00:07 -06:00
{
// use a generated translation location by default
2023-04-17 13:51:35 +02:00
if (empty($description)) {
2019-02-15 09:00:07 -06:00
$description = __('commands.' . $this->getName() . '.arguments.' . $name);
}
parent::addArgument($name, $mode, $description, $default);
return $this;
}
/**
* Adds an option. If $description is null, translate commands.command-name.arguments.name
* If you want the description to be empty, just set an empty string
*
2021-09-08 23:35:56 +02:00
* @param string $name The option name
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
* @param string $description A description text
* @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
2021-09-10 20:09:53 +02:00
* @return $this
2019-02-15 09:00:07 -06:00
*
* @throws InvalidArgumentException If option mode is invalid or incompatible
*/
2023-04-17 13:51:35 +02:00
public function addOption(string $name, array|string|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null): static
2019-02-15 09:00:07 -06:00
{
// use a generated translation location by default
2023-04-17 13:51:35 +02:00
if (empty($description)) {
2019-02-15 09:00:07 -06:00
$description = __('commands.' . $this->getName() . '.options.' . $name);
}
2022-09-16 04:58:22 -05:00
// inject our custom InputOption to allow callable option enums
2022-09-19 16:03:55 +02:00
$this->getDefinition()->addOption(
new DynamicInputOption(
$name,
$shortcut,
$mode,
$description,
$default,
$this->getCallable('Defaults', $name),
$this->getCallable('Values', $name),
)
);
2019-02-15 09:00:07 -06:00
return $this;
}
/**
* Validate the input of this command. Uses Laravel input validation
* merging the arguments and options together to check.
*/
2021-08-27 22:48:21 -05:00
protected function validate(array $rules, array $messages = []): array
2019-02-15 09:00:07 -06:00
{
2022-03-12 16:14:32 -06:00
// auto create option value rules if they don't exist
if (isset($this->optionValues)) {
2022-09-16 04:58:22 -05:00
foreach (array_keys($this->optionValues) as $option) {
2022-09-19 16:03:55 +02:00
$callable = $this->getCallable('Values', $option);
2022-09-16 04:58:22 -05:00
if (empty($rules[$option]) && $callable) {
$values = call_user_func($callable);
2022-03-12 16:14:32 -06:00
$rules[$option] = Rule::in($values);
$messages[$option . '.in'] = trans('commands.lnms.validation-errors.optionValue', [
'option' => $option,
'values' => implode(', ', $values),
]);
}
}
}
$error_messages = trans('commands.' . $this->getName() . '.validation-errors');
2021-08-27 22:48:21 -05:00
$validator = Validator::make(
$this->arguments() + $this->options(),
$rules,
is_array($error_messages) ? array_merge($error_messages, $messages) : $messages
2021-08-27 22:48:21 -05:00
);
2019-02-15 09:00:07 -06:00
try {
$validator->validate();
2021-08-27 22:48:21 -05:00
return $validator->validated();
2019-02-15 09:00:07 -06:00
} catch (ValidationException $e) {
collect($validator->getMessageBag()->all())->each(function ($message) {
$this->error($message);
});
exit(1);
}
}
2021-11-17 19:23:55 -06:00
protected function configureOutputOptions(): void
{
\Log::setDefaultDriver($this->getOutput()->isQuiet() ? 'stack' : 'console');
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
Debug::set();
if ($verbosity >= 256) {
Debug::setVerbose();
}
}
}
2022-09-16 04:58:22 -05:00
2022-09-19 16:03:55 +02:00
private function getCallable(string $type, string $name): ?callable
2022-09-16 04:58:22 -05:00
{
2022-09-19 16:03:55 +02:00
if (empty($this->{'option' . $type}[$name])) {
2022-09-16 04:58:22 -05:00
return null;
}
2022-09-19 16:03:55 +02:00
$values = $this->{'option' . $type}[$name];
2022-09-16 04:58:22 -05:00
if (is_callable($values)) {
return $values;
}
return function () use ($values) {
return $values;
};
}
2019-02-15 09:00:07 -06:00
}