feat: Added autocomplete file path in cli command
This commit is contained in:
parent
a0b3e9548b
commit
ed4dc009fd
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Aerex\BaikalStorage\Commands;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
@ -12,8 +11,6 @@ use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Monolog\Logger as Monolog;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
|
||||
|
||||
|
||||
class CreateConfigFileCommand extends Command {
|
||||
protected $io;
|
||||
|
||||
@ -48,22 +45,46 @@ class CreateConfigFileCommand extends Command {
|
||||
->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) {
|
||||
$this->io = new SymfonyStyle($input, $output);
|
||||
|
||||
$this->io->title('Baikal Storage Plugin Configuration Creation');
|
||||
|
||||
$filePath = $this->io->askQuestion(new Question('Where to create `config.yaml` configuration file?'));
|
||||
$this->fullFilePath = $this->verifyFileCanBeCreated($filePath, CreateConfigFileCommand::$CONFIG_FILE_NAME);
|
||||
// TODO: move create config file code block to function
|
||||
$question = new Question('Where to create `config.yaml` configuration file?');
|
||||
$question->setAutocompleterCallback($this->autocompleteFilePathCallback);
|
||||
$filePath = $this->io->askQuestion($question);
|
||||
|
||||
$this
|
||||
->setupGeneralConfigs()
|
||||
try {
|
||||
$this->fullFilePath = $this->verifyAndCreateFile($filePath, CreateConfigFileCommand::$CONFIG_FILE_NAME);
|
||||
$this->setupGeneralConfigs()
|
||||
->setupStorages()
|
||||
->saveConfigs();
|
||||
} catch (\Exception $e) {
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// or return this if some error happened during the execution
|
||||
// (it's equivalent to returning int(1))
|
||||
// return Command::FAILURE;
|
||||
$this->output->writeln("`config.yaml file created at $filePath");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
protected function getStorageNames() {
|
||||
@ -78,7 +99,7 @@ class CreateConfigFileCommand extends Command {
|
||||
return $storages;
|
||||
}
|
||||
|
||||
protected function verifyFileCanBeCreated($filePath, $fileName) {
|
||||
protected function verifyAndCreateFile($filePath, $fileName) {
|
||||
if (empty($filePath)) {
|
||||
throw new \RuntimeException('Configuration file path cannot be empty');
|
||||
}
|
||||
@ -105,7 +126,7 @@ class CreateConfigFileCommand extends Command {
|
||||
if ($this->io->confirm('Enable logging?')) {
|
||||
$this->configs['general']['logger'] = [];
|
||||
$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->setErrorMessage('Log level %s is invalid');
|
||||
@ -134,5 +155,4 @@ class CreateConfigFileCommand extends Command {
|
||||
$configBuilder = new ConfigBuilder($this->fullFilePath);
|
||||
$configBuilder->saveConfigs($this->configs);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user