summaryrefslogtreecommitdiffstats
path: root/application/clicommands/AutocompleteCommand.php
blob: 34e4005d5d233a5c359618189f855e8bb3e41eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

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.
 *
 * Usage: icingacli autocomplete [<module>] [<command> [<action>]]
 */
class AutocompleteCommand extends Command
{
    protected $defaultActionName = 'complete';

    protected function suggest($suggestions)
    {
        if ($suggestions) {
            $key = array_search('autocomplete', $suggestions);
            if ($key !== false) {
                unset($suggestions[$key]);
            }
            echo implode("\n", $suggestions)
            //. serialize($GLOBALS['argv'])
            . "\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.
     *
     * Usage: icingacli autocomplete [<module>] [<command> [<action>]]
     */
    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
            return $this->suggest(array('--verbose', '--help', '--debug'));
        }

        $search = $params->shift();
        if (!$search) {
            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());
            }
        }
    }
}