Passed
Push — master ( 46bb2c...c0eb20 )
by Anton
04:38 queued 02:01
created

AutocompleteCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 3
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 80 2
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * @codeCoverageIgnore
17
 */
18
class AutocompleteCommand extends Command
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('autocomplete')
27
            ->setDescription('Install command line autocompletion capabilities')
28
            ->addOption('--install', null, InputOption::VALUE_NONE);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        if ($input->getOption('install')) {
37
            $output->write(<<<'BASH'
38
#!/bin/bash
39
40
_deployer()
41
{
42
    local cur script com opts
43
    COMPREPLY=()
44
    _get_comp_words_by_ref -n : cur words
45
46
    # for an alias, get the real script behind it
47
    if [[ $(type -t ${words[0]}) == "alias" ]]; then
48
        script=$(alias ${words[0]} | sed -E "s/alias ${words[0]}='(.*)'/\1/")
49
    else
50
        script=${words[0]}
51
    fi
52
53
    # lookup for command
54
    for word in ${words[@]:1}; do
55
        if [[ $word != -* ]]; then
56
            com=$word
57
            break
58
        fi
59
    done
60
61
    # completing for an option
62
    if [[ ${cur} == --* ]] ; then
63
        opts=$script
64
        [[ -n $com ]] && opts=$opts" -h "$com
65
        opts=$($opts --no-ansi 2>/dev/null | sed -n '/Options/,/^$/p' | sed -e '1d;$d' | sed 's/[^--]*\(--.*\)/\1/' | sed -En 's/[^ ]*(-(-[[:alnum:]]+){1,}).*/\1/p' | awk '{$1=$1};1'; exit ${PIPESTATUS[0]});
66
        [[ $? -eq 0 ]] || return 0;
67
        COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
68
        __ltrim_colon_completions "$cur"
69
70
        return 0
71
    fi
72
		
73
    # completing for a command
74
    if [[ $cur == $com ]]; then
75
        coms=$($script list --raw 2>/dev/null | awk '{print $1}'; exit ${PIPESTATUS[0]})
76
        [[ $? -eq 0 ]] || return 0;
77
        COMPREPLY=($(compgen -W "${coms}" -- ${cur}))
78
        __ltrim_colon_completions "$cur"
79
80
        return 0;
81
    fi
82
}
83
84
complete -o default -F _deployer dep
85
86
BASH
87
            );
88
        } else {
89
            $output->write(<<<'HELP'
90
To install Deployer autocomplete run one of the following commands:            
91
            
92
<comment># Bash (Ubuntu/Debian)</comment>
93
94
  dep autocomplete --install | sudo tee /etc/bash_completion.d/deployer
95
96
<comment># Bash (Mac OSX with Homebrew "bash-completion")</comment>
97
98
  dep autocomplete --install > $(brew --prefix)/etc/bash_completion.d/deployer
99
100
<comment># Zsh</comment>
101
102
  dep autocomplete --install > ~/.deployer_completion && echo "source ~/.deployer_completion" >> ~/.zshrc
103
104
<comment># Fish</comment>
105
106
  dep autocomplete --install > ~/.config/fish/completions/deployer.fish
107
108
Autocomplete will be working after restarting terminal or you can run "source ~/.bash_profile", etc.
109
110
HELP
111
            );
112
        }
113
    }
114
}
115