File: _depthchargectl.bash

package info (click to toggle)
depthcharge-tools 0.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 760 kB
  • sloc: python: 6,286; sh: 650; makefile: 12
file content (243 lines) | stat: -rw-r--r-- 8,550 bytes parent folder | download | duplicates (2)
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
# SPDX-License-Identifier: GPL-2.0-or-later

# depthcharge-tools depthchargectl bash completions
# Copyright (C) 2020-2022 Alper Nebi Yasak <alpernebiyasak@gmail.com>
# See COPYRIGHT and LICENSE files for full copyright information.

_depthchargectl__file() {
    COMPREPLY+=($(compgen -f -- "$cur"))
    compopt -o filenames
    if [ "${#COMPREPLY[@]}" -eq 1 ]; then
        if [ -d "${COMPREPLY[0]}" ]; then
            compopt -o nospace
            COMPREPLY=("${COMPREPLY[0]}/")
        elif [ -f "${COMPREPLY[0]}" ]; then
            compopt +o nospace
        fi
    fi
} 2>/dev/null

_depthchargectl__timestamp() {
    local timestamp="$(date "+%s")"
    COMPREPLY+=($(compgen -W "$timestamp" -- "$cur"))
} 2>/dev/null

_depthchargectl__disk() {
    local disks="$(lsblk -o "PATH" -n -l)"
    COMPREPLY+=($(compgen -W "$disks" -- "$cur"))
} 2>/dev/null

_depthchargectl__root() {
    local root="$(findmnt --fstab -n -o SOURCE "/")"
    COMPREPLY+=($(compgen -W "$root" -- "$cur"))
} 2>/dev/null

_depthchargectl__boot() {
    local boot="$(findmnt --fstab -n -o SOURCE "/boot")"
    COMPREPLY+=($(compgen -W "$boot" -- "$cur"))
} 2>/dev/null

_depthchargectl__cmdline() {
    local cmdline="$(cat /proc/cmdline | sed -e 's/\(cros_secure\|kern_guid\)[^ ]* //g')"
    COMPREPLY+=($(compgen -W "$cmdline" -- "$cur"))
} 2>/dev/null

_depthchargectl__kernel() {
    if command -v _kernel_versions >/dev/null 2>/dev/null; then
        _kernel_versions
    else
        local script="from depthcharge_tools.utils.platform import installed_kernels"
        "$script;kernels = (k.release for k in installed_kernels());"
        "$script;print(*sorted(filter(None, kernels)));"
        COMPREPLY+=($(compgen -W "$(python3 -c "$script")" -- "$cur"))
    fi
} 2>/dev/null

_depthchargectl__boards() {
    # later
    local script="import re"
    script="$script;from depthcharge_tools import boards_ini"
    script="$script;boards = re.findall(\"codename = (.+)\", boards_ini)"
    script="$script;print(*sorted(boards))"
    COMPREPLY+=($(compgen -W "$(python3 -c "$script")" -- "$cur"))
} 2>/dev/null

_depthchargectl() {
    COMPREPLY=()
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD-1]}"
    local global_opts=(-h --help -V --version -v --verbose --tmpdir --root)
    local config_opts=(
        --config --board --images-dir
        --vboot-keyblock --vboot-public-key --vboot-private-key
        --kernel-cmdline --ignore-initramfs
    )
    local cmds=(bless build config check list remove target write)

    case "$prev" in
        --root) _depthchargectl__root; _depthchargectl__disk; return ;;
        --root-mountpoint) _depthchargectl__file; return ;;
        --boot-mountpoint) _depthchargectl__file; return ;;
        --tmpdir) _depthchargectl__file; return ;;
        --config) _depthchargectl__file; return ;;
        --board) _depthchargectl__boards; return ;;
        --images-dir) _depthchargectl__file; return ;;
        --vboot-keyblock) _depthchargectl__file; return ;;
        --vboot-public-key) _depthchargectl__file; return ;;
        --vboot-private-key) _depthchargectl__file; return ;;
        --kernel-cmdline) _depthchargectl__cmdline; return ;;
        --ignore-initramfs) : ;;
        --zimage-initramfs-hack) COMPREPLY+=($(compgen -W "set-init-size pad-vmlinuz none" -- "$cur")) ;;
        --) ;;
        *) ;;
    esac

    local cmd
    for cmd in "${COMP_WORDS[@]}"; do
        case "$cmd" in
            bless)      _depthchargectl_bless; break ;;
            build)      _depthchargectl_build; break ;;
            config)     _depthchargectl_config; break ;;
            check)      _depthchargectl_check; break ;;
            list)       _depthchargectl_list; break ;;
            remove)     _depthchargectl_remove; break ;;
            target)     _depthchargectl_target; break ;;
            write)      _depthchargectl_write; break ;;
            *) cmd="" ;;
        esac
    done

    if [ -z "$cmd" ]; then
        COMPREPLY+=($(compgen -W "${cmds[*]}" -- "$cur"))
        COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
        COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
    fi
}

