Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions drupalorg-cli-completion.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
44 changes: 44 additions & 0 deletions src/Cli/Command/Completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace mglaman\DrupalOrgCli\Command;

use mglaman\DrupalOrgCli\Cache;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;

class Completion extends Command
{
protected function configure()
{
$this
->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 $commandToAdd) {
if ($commandToAdd['name'] == $this->getName()) {
continue;
}
$commandsToComplete[] = $commandToAdd['name'];
}
$this->stdOut->writeln(implode(' ', $commandsToComplete));
}
}