summaryrefslogtreecommitdiffstats
path: root/bin/_hcl_completions
blob: 16f551cb73ebf1ed18219db7bb29b2744ac03e1f (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
#!/bin/bash

_hcl_getProjects()
{
    $1 tasks | perl -ne 's/ - (?!.* -).*//g; print;' | sed -e 's/\s[0-9]\+\s/ /g' | sed -e 's/\s/ /g' | sort | uniq
}

_hcl_getTasks()
{
    $1 tasks | grep "^$2" | sed -e 's/\s/ /g' | sort | uniq | cut -d" " -f 2-
}

_hcl()
{
    local count=${#COMP_WORDS[@]}
    local script=${COMP_WORDS[0]}
    local cur=${COMP_WORDS[COMP_CWORD]}
    case $count in
        2)
            COMPREPLY=( $(compgen -W "start resume log stop note show tasks alias unalias aliases cancel nvm oops config status" -- $cur) )
            ;;
        3)
            local namespace=${COMP_WORDS[1]}
            
            case $namespace in
                'alias')
                    COMPREPLY=( $(compgen -W "" -- $cur) )
                ;;
                'start'|'resume'|'log')
                    COMPREPLY=( $(compgen -W "$($script aliases | sed -e 's/,//g')" -- $cur) )
                ;;
            esac
            ;;
        4)
            local namespace=${COMP_WORDS[1]}
            local task=${COMP_WORDS[2]}
            case $namespace in
                'alias')
                    local IFS_OLD="$IFS";
                    local IFS=$'\n';
                    COMPREPLY=( $(compgen -W "$(_hcl_getProjects $script)" -- $cur) )
                    local IFS=$IFS_OLD;
                    if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
                        COMPREPLY=$(echo $COMPREPLY | sed -e 's/\s.*$//g')
                    fi
                ;;
            esac
            ;;
        5)
            local namespace=${COMP_WORDS[1]}
            local task=${COMP_WORDS[2]}
            local projectid=${COMP_WORDS[3]}
            case $namespace in
                'alias')
                    local IFS_OLD="$IFS";
                    local IFS=$'\n';
                    COMPREPLY=( $(compgen -W "$(_hcl_getTasks $script $projectid)" -- $cur) )
                    local IFS=$IFS_OLD;
                    if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
                        COMPREPLY=$(echo $COMPREPLY | sed -e 's/\s.*$//g')
                    fi
                ;;
            esac
            ;;
    esac 
}
complete -F _hcl hcl