summaryrefslogtreecommitdiff
path: root/scripts/deploy/completions/bash/completion_jvv.sh
blob: ce5668be9e4464edee8b54f40b39417581033a4c (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
#!/bin/bash
# The JustEnoughVCS CommandLine Completion

_jvv_completion() {
    local cur prev words cword
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    words=("${COMP_WORDS[@]}")
    cword=$COMP_CWORD

    # Current
    local cmd="${words[0]}"
    local subcmd="${words[1]}"
    local subsubcmd="${words[2]}"

    # Subcommands
    local base_commands="create init here member service listen members -c -i -H -m -l -M"

    # Subcommands - Member
    local member_commands="register remove list help + - ls"

    # Subcommands - Service
    local service_commands="listen help"

    # Completion subcommands
    if [[ $cword -eq 1 ]]; then
        COMPREPLY=($(compgen -W "$base_commands" -- "$cur"))
        return 0
    fi

    # Completion member
    if [[ "$subcmd" == "member" || "$subcmd" == "-m" ]]; then
        if [[ $cword -eq 2 ]]; then
            COMPREPLY=($(compgen -W "$member_commands" -- "$cur"))
            return 0
        fi

        case "$subsubcmd" in
            "remove"|"-")
                if [[ $cword -eq 3 ]]; then
                    # Use jvv member list --raw
                    local members
                    members=$($cmd member list --raw 2>/dev/null)
                    COMPREPLY=($(compgen -W "$members" -- "$cur"))
                fi
                ;;
        esac
        return 0
    fi

    # Completion service
    if [[ "$subcmd" == "service" ]]; then
        if [[ $cword -eq 2 ]]; then
            COMPREPLY=($(compgen -W "$service_commands" -- "$cur"))
            return 0
        fi
        return 0
    fi

    # aliases
    case "$subcmd" in
        "-m")
            if [[ $cword -eq 2 ]]; then
                COMPREPLY=($(compgen -W "$member_commands" -- "$cur"))
            fi
            ;;
        "listen"|"-l")
            # listen command has no arguments to complete
            ;;
        "members"|"-M")
            # members command has no arguments to complete
            ;;
    esac
}

# Register completion function
complete -F _jvv_completion jvv