Make InitialApiKeyCommand invokable

This commit is contained in:
Alejandro Celaya 2025-11-01 11:41:50 +01:00
parent dfef735c89
commit 2d83b8d046
2 changed files with 15 additions and 19 deletions

View File

@ -5,11 +5,15 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Api; namespace Shlinkio\Shlink\CLI\Command\Api;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: InitialApiKeyCommand::NAME,
description: 'Tries to create initial API key'
)]
class InitialApiKeyCommand extends Command class InitialApiKeyCommand extends Command
{ {
public const string NAME = 'api-key:initial'; public const string NAME = 'api-key:initial';
@ -19,22 +23,14 @@ class InitialApiKeyCommand extends Command
parent::__construct(); parent::__construct();
} }
protected function configure(): void public function __invoke(
{ SymfonyStyle $io,
$this #[Argument('The initial API to create')] string $apiKey
->setHidden() ): int {
->setName(self::NAME) $result = $this->apiKeyService->createInitial($apiKey);
->setDescription('Tries to create initial API key')
->addArgument('apiKey', InputArgument::REQUIRED, 'The initial API to create');
}
protected function execute(InputInterface $input, OutputInterface $output): int if ($result === null && $io->isVerbose()) {
{ $io->writeln('<comment>Other API keys already exist. Initial API key creation skipped.</comment>');
$key = $input->getArgument('apiKey');
$result = $this->apiKeyService->createInitial($key);
if ($result === null && $output->isVerbose()) {
$output->writeln('<comment>Other API keys already exist. Initial API key creation skipped.</comment>');
} }
return Command::SUCCESS; return Command::SUCCESS;

View File

@ -35,7 +35,7 @@ class InitialApiKeyCommandTest extends TestCase
$this->apiKeyService->expects($this->once())->method('createInitial')->with('the_key')->willReturn($result); $this->apiKeyService->expects($this->once())->method('createInitial')->with('the_key')->willReturn($result);
$this->commandTester->execute( $this->commandTester->execute(
['apiKey' => 'the_key'], ['api-key' => 'the_key'],
['verbosity' => $verbose ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL], ['verbosity' => $verbose ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL],
); );
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();