summaryrefslogtreecommitdiff
path: root/completion/xdg-app
blob: 949063dc41348cc4a01ef040f0d54dc6b7fa30c1 (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
252
253
254
255
256
257
258
259
260
261
262
[ -z "$BASH_VERSION" ] && return

__contains_word () {
        local w word=$1; shift
        for w in "$@"; do
                [[ $w = "$word" ]] && return
        done
}

_xdg-app() {
        local cur=${COMP_WORDS[COMP_CWORD]}
        local prev=${COMP_WORDS[COMP_CWORD-1]}
        local i verb comps mode
        local remote name
        local dir cmd sdk loc

        local -A VERBS=(
                [ALL]='add-remote delete-remote list-remotes repo-contents install-runtime update-runtime uninstall-runtime list-runtimes install-app update-app uninstall-app list-apps run build-init build build-finish build-export repo-update'
                [MODE]='add-remote delete-remote list-remotes repo-contents install-runtime update-runtime uninstall-runtime list-runtimes install-app update-app uninstall-app list-apps'
                [UNINSTALL]='uninstall-runtime uninstall-app'
                [ARCH]='build-init install-runtime install-app run uninstall-runtime uninstall-app update-runtime update-app'
        )

        local -A OPTS=(
                [GENERAL]='--help --verbose --version'
                [MODE]='--user --system'
                [ARCH]='--arch'
                [LIST_REMOTES]='--show-urls'
                [REPO_CONTENTS]='--show-details --runtimes --apps --update'
                [UNINSTALL]='--keep-ref'
                [RUN]='--command --branch --devel --forbid --runtime'
                [BUILD_INIT]='--arch --var'
                [BUILD]='--runtime --network --x11'
                [BUILD_FINISH]='--command --allow'
                [BUILD_EXPORT]='--subject --body'
                [ARG]='--arch --command --branch --var --allow --forbid --subject --body --runtime'
        )

        if __contains_word "--user" ${COMP_WORDS[*]}; then
                mode=--user
        else
                mode=--system
        fi

        if __contains_word "$prev" ${OPTS[ARG]}; then
                case $prev in
                        --arch)
                                comps='x86_64 i686'
                                ;;
                        --command)
                                comps=$(compgen -A command)
                                ;;
                        --var|--runtime)
                                comps=$(xdg-app $mode list-runtimes)
                                ;;
                        --allow|--forbid)
                                comps='x11 wayland ipc pulseaudio system-dbus session-dbus network host-fs homedir'
                                ;;
                        --branch|--subject|--body)
                                comps=''
                                ;;
                esac
                COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
                return 0
        fi

        for ((i=0; i < COMP_CWORD; i++)); do
                if [[ "${COMP_WORDS[i]}" = -* ]]; then
                        continue
                fi
                if  __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
                        continue
                fi
                if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
                   test -z $verb; then
                        verb=${COMP_WORDS[i]}
                elif [[ $verb = install-* ]]; then
                        if [[ -z $remote ]]; then
                                remote=${COMP_WORDS[i]}
                        elif [[ -z $name ]]; then
                                name=${COMP_WORDS[i]}
                        fi
                elif [[ $verb =~ (update-*|uninstall-*|run) ]]; then
                        if [[ -z $name ]]; then
                                name=${COMP_WORDS[i]}
                        fi
                elif [[ $verb = build-init ]]; then
                        if [[ -z $dir ]]; then
                                dir=${COMP_WORDS[i]}
                        elif [[ -z $sdk ]]; then
                                sdk=${COMP_WORDS[i]}
                        elif [[ -z $name ]]; then
                                name=${COMP_WORDS[i]}
                        fi
                elif [[ $verb = build ]]; then
                        if [[ -z $dir ]]; then
                                dir=${COMP_WORDS[i]}
                        elif [[ -z $cmd ]]; then
                                cmd=${COMP_WORDS[i]}
                        fi
                elif [[ $verb = build-finish ]]; then
                        if [[ -z $dir ]]; then
                                dir=${COMP_WORDS[i]}
                        fi
                elif [[ $verb = build-export ]]; then
                        if [[ -z $loc ]]; then
                                loc=${COMP_WORDS[i]}
                        elif [[ -z $dir ]]; then
                                dir=${COMP_WORDS[i]}
                        elif [[ -z $name ]]; then
                                name=${COMP_WORDS[i]}
                        fi
                elif [[ $verb = repo-update ]]; then
                        if [[ -z $loc ]]; then
                                loc=${COMP_WORDS[i]}
                        fi
                fi
        done

        if [[ -z $verb ]]; then
                comps="${VERBS[*]}"

        elif [[ "$cur" = -* ]]; then
                comps="${OPTS[GENERAL]}"
                if __contains_word "$verb" ${VERBS[MODE]}; then
                        comps="$comps ${OPTS[MODE]}"
                fi
                if [ "$verb" = "list-remotes" ]; then
                        comps="$comps ${OPTS[LIST_REMOTES]}"
                fi
                if __contains_word "$verb" ${VERBS[ARCH]}; then
                        comps="$comps ${OPTS[ARCH]}"
                fi
                if __contains_word "$verb" ${VERBS[UNINSTALL]}; then
                        comps="$comps ${OPTS[UNINSTALL]}"
                fi
                if [ "$verb" = "run" ]; then
                        comps="$comps ${OPTS[RUN]}"
                fi
                if [ "$verb" = "repo-contents" ]; then
                        comps="$comps ${OPTS[REPO_CONTENTS]}"
                fi
                if [ "$verb" = "build-init" ]; then
                        comps="$comps ${OPTS[BUILD_INIT]}"
                fi
                if [ "$verb" = "build" ]; then
                        comps="$comps ${OPTS[BUILD]}"
                fi
                if [ "$verb" = "build-finish" ]; then
                        comps="$comps ${OPTS[BUILD_FINISH]}"
                fi
                if [ "$verb" = "build-export" ]; then
                        comps="$comps ${OPTS[BUILD_EXPORT]}"
                fi

        else
                case "$verb" in
                add-remote|delete-remote|repo-contents)
                        comps=$(xdg-app $mode list-remotes)
                ;;

                install-runtime)
                        if [[ -z $remote ]]; then
                                comps=$(xdg-app $mode list-remotes)
                        elif [[ -z $name ]]; then
                                comps=$(xdg-app $mode repo-contents $remote --runtimes)
                        else
                                comps='' # FIXME: branches
                        fi
                ;;

                list-remotes|list-runtimes|list-apps)
                        comps=''
                        ;;

                update-runtime|uninstall-runtime)
                        if [[ -z $name ]]; then
                                comps=$(xdg-app $mode list-runtimes)
                        else
                                comps='' # FIXME: branches
                        fi
                ;;

                install-app)
                        if [[ -z $remote ]]; then
                                comps=$(xdg-app $mode list-remotes)
                        elif [[ -z $name ]]; then
                                comps=$(xdg-app $mode repo-contents $remote --apps)
                        else
                                comps='' # FIXME: branches
                        fi
                ;;

                update-app|uninstall-app)
                        if [[ -z $name ]]; then
                                comps=$(xdg-app $mode list-apps)
                        else
                                comps='' # FIXME: branches
                        fi
                        ;;

                run)
                        if [[ -z $name ]]; then
                                comps=$(xdg-app $mode list-apps)
                        fi
                        ;;

                build-init)
                        if [[ -z $dir ]]; then
                                comps=''
                                compopt -o dirnames
                        elif [[ -z $sdk ]]; then
                                comps="$(xdg-app list-runtimes) $(xdg-app --user list-runtimes)"
                        elif [[ -z $name ]]; then
                                comps="$(xdg-app list-runtimes) $(xdg-app --user list-runtimes)"
                        else
                                comps='' # FIXME: branches
                        fi
                        ;;

                build)
                        if [[ -z $dir ]]; then
                                comps=''
                                compopt -o dirnames
                        elif [[ -z $cmd ]]; then
                                comps=''
                                compopt -o bashdefault
                        fi
                        ;;

                build-finish)
                        if [[ -z $dir ]]; then
                                comps=''
                                compopt -o dirnames
                        fi
                        ;;

                build-export)
                        if [[ -z $loc ]]; then
                                comps=''
                                compopt -o dirnames
                        elif [[ -z $dir ]]; then
                                comps=''
                                compopt -o dirnames
                        fi
                        ;;

                repo-update)
                        if [[ -z $loc ]]; then
                                comps=''
                                compopt -o dirnames
                        fi
                        ;;

                esac
        fi

        COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
        return 0
}

complete -F _xdg-app xdg-app