ListDirectoryCommand.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Commands;
  3. use App\AppService;
  4. use Symfony\Component\Console\Helper\Table;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use YdbPlatform\Ydb\Ydb;
  10. class ListDirectoryCommand extends Command
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected static $defaultName = 'ls';
  16. /**
  17. * @var AppService
  18. */
  19. protected $appService;
  20. public function __construct()
  21. {
  22. $this->appService = new AppService;
  23. parent::__construct();
  24. }
  25. protected function configure()
  26. {
  27. $this->setDescription('List a directory.');
  28. $this->addArgument('dirname', InputArgument::OPTIONAL, 'The directory name.');
  29. }
  30. /**
  31. * @param InputInterface $input
  32. * @param OutputInterface $output
  33. * @return int
  34. */
  35. protected function execute(InputInterface $input, OutputInterface $output)
  36. {
  37. $dirname = $input->getArgument('dirname') ?: '';
  38. $ydb = $this->appService->initYdb();
  39. $result = $ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
  40. $scheme = $ydb->scheme();
  41. return $scheme->listDirectory($dirname);
  42. });
  43. if (!empty($result))
  44. {
  45. $t = new Table($output);
  46. $t
  47. ->setHeaders(['name', 'type', 'owner'])
  48. ->setRows(array_map(function($row) {
  49. return [
  50. $row['name'] ?? null,
  51. $row['type'] ?? null,
  52. $row['owner'] ?? null,
  53. ];
  54. }, $result))
  55. ;
  56. $t->render();
  57. }
  58. else
  59. {
  60. $output->writeln('Empty directory');
  61. }
  62. return Command::SUCCESS;
  63. }
  64. }