2014-01-22 12:45:42 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-01-22 12:45:42 +01:00
|
|
|
|
|
|
|
namespace Icinga\Clicommands;
|
|
|
|
|
|
|
|
use Icinga\Cli\Command;
|
|
|
|
use Icinga\Cli\Loader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Autocomplete for modules, commands and actions
|
|
|
|
*
|
|
|
|
* The autocomplete command shows help for a given command, module and also for a
|
|
|
|
* given module's command or a specific command's action.
|
|
|
|
*
|
2014-03-19 17:25:53 +01:00
|
|
|
* Usage: icingacli autocomplete [<module>] [<command> [<action>]]
|
2014-01-22 12:45:42 +01:00
|
|
|
*/
|
|
|
|
class AutocompleteCommand extends Command
|
|
|
|
{
|
|
|
|
protected $defaultActionName = 'complete';
|
|
|
|
|
|
|
|
protected function suggest($suggestions)
|
|
|
|
{
|
|
|
|
if ($suggestions) {
|
|
|
|
$key = array_search('autocomplete', $suggestions);
|
2014-02-21 14:07:32 +01:00
|
|
|
if ($key !== false) {
|
2014-01-22 12:45:42 +01:00
|
|
|
unset($suggestions[$key]);
|
|
|
|
}
|
|
|
|
echo implode("\n", $suggestions)
|
2014-02-21 14:07:32 +01:00
|
|
|
//. serialize($GLOBALS['argv'])
|
2014-01-22 12:45:42 +01:00
|
|
|
. "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show help for modules, commands and actions [default]
|
|
|
|
*
|
|
|
|
* The help command shows help for a given command, module and also for a
|
|
|
|
* given module's command or a specific command's action.
|
|
|
|
*
|
2014-03-19 17:25:53 +01:00
|
|
|
* Usage: icingacli autocomplete [<module>] [<command> [<action>]]
|
2014-01-22 12:45:42 +01:00
|
|
|
*/
|
|
|
|
public function completeAction()
|
|
|
|
{
|
|
|
|
$module = null;
|
|
|
|
$command = null;
|
|
|
|
$action = null;
|
|
|
|
|
|
|
|
$loader = new Loader($this->app);
|
|
|
|
$params = $this->params;
|
|
|
|
$bare_params = $GLOBALS['argv'];
|
|
|
|
$cword = (int) $params->shift('autoindex');
|
|
|
|
|
|
|
|
$search_word = $bare_params[$cword];
|
|
|
|
if ($search_word === '--') {
|
|
|
|
// TODO: Unfinished, completion missing
|
2014-02-21 14:07:32 +01:00
|
|
|
return $this->suggest(array('--verbose', '--help', '--debug'));
|
2014-01-22 12:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$search = $params->shift();
|
2014-02-21 14:07:32 +01:00
|
|
|
if (!$search) {
|
2014-01-22 12:45:42 +01:00
|
|
|
return $this->suggest(
|
|
|
|
array_merge($loader->listCommands(), $loader->listModules())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$found = $loader->resolveName($search);
|
|
|
|
if ($found) {
|
|
|
|
// Do not return suggestions if we are already on the next word:
|
|
|
|
if ($bare_params[$cword] === $search) {
|
|
|
|
return $this->suggest(array($found));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->suggest($loader->getLastSuggestions());
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = null;
|
|
|
|
if ($loader->hasCommand($found)) {
|
|
|
|
$command = $found;
|
|
|
|
$obj = $loader->getCommandInstance($command);
|
|
|
|
} elseif ($loader->hasModule($found)) {
|
|
|
|
$module = $found;
|
|
|
|
$search = $params->shift();
|
|
|
|
if (! $search) {
|
|
|
|
return $this->suggest(
|
|
|
|
$loader->listModuleCommands($module)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$command = $loader->resolveModuleCommandName($found, $search);
|
|
|
|
if ($command) {
|
|
|
|
// Do not return suggestions if we are already on the next word:
|
|
|
|
if ($bare_params[$cword] === $search) {
|
|
|
|
return $this->suggest(array($command));
|
|
|
|
}
|
|
|
|
$obj = $loader->getModuleCommandInstance(
|
|
|
|
$module,
|
|
|
|
$command
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->suggest($loader->getLastSuggestions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($obj !== null) {
|
|
|
|
$search = $params->shift();
|
|
|
|
if (! $search) {
|
|
|
|
return $this->suggest($obj->listActions());
|
|
|
|
}
|
|
|
|
$action = $loader->resolveObjectActionName(
|
|
|
|
$obj,
|
|
|
|
$search
|
|
|
|
);
|
|
|
|
if ($action) {
|
|
|
|
if ($bare_params[$cword] === $search) {
|
|
|
|
return $this->suggest(array($action));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->suggest($loader->getLastSuggestions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|