Fixed bug preventing emby/jellyfin from adding new backends. Closes #360

This commit is contained in:
Abdulmhsen B. A. A
2023-11-11 13:31:48 +03:00
parent 456f7d9c10
commit 3961fbd2e7
12 changed files with 99 additions and 14 deletions

View File

@@ -6,12 +6,15 @@ namespace App\Commands\Config;
use App\Command;
use App\Libs\Routable;
use RuntimeException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
#[Routable(command: self::ROUTE)]
final class AddCommand extends Command
@@ -23,7 +26,7 @@ final class AddCommand extends Command
$this->setName(self::ROUTE)
->setDescription('Add new backend.')
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use Alternative config file.')
->addArgument('backend', InputArgument::REQUIRED, 'Backend name')
->addArgument('backend', InputArgument::OPTIONAL, 'Backend name', null)
->setHelp(
r(
<<<HELP
@@ -50,6 +53,31 @@ final class AddCommand extends Command
*/
protected function runCommand(InputInterface $input, OutputInterface $output): int
{
if (function_exists('stream_isatty') && defined('STDERR')) {
$tty = stream_isatty(STDERR);
} else {
$tty = true;
}
if (false === $tty || $input->getOption('no-interaction')) {
$output->writeln(
r(
<<<ERROR
<error>ERROR:</error> This command require <notice>interaction</notice>. For example:
{cmd} <cmd>{route}</cmd>
ERROR,
[
'cmd' => trim(commandContext()),
'route' => self::ROUTE,
]
)
);
return self::FAILURE;
}
$opts = [
'--add' => true,
];
@@ -61,7 +89,49 @@ final class AddCommand extends Command
$opts['--' . $option] = $val;
}
$opts['backend'] = strtolower($input->getArgument('backend'));
$backend = $input->getArgument('backend');
if (null !== $backend) {
$opts['backend'] = strtolower($backend);
} else {
// -- $backend.token
$opts['backend'] = (function () use (&$opts, $input, $output) {
$chosen = ag($opts, 'backend');
$question = new Question(
<<<HELP
<question>What should we be calling this <value>backend</value>?</question>
------------------
Backend name is used to identify the backend. The backend name must only contains
<value>lower case a-z</value>, <value>0-9</value> and <value>_</value>.
------------------
<notice>Choose good name to identify your backend. For example, <value>home_plex</value>.</notice>
HELP. PHP_EOL . '> ',
$chosen
);
$question->setValidator(function ($answer) {
if (empty($answer)) {
throw new RuntimeException('Backend Name cannot be empty.');
}
if (!isValidName($answer) || strtolower($answer) !== $answer) {
throw new RuntimeException(
r(
'<error>ERROR:</error> Invalid [<value>{name}</value>] name was given. Only [<value>a-z, 0-9, _</value>] are allowed.',
[
'name' => $answer
],
)
);
}
return $answer;
});
return (new QuestionHelper())->ask($input, $output, $question);
})();
$output->writeln('');
$opts['backend'] = strtolower($opts['backend']);
}
return $this->getApplication()?->find(ManageCommand::ROUTE)->run(new ArrayInput($opts), $output) ?? 1;
}