_depthchargectl_bless() {
    local opts=(--bad --oneshot -i --partno)
    case "$prev" in
        -i|--partno) return ;;
        *) _depthchargectl__disk ;;
    esac
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
}

_depthchargectl_build() {
    local opts=(
        --description --root --compress --timestamp -o --output
        --kernel-release --kernel --initramfs --fdtdir --dtbs
)
    case "$prev" in
        --description)
            if [ -f /etc/os-release ]; then
                local name="$(. /etc/os-release; echo "$NAME")"
                COMPREPLY+=($(compgen -W "$name" -- "$cur"))
            fi
            return
            ;;
        --root) _depthchargectl__root; _depthchargectl__disk; return ;;
        --compress)
            local compress=(none lz4 lzma)
            COMPREPLY+=($(compgen -W "${compress[*]}" -- "$cur"))
            return
            ;;
        --timestamp) _depthchargectl__timestamp; return;;
        -o|--output) _depthchargectl__file; return ;;
        --kernel-release) _depthchargectl__kernel; return ;;
        --kernel) _depthchargectl__file; return ;;
        --initramfs) _depthchargectl__file; return ;;
        --fdtdir) _depthchargectl__file; return ;;
        --dtbs) _depthchargectl__file; return ;;
        *) _depthchargectl__kernel;;
    esac
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
}

_depthchargectl_config() {
    local opts=(--section --default)
    case "$prev" in
        --section) return ;;
        --default) return ;;
        *) ;;
    esac
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
}

_depthchargectl_check() {
    _depthchargectl__file
    COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
}

_depthchargectl_list() {
    local opts=(-a --all-disks -c --count -n --noheadings -o --output)
    local outputs=(A ATTRIBUTE S SUCCESSFUL T TRIES P PRIORITY PATH DISK DISKPATH PARTNO SIZE)
    case "$prev" in
        -o|--output)
            compopt -o nospace
            case "$cur" in
                *,) COMPREPLY+=($(compgen -W "${outputs[*]}" -P "$cur" -- "")) ;;
                *,*) COMPREPLY+=($(compgen -W "${outputs[*]}" -P "${cur%,*}," -- "${cur##*,}")) ;;
                *) COMPREPLY+=($(compgen -W "${outputs[*]}" -- "$cur")) ;;
            esac
            ;;
        *)
            _depthchargectl__disk
            COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
            COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
            COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
            ;;
    esac
}

_depthchargectl_remove() {
    local opts=(-f --force)
    _depthchargectl__file
    _depthchargectl__kernel
    COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
    COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
}

_depthchargectl_target() {
    local opts=(-s --min-size --allow-current -a --all-disks)
    local sizes=(8M 16M 32M 64M 128M 256M 512M)
    case "$prev" in
        -s|--min-size)
            COMPREPLY+=($(compgen -W "${sizes[*]}" -- "$cur"))
            ;;
        *)
            _depthchargectl__disk
            COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
            COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
            COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
            ;;
    esac
}

_depthchargectl_write() {
    local opts=(-f --force -t --target --no-prioritize --allow-current)
    case "$prev" in
        -t|--target)
            _depthchargectl__disk
            ;;
        *)
            _depthchargectl__kernel
            _depthchargectl__file
            COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))
            COMPREPLY+=($(compgen -W "${global_opts[*]}" -- "$cur"))
            COMPREPLY+=($(compgen -W "${config_opts[*]}" -- "$cur"))
            ;;
    esac
}

complete -F _depthchargectl depthchargectl

# vim: filetype=sh