feat: Added autocomplete file path in cli command

This commit is contained in:
Aerex 2021-07-05 15:40:16 -05:00
parent a0b3e9548b
commit ed4dc009fd
1 changed files with 41 additions and 21 deletions

View File

@ -1,6 +1,5 @@
<?php <?php
namespace Aerex\BaikalStorage\Commands; namespace Aerex\BaikalStorage\Commands;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -12,8 +11,6 @@ use Symfony\Component\Console\Question\ChoiceQuestion;
use Monolog\Logger as Monolog; use Monolog\Logger as Monolog;
use Aerex\BaikalStorage\Configs\ConfigBuilder; use Aerex\BaikalStorage\Configs\ConfigBuilder;
class CreateConfigFileCommand extends Command { class CreateConfigFileCommand extends Command {
protected $io; protected $io;
@ -48,22 +45,46 @@ class CreateConfigFileCommand extends Command {
->setDescription('Create baikal storage plugin configuration file'); ->setDescription('Create baikal storage plugin configuration file');
} }
protected function autocompleteFilePathCallback(string $input): array {
// Strip any characters from the last slash to the end of the string
// to keep only the last directory and generate suggestions for it
$inputPath = preg_replace('%(/|^)[^/]*$%', '$1', $input);
$inputPath = '' === $inputPath ? '.': $inputPath;
$foundFilesAndDirs = scandir($inputPath) ?: [];
// Excluded restricted directories
$restrictedDirs = ['dev', 'bin', 'boot', 'etc', 'input', 'lib',
'lib64', 'mnt', 'proc', 'run', 'sbin', 'sys', 'srv', 'usr', 'var'];
$foundSafeFileAndDirs = array_diff($foundFilesAndDirs, $restrictedDirs);
return array_map(function($dirOrFile) use ($inputPath) {
return $inputPath.$dirOrFile;
}, $foundSafeFileAndDirs);
}
protected function execute(InputInterface $input, OutputInterface $output) { protected function execute(InputInterface $input, OutputInterface $output) {
$this->io = new SymfonyStyle($input, $output); $this->io = new SymfonyStyle($input, $output);
$this->io->title('Baikal Storage Plugin Configuration Creation'); $this->io->title('Baikal Storage Plugin Configuration Creation');
$filePath = $this->io->askQuestion(new Question('Where to create `config.yaml` configuration file?')); // TODO: move create config file code block to function
$this->fullFilePath = $this->verifyFileCanBeCreated($filePath, CreateConfigFileCommand::$CONFIG_FILE_NAME); $question = new Question('Where to create `config.yaml` configuration file?');
$question->setAutocompleterCallback($this->autocompleteFilePathCallback);
$filePath = $this->io->askQuestion($question);
$this try {
->setupGeneralConfigs() $this->fullFilePath = $this->verifyAndCreateFile($filePath, CreateConfigFileCommand::$CONFIG_FILE_NAME);
->setupStorages() $this->setupGeneralConfigs()
->saveConfigs(); ->setupStorages()
->saveConfigs();
} catch (\Exception $e) {
return Command::FAILURE;
}
// or return this if some error happened during the execution $this->output->writeln("`config.yaml file created at $filePath");
// (it's equivalent to returning int(1))
// return Command::FAILURE; return Command::SUCCESS;
} }
protected function getStorageNames() { protected function getStorageNames() {
@ -78,7 +99,7 @@ class CreateConfigFileCommand extends Command {
return $storages; return $storages;
} }
protected function verifyFileCanBeCreated($filePath, $fileName) { protected function verifyAndCreateFile($filePath, $fileName) {
if (empty($filePath)) { if (empty($filePath)) {
throw new \RuntimeException('Configuration file path cannot be empty'); throw new \RuntimeException('Configuration file path cannot be empty');
} }
@ -105,7 +126,7 @@ class CreateConfigFileCommand extends Command {
if ($this->io->confirm('Enable logging?')) { if ($this->io->confirm('Enable logging?')) {
$this->configs['general']['logger'] = []; $this->configs['general']['logger'] = [];
$logFilePath = $this->io->askQuestion(new Question('Where to create log file?')); $logFilePath = $this->io->askQuestion(new Question('Where to create log file?'));
$this->configs['general']['logger']['file'] = $this->verifyFileCanBeCreated($logFilePath, CreateConfigFileCommand::$LOGGER_FILE_NAME); $this->configs['general']['logger']['file'] = $this->verifyAndCreateFile($logFilePath, CreateConfigFileCommand::$LOGGER_FILE_NAME);
$logLevelChoiceQuestion = new ChoiceQuestion('Log level (defaults to ERROR)', array_keys(Monolog::getLevels()), 4); $logLevelChoiceQuestion = new ChoiceQuestion('Log level (defaults to ERROR)', array_keys(Monolog::getLevels()), 4);
$logLevelChoiceQuestion->setErrorMessage('Log level %s is invalid'); $logLevelChoiceQuestion->setErrorMessage('Log level %s is invalid');
@ -134,5 +155,4 @@ class CreateConfigFileCommand extends Command {
$configBuilder = new ConfigBuilder($this->fullFilePath); $configBuilder = new ConfigBuilder($this->fullFilePath);
$configBuilder->saveConfigs($this->configs); $configBuilder->saveConfigs($this->configs);
} }
} }