RedisCommand.php 1.6 KB

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