aboutsummaryrefslogtreecommitdiffstats
path: root/share/bash-completion/completions/continuum-party
blob: 570aeb136de922968c7b2ff42e14cfbc71a5ea78 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# bash completion for continuum-party, a party's client of a federation    -*- shell-script -*-
#
# Completes its commands and options, and from the node it works with (the options given
# before the command: --node, --federation, --config and the rest) the proposals on the
# current version by hash, the parties, the places holding a value; the federations in
# ~/.config/continuum/federations; and the revisions' features it knows.

# The client, with the options given before the command, its errors left out.
_continuum_party_asking()
{
    "${_continuum_party_client[@]}" "$@" 2>/dev/null
}

# The proposals on the current version, by hash.
_continuum_party_proposals()
{
    _continuum_party_asking proposals | sed -n 's/^\([0-9a-f]\{64\}\): .*/\1/p'
}

# The parties of the current version.
_continuum_party_parties()
{
    _continuum_party_asking status |
        awk '/^parties$/ { on = 1; next } on && /^[^ ]/ { on = 0 } on && /^  [^ ]/ { print $1 }'
}

# The places holding a value, and the paths on their way.
_continuum_party_places()
{
    _continuum_party_asking values | cut -d' ' -f1 |
        awk -F/ '{ path = $1; print path; for (i = 2; i <= NF; i++) { path = path "/" $i; print path } }' |
        sort -u
}

# The features of the revisions the client knows, as `--version` names them.
_continuum_party_features()
{
    "${_continuum_party_client[0]}" --version 2>/dev/null |
        grep -o '([a-z0-9-]*)' | tr -d '()'
}

# The federations, by their directories' names.
_continuum_party_federations()
{
    local dir=${XDG_CONFIG_HOME:-$HOME/.config}/continuum/federations entry
    [[ -d $dir ]] || return
    for entry in "$dir"/*/; do
        [[ -d $entry ]] || continue
        entry=${entry%/}
        printf '%s\n' "${entry##*/}"
    done
}

# Files, or directories to find them in.
_continuum_party_files()
{
    if declare -F _filedir >/dev/null; then
        _filedir
    else
        COMPREPLY=($(compgen -f -- "$cur"))
    fi
}

_continuum_party_words()
{
    COMPREPLY=($(compgen -W "$1" -- "$cur"))
}

