From 10e4c4cce8b863234025b91806c7bfdaa712888c Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Tue, 7 Jan 2025 15:09:31 +0000 Subject: [PATCH] Add Valkey support (4.x) --- src/Application.php | 1 + src/Command/Service/RedisCliCommand.php | 69 ++------------------- src/Command/Service/ValkeyCliCommand.php | 76 ++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 src/Command/Service/ValkeyCliCommand.php diff --git a/src/Application.php b/src/Application.php index 5da57a1172..06e7510309 100644 --- a/src/Application.php +++ b/src/Application.php @@ -233,6 +233,7 @@ protected function getCommands() $commands[] = new Command\Service\MongoDB\MongoShellCommand(); $commands[] = new Command\Service\RedisCliCommand(); $commands[] = new Command\Service\ServiceListCommand(); + $commands[] = new Command\Service\ValkeyCliCommand(); $commands[] = new Command\Session\SessionSwitchCommand(); $commands[] = new Command\Backup\BackupCreateCommand(); $commands[] = new Command\Backup\BackupDeleteCommand(); diff --git a/src/Command/Service/RedisCliCommand.php b/src/Command/Service/RedisCliCommand.php index fd8124c55f..5280bf4d74 100644 --- a/src/Command/Service/RedisCliCommand.php +++ b/src/Command/Service/RedisCliCommand.php @@ -2,70 +2,9 @@ namespace Platformsh\Cli\Command\Service; -use Platformsh\Cli\Command\CommandBase; -use Platformsh\Cli\Model\Host\RemoteHost; -use Platformsh\Cli\Service\Relationships; -use Platformsh\Cli\Service\Ssh; -use Platformsh\Cli\Util\OsUtil; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -class RedisCliCommand extends CommandBase +class RedisCliCommand extends ValkeyCliCommand { - protected function configure() - { - $this->setName('service:redis-cli'); - $this->setAliases(['redis']); - $this->setDescription('Access the Redis CLI'); - $this->addArgument('args', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Arguments to add to the Redis command'); - Relationships::configureInput($this->getDefinition()); - Ssh::configureInput($this->getDefinition()); - $this->addProjectOption() - ->addEnvironmentOption() - ->addAppOption(); - $this->addExample('Open the redis-cli shell'); - $this->addExample('Ping the Redis server', 'ping'); - $this->addExample('Show Redis status information', 'info'); - $this->addExample('Scan keys', "-- --scan"); - $this->addExample('Scan keys matching a pattern', '-- "--scan --pattern \'*-11*\'"'); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - if ($this->runningViaMulti && !$input->getArgument('args')) { - throw new \RuntimeException('The redis-cli command cannot run as a shell via multi'); - } - - /** @var \Platformsh\Cli\Service\Relationships $relationshipsService */ - $relationshipsService = $this->getService('relationships'); - $host = $this->selectHost($input, $relationshipsService->hasLocalEnvVar()); - - $service = $relationshipsService->chooseService($host, $input, $output, ['redis']); - if (!$service) { - return 1; - } - - $redisCommand = sprintf( - 'redis-cli -h %s -p %d', - OsUtil::escapePosixShellArg($service['host']), - $service['port'] - ); - if ($args = $input->getArgument('args')) { - if (count($args) === 1) { - $redisCommand .= ' ' . $args[0]; - } else { - $redisCommand .= ' ' . implode(' ', array_map([OsUtil::class, 'escapePosixShellArg'], $args)); - } - } elseif ($this->isTerminal(STDIN) && $host instanceof RemoteHost) { - // Force TTY output when the input is a terminal. - $host->setExtraSshOptions(['RequestTTY yes']); - } - - $this->stdErr->writeln( - sprintf('Connecting to Redis service via relationship %s on %s', $service['_relationship_name'], $host->getLabel()) - ); - - return $host->runCommandDirect($redisCommand); - } + protected $dbName = 'redis'; + protected $dbTitle = 'Redis'; + protected $dbCommand = 'redis-cli'; } diff --git a/src/Command/Service/ValkeyCliCommand.php b/src/Command/Service/ValkeyCliCommand.php new file mode 100644 index 0000000000..1e30208eb9 --- /dev/null +++ b/src/Command/Service/ValkeyCliCommand.php @@ -0,0 +1,76 @@ +setName('service:' . $this->dbCommand); + $this->setAliases([$this->dbName]); + $this->setDescription('Access the ' . $this->dbTitle . ' CLI'); + $this->addArgument('args', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, sprintf('Arguments to add to the %s command', $this->dbCommand)); + Relationships::configureInput($this->getDefinition()); + Ssh::configureInput($this->getDefinition()); + $this->addProjectOption() + ->addEnvironmentOption() + ->addAppOption(); + $this->addExample(sprintf('Open the %s shell', $this->dbCommand)); + $this->addExample(sprintf('Ping the %s server', $this->dbTitle), 'ping'); + $this->addExample(sprintf('Show %s status information', $this->dbTitle), 'info'); + $this->addExample('Scan keys', "-- --scan"); + $this->addExample('Scan keys matching a pattern', '-- "--scan --pattern \'*-11*\'"'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + if ($this->runningViaMulti && !$input->getArgument('args')) { + throw new \RuntimeException(sprintf('The %s command cannot run as a shell via multi', $this->dbCommand)); + } + + /** @var \Platformsh\Cli\Service\Relationships $relationshipsService */ + $relationshipsService = $this->getService('relationships'); + $host = $this->selectHost($input, $relationshipsService->hasLocalEnvVar()); + + $service = $relationshipsService->chooseService($host, $input, $output, [$this->dbName]); + if (!$service) { + return 1; + } + + $command = sprintf( + '%s -h %s -p %d', + $this->dbCommand, + OsUtil::escapePosixShellArg($service['host']), + $service['port'] + ); + if ($args = $input->getArgument('args')) { + if (count($args) === 1) { + $command .= ' ' . $args[0]; + } else { + $command .= ' ' . implode(' ', array_map([OsUtil::class, 'escapePosixShellArg'], $args)); + } + } elseif ($this->isTerminal(STDIN) && $host instanceof RemoteHost) { + // Force TTY output when the input is a terminal. + $host->setExtraSshOptions(['RequestTTY yes']); + } + + $this->stdErr->writeln( + sprintf('Connecting to %s service via relationship %s on %s', $this->dbTitle, $service['_relationship_name'], $host->getLabel()) + ); + + return $host->runCommandDirect($command); + } +}