|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl> |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OC\Core\Command\Memcache; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OC\RedisFactory; |
| 13 | +use OCP\ICertificateManager; |
| 14 | +use OCP\IL10N; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class RedisCommand extends Base { |
| 20 | + protected IL10N $l; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + protected ICertificateManager $certificateManager, |
| 24 | + protected RedisFactory $redisFactory, |
| 25 | + ) { |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function configure() { |
| 30 | + $this |
| 31 | + ->setName('memcache:redis') |
| 32 | + ->setDescription('Send raw redis command to the configured redis server') |
| 33 | + ->addArgument('redis-command', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The command to run'); |
| 34 | + parent::configure(); |
| 35 | + } |
| 36 | + |
| 37 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 38 | + $command = $input->getArgument('redis-command'); |
| 39 | + if (!$this->redisFactory->isAvailable()) { |
| 40 | + $output->writeln("<error>No redis server configured</error>"); |
| 41 | + return 1; |
| 42 | + } |
| 43 | + try { |
| 44 | + $redis = $this->redisFactory->getInstance(); |
| 45 | + } catch (\Exception $e) { |
| 46 | + $output->writeln('Failed to connect to redis: ' . $e->getMessage()); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + $redis->setOption(\Redis::OPT_REPLY_LITERAL, true); |
| 51 | + $result = $redis->rawCommand(...$command); |
| 52 | + if ($result === false) { |
| 53 | + $output->writeln("<error>Redis command failed</error>"); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + $output->writeln($result); |
| 57 | + return 0; |
| 58 | + } |
| 59 | +} |
0 commit comments