Skip to content

Commit fd1d0e6

Browse files
committed
feat: add command to send raw commands to redis
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 3a60e40 commit fd1d0e6

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

core/register_command.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140
$application->add(Server::get(Command\Security\BruteforceResetAttempts::class));
141141
$application->add(Server::get(Command\SetupChecks::class));
142142
$application->add(Server::get(Command\FilesMetadata\Get::class));
143+
144+
$application->add(Server::get(Command\Memcache\RedisCommand::class));
143145
} else {
144146
$application->add(Server::get(Command\Maintenance\Install::class));
145147
}

0 commit comments

Comments
 (0)