_continuum_party()
{
    local cur prev words cword
    if declare -F _init_completion >/dev/null; then
        _init_completion -n =: || return
    else
        words=("${COMP_WORDS[@]}")
        cword=$COMP_CWORD
        cur=${words[cword]}
        prev=${words[cword - 1]}
    fi

    local global_options='--node --token --key --record --party --federation --config
        --unset --help --version'
    local commands='init write keys nodes next propose vote again restore found root fork
        check judge proposals show status values get twice help'
    local intents='admit remove give take keys nodes checkpoint revision'

    # The command, the intent of `propose` and `fork`, and the arguments after them that
    # are no options, as far as the word completed.
    local i command= intent= word
    local -a positional=()
    _continuum_party_client=("${words[0]}")
    for ((i = 1; i < cword; i++)); do
        word=${words[i]}
        if [[ -z $command ]]; then
            case $word in
                --node | --token | --key | --record | --party | --federation | --config | --unset)
                    _continuum_party_client+=("$word" "${words[i + 1]}")
                    ((i++))
                    ;;
                -*) _continuum_party_client+=("$word") ;;
                *) command=$word ;;
            esac
            continue
        fi
        case $word in
            --keys | --nodes | --at | --founder | --rule | --revision | --root | --add | \
                --remove | --set)
                # Options taking values: those up to the next option are theirs.
                while ((i + 1 < cword)) && [[ ${words[i + 1]} != -* ]]; do
                    ((i++))
                    case $word in
                        --keys | --nodes | --at | --add | --remove | --set) ;;
                        *) break ;;
                    esac
                done
                ;;
            -*) ;;
            *)
                if [[ -z $intent && ${#positional[@]} -eq 0 ]] &&
                    [[ $command == propose || $command == fork ]] &&
                    [[ " $intents proposal " == *" $word "* ]]; then
                    intent=$word
                else
                    positional+=("$word")
                fi
                ;;
        esac
    done

    # A value of the option before.
    case $prev in
        --federation)
            _continuum_party_words "$(_continuum_party_federations)"
            return
            ;;
        --key | --record | --config | --root | --keys)
            _continuum_party_files
            return
            ;;
        --revision)
            _continuum_party_words "$(_continuum_party_features)"
            return
            ;;
        --add | --remove | --set)
            # Keys, or files of them; nodes by ID.
            [[ $command == keys ]] && _continuum_party_files
            return
            ;;
        --node | --token | --unset | --nodes | --founder | --rule | --at | --wait | --party)
            return
            ;;
    esac

    if [[ -z $command ]]; then
        if [[ $cur == -* ]]; then
            _continuum_party_words "$global_options"
        else
            _continuum_party_words "$commands"
        fi
        return
    fi

    local n=${#positional[@]}
    case $command in
        propose | fork)
            local own
            if [[ $command == propose ]]; then
                own='--yes --print --wait'
            else
                own='--at --founder'
            fi
            if [[ -z $intent ]]; then
                if [[ $cur == -* ]]; then
                    [[ $command == propose ]] && _continuum_party_words "$own"
                elif ((n == 0)); then
                    local offered=$intents
                    [[ $command == fork ]] && offered="${intents/checkpoint/} proposal"
                    _continuum_party_words "$offered"
                    local -a named=("${COMPREPLY[@]}")
                    _continuum_party_files
                    COMPREPLY+=("${named[@]}")
                fi
                return
            fi
            case $intent in
                admit) own+=' --keys --nodes' ;;
            esac
            if [[ $cur == -* ]]; then
                _continuum_party_words "$own"
                return
            fi
            case $intent:$n in
                remove:0 | give:1 | keys:0 | nodes:0)
                    local parties
                    parties=$(_continuum_party_parties)
                    [[ $intent == give ]] && parties+=' {party}'
                    _continuum_party_words "$parties"
                    ;;
                keys:*) _continuum_party_files ;;
                revision:0) _continuum_party_words "$(_continuum_party_features)" ;;
                proposal:0) _continuum_party_words "$(_continuum_party_proposals)" ;;
            esac
            ;;
        vote)
            if [[ $cur == -* ]]; then
                _continuum_party_words '--through --wait'
            elif ((n == 0)); then
                _continuum_party_words "$(_continuum_party_proposals)"
            elif ((n == 1)); then
                _continuum_party_words 'yes no'
            fi
            ;;
        show)
            ((n == 0)) && _continuum_party_words "$(_continuum_party_proposals)"
            ;;
        keys | nodes)
            if [[ $cur == -* ]]; then
                _continuum_party_words '--list --add --remove --set --force'
            fi
            ;;
        write | values | get)
            if [[ $cur == -* ]]; then
                [[ $command == values ]] && _continuum_party_words '--json'
                [[ $command == get ]] && _continuum_party_words '-r --raw'
            elif ((n == 0)); then
                _continuum_party_words "$(_continuum_party_places)"
            fi
            ;;
        init)
            [[ $cur == -* ]] && _continuum_party_words '--root'
            ;;
        status)
            [[ $cur == -* ]] && _continuum_party_words '--json'
            ;;
        next)
            [[ $cur == -* ]] && _continuum_party_words '--revision'
            ;;
        found)
            [[ $cur == -* ]] && _continuum_party_words '--nodes --rule --revision'
            ;;
        root | check | twice)
            _continuum_party_files
            ;;
        help)
            ((n == 0)) && _continuum_party_words "$commands"
            ;;
    esac
} &&
    complete -F _continuum_party continuum-party

# ex: filetype=sh