summarylogtreecommitdiffstats
path: root/completion
blob: 9f7a1581b3567583368deea14b1deeac4fb8a75f (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
# gitern(8) completion                                     -*- shell-script -*-

_accounts()
{
    gitern pubkey list | grep ':$' | tr -d ':'
}

_gitern()
{
    local cur prev words cword
    _init_completion || return

    local subcword cmd subcmd=""
    for ((subcword = 1; subcword < ${#words[@]} - 1; subcword++)); do
        [[ ${words[subcword]} == -b?(atch) ]] && return
        [[ -v cmd ]] && subcmd=${words[subcword]} && break
        [[ ${words[subcword]} != -* && \
        ${words[subcword - 1]} != -@(f?(amily)|rc?(vbuf)) ]] &&
            cmd=${words[subcword]}
    done

    if [[ ! -v cmd ]]; then
        COMPREPLY=($(compgen -W "create delete list account pubkey help" -- "$cur"))
        return
    fi

    case $cmd in
        account)
            (( cword == subcword )) &&
                COMPREPLY=($(compgen -W "$(_accounts)" -- "$cur"))
            ;;
        create | delete | list) ;;
        help)
            case $subcmd in
                pubkey)
                    (( cword == subcword+1 )) &&
                        COMPREPLY=($(compgen -W "add list remove" -- "$cur"))
                    ;;
                *)
                    ((cword == subcword)) &&
                        COMPREPLY=($(compgen -W "create delete list account pubkey" -- "$cur"))
                    ;;
            esac
            ;;
        pubkey)
            case $subcmd in
                add)
                    (( cword == subcword+1 )) &&
                        COMPREPLY=($(compgen -W "$(_accounts)" -- "$cur"))
                    ;;
                list)
                    case $cur in
                        -*)
                            COMPREPLY=($(compgen -W "--full" -- "$cur"))
                            ;;
                        *)
                            if [[ $prev == "--full" && $words[cword-2] == "list" || $prev == "list" ]]; then
                                COMPREPLY=($(compgen -W "$(_accounts)" -- "$cur"))
                            fi
                            ;;
                    esac
                    ;;
                remove)
                    case $((cword - subcword)) in
                        1)
                            COMPREPLY=($(compgen -W "$(_accounts)" -- "$cur"))
                            ;;
                        2)
                            COMPREPLY=($(compgen -W "$(gitern pubkey list ${words[cword-1]}| grep 'SHA' | cut -d' ' -f1)" -- "$cur"))
                            ;;
                    esac
                    ;;
                *)
                    ((cword == subcword)) &&
                        COMPREPLY=($(compgen -W "add list remove" -- "$cur"))
                    ;;
            esac
            ;;
    esac

} &&
    complete -F _gitern gitern

# ex: filetype=sh