1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Commands;
- use App\AppService;
- use Symfony\Component\Console\Helper\Table;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use YdbPlatform\Ydb\Ydb;
- class ListDirectoryCommand extends Command
- {
- /**
- * @var string
- */
- protected static $defaultName = 'ls';
- /**
- * @var AppService
- */
- protected $appService;
- public function __construct()
- {
- $this->appService = new AppService;
- parent::__construct();
- }
- protected function configure()
- {
- $this->setDescription('List a directory.');
- $this->addArgument('dirname', InputArgument::OPTIONAL, 'The directory name.');
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- * @return int
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $dirname = $input->getArgument('dirname') ?: '';
- $ydb = $this->appService->initYdb();
- $result = $ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
- $scheme = $ydb->scheme();
- return $scheme->listDirectory($dirname);
- });
- if (!empty($result))
- {
- $t = new Table($output);
- $t
- ->setHeaders(['name', 'type', 'owner'])
- ->setRows(array_map(function($row) {
- return [
- $row['name'] ?? null,
- $row['type'] ?? null,
- $row['owner'] ?? null,
- ];
- }, $result))
- ;
- $t->render();
- }
- else
- {
- $output->writeln('Empty directory');
- }
- return Command::SUCCESS;
- }
- }
|