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;
@ -33,12 +30,12 @@ class CreateConfigFileCommand extends Command {
$configs['taskrc'] = $taskrcFilePath; $configs['taskrc'] = $taskrcFilePath;
$filePath = $this->io->askQuestion(new Question('Where is the data location (taskdata)')); $filePath = $this->io->askQuestion(new Question('Where is the data location (taskdata)'));
$taskDataFilePath = $filePath; $taskDataFilePath = $filePath;
if (!file_exists($taskDataFilePath)) { if (!file_exists($taskDataFilePath)) {
throw new \RuntimeException("The task.data location $taskDataFilePath does not exist"); throw new \RuntimeException("The task.data location $taskDataFilePath does not exist");
} }
$configs['taskdata'] = $taskDataFilePath; $configs['taskdata'] = $taskDataFilePath;
$displayName = $this->io->askQuestion(new Question('What is the displayname for the default calendar (e.g default)')); $displayName = $this->io->askQuestion(new Question('What is the displayname for the default calendar (e.g default)'));
$configs['default_calendar'] = isset($displayName) ? $displayName : 'default'; $configs['default_calendar'] = isset($displayName) ? $displayName : 'default';
$this->configs['storages']['taskwarior'] = $configs; $this->configs['storages']['taskwarior'] = $configs;
} }
@ -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() {
@ -74,13 +95,13 @@ class CreateConfigFileCommand extends Command {
})); }));
array_walk($storages, function(&$storage) { array_walk($storages, function(&$storage) {
$storage = strtolower(preg_replace('/\.php/', '', $storage)); $storage = strtolower(preg_replace('/\.php/', '', $storage));
}); });
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');
} }
if (!is_dir($filePath)) { if (!is_dir($filePath)) {
@ -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');
@ -128,11 +149,10 @@ class CreateConfigFileCommand extends Command {
$storageConfigMethod->invoke($this); $storageConfigMethod->invoke($this);
} }
return $this; return $this;
} }
protected function saveConfigs() { protected function saveConfigs() {
$configBuilder = new ConfigBuilder($this->fullFilePath); $configBuilder = new ConfigBuilder($this->fullFilePath);
$configBuilder->saveConfigs($this->configs); $configBuilder->saveConfigs($this->configs);
} }
} }