From 2cb24cc091883935606c3a003963fe313a0fd2b0 Mon Sep 17 00:00:00 2001 From: Hans Fredrik Nordhaug Date: Sun, 10 May 2020 15:09:35 +0200 Subject: [PATCH 1/4] Added command "complete" to list commands for Bash completion. --- src/Cli/Application.php | 1 + src/Cli/Command/Completion.php | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/Cli/Command/Completion.php diff --git a/src/Cli/Application.php b/src/Cli/Application.php index a456a05..a59325c 100644 --- a/src/Cli/Application.php +++ b/src/Cli/Application.php @@ -41,6 +41,7 @@ public function getCommands() } $commands[] = new Command\CacheClear(); + $commands[] = new Command\Completion(); $commands[] = new Command\DrupalCi\ListResults(); $commands[] = new Command\DrupalCi\Watch(); $commands[] = new Command\Issue\Link(); diff --git a/src/Cli/Command/Completion.php b/src/Cli/Command/Completion.php new file mode 100644 index 0000000..71676f9 --- /dev/null +++ b/src/Cli/Command/Completion.php @@ -0,0 +1,44 @@ +setName('complete') + ->setDescription('List commands for (Bash) completion'); + } + + /** + * {@inheritdoc} + * + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $command = $this->getApplication()->find('list'); + + $arguments = [ + '--format' => 'json' + ]; + $input = new ArrayInput($arguments); + $output = new BufferedOutput(); + $returnCode = $command->run($input, $output); + $commandListArray = json_decode($output->fetch(), true); + $commandsToComplete = []; + foreach ($commandListArray['commands'] as $command) { + if ($command['name'] == $this->getName()) { + continue; + } + $commandsToComplete[] = $command['name']; + } + $this->stdOut->writeln(implode(' ', $commandsToComplete)); + } +} From 4787d956f4af9cb76e3b93f6b166925bdb1cdbd3 Mon Sep 17 00:00:00 2001 From: Hans Fredrik Nordhaug Date: Sun, 10 May 2020 15:10:11 +0200 Subject: [PATCH 2/4] Added Bash completion file (using the "complete" command). --- drupalorg-cli-completion.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 drupalorg-cli-completion.sh diff --git a/drupalorg-cli-completion.sh b/drupalorg-cli-completion.sh new file mode 100644 index 0000000..0778848 --- /dev/null +++ b/drupalorg-cli-completion.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +_drupalorgcli_complete() { + local cur prev words cword command + _init_completion -n : || return + + # This is a minimal completion - global options and options for each command is not completed. + # Many edge cases are not covered, for example completion after command option is wrong. + + COMPREPLY=() + + # Stop completing if command is already present, except for "help". + # (This check fails when options follows command.) + if [[ -n $prev ]] && [[ $prev != "help" ]]; then + command=$(compgen -W "$(drupalorg complete)" -- "$prev") + if [[ $command == $prev ]]; then + return 0 + fi + fi + + COMPREPLY=( $(compgen -W "$(drupalorg complete)" -- "$cur") ) + __ltrim_colon_completions "$cur" +} && +complete -F _drupalorgcli_complete drupalorg From 1c454027e507175c967192cf7cfdf42c53ffee33 Mon Sep 17 00:00:00 2001 From: Hans Fredrik Nordhaug Date: Sun, 10 May 2020 15:46:50 +0200 Subject: [PATCH 3/4] Fixing validation errors. --- src/Cli/Command/Completion.php | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Cli/Command/Completion.php b/src/Cli/Command/Completion.php index 71676f9..1b16721 100644 --- a/src/Cli/Command/Completion.php +++ b/src/Cli/Command/Completion.php @@ -23,22 +23,22 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $command = $this->getApplication()->find('list'); + $command = $this->getApplication()->find('list'); - $arguments = [ - '--format' => 'json' - ]; - $input = new ArrayInput($arguments); - $output = new BufferedOutput(); - $returnCode = $command->run($input, $output); - $commandListArray = json_decode($output->fetch(), true); - $commandsToComplete = []; - foreach ($commandListArray['commands'] as $command) { - if ($command['name'] == $this->getName()) { - continue; - } - $commandsToComplete[] = $command['name']; - } - $this->stdOut->writeln(implode(' ', $commandsToComplete)); + $arguments = [ + '--format' => 'json' + ]; + $input = new ArrayInput($arguments); + $output = new BufferedOutput(); + $returnCode = $command->run($input, $output); + $commandListArray = json_decode($output->fetch(), true); + $commandsToComplete = []; + foreach ($commandListArray['commands'] as $commandToAdd) { + if ($commandToAdd['name'] == $this->getName()) { + continue; + } + $commandsToComplete[] = $commandToAdd['name']; + } + $this->stdOut->writeln(implode(' ', $commandsToComplete)); } } From b5c0b931ea8aef75858e340823374f31884b5067 Mon Sep 17 00:00:00 2001 From: Hans Fredrik Nordhaug Date: Wed, 13 May 2020 11:08:28 +0200 Subject: [PATCH 4/4] Added installation instructions to Readme. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 00b3448..dd5f070 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,18 @@ Use the following command to install the command line tool via Composer: `composer global require mglaman/drupalorg-cli` +### Installing (Bash) completion + +`drupalorg` comes with completion support for all commands, excluding options. + +To activate it, either source the completion file or add it to the system-wide completion directory, normally `/etc/bash_completion.d/`. + +In your `.bashrc` (or `.profile`) add + +``` +source [...]/vendor/mglaman/drupalorg-cli/drupalorg-cli-completion.sh +``` + ## Updating Automatic updating is not yet supported. You will need to manually download new releases.