summaryrefslogtreecommitdiff
path: root/scripts/shell_completions/bash/borg
blob: 1561d2a2a8af926d0093377b2cc46a3261aa8dc1 (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
# Completions for borg
# https://www.borgbackup.org/
# Note:
# Listing archives works on password protected repositories only if $BORG_PASSPHRASE is set.
# Install:
# Copy this file to /usr/share/bash-completion/completions/ or /etc/bash_completion.d/

_borg2()
{
    compopt -o default
    COMPREPLY=()
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD-1]}"
    local prevprev="${COMP_WORDS[COMP_CWORD-2]}"
    local common_opts="-h --help --critical --error --warning --info -v --verbose --debug --debug-topic -p --progress --iec --log-json --lock-wait --bypass-lock --show-version --show-rc --umask --remote-path --upload-ratelimit --upload-buffer --consider-part-files --debug-profile --rsh -r --repo"
    local opts="${common_opts}"

    # Commands
    if [[ ${COMP_CWORD} == 1 ]] ; then
        local borg_commands="benchmark break-lock check compact config create delete diff export-tar extract help import-tar info key list mount prune rcompress rcreate rdelete recreate rename rinfo rlist serve transfer umount with-lock"
        COMPREPLY=( $(compgen -W "${borg_commands}" -- ${cur}) )
        compopt +o default
        return 0
    fi

    case "${prev}" in
        'key')
            COMPREPLY=( $(compgen -W "change-location change-passphrase export import" -- ${cur}) )
            return 0
            ;;
        'benchmark')
            COMPREPLY=( $(compgen -W "cpu crud" -- ${cur}) )
            return 0
            ;;
        'help')
            COMPREPLY=( $(compgen -W "patterns placeholders compression" -- ${cur}) )
            return 0
            ;;
        '--encryption' | '-e')
            local encryption_modes="authenticated authenticated-blake2 keyfile-aes-ocb keyfile-blake2-aes-ocb keyfile-blake2-chacha20-poly1305 keyfile-chacha20-poly1305 none repokey-aes-ocb repokey-blake2-aes-ocb repokey-blake2-chacha20-poly1305 repokey-chacha20-poly1305"
            COMPREPLY=( $(compgen -W "${encryption_modes}" -- ${cur}) )
            return 0
            ;;
        '--files-cache')
            local files_cache_mode="ctime,size,inode mtime,size,inode ctime,size mtime,size rechunk,ctime rechunk,mtime size disabled"
            COMPREPLY=( $(compgen -W "${files_cache_mode}" -- ${cur}) )
            return 0
            ;;
        '--compression' | '-C')
            local compression_methods="none auto lz4 zstd,1 zstd,2 zstd,3 zstd,4 zstd,5 zstd,6 zstd,7 zstd,8 zstd,9 zstd,10 zstd,11 zstd,12 zstd,13 zstd,14 zstd,15 zstd,16 zstd,17 zstd,18 zstd,19 zstd,20 zstd,21 zstd,22 zlib,1 zlib,2 zlib,3 zlib,4 zlib,5 zlib,6 zlib,7 zlib,8 zlib,9 lzma,0 lzma,1 lzma,2 lzma,3 lzma,4 lzma,5 lzma,6 lzma,7 lzma,8 lzma,9"
            COMPREPLY=( $(compgen -W "${compression_methods}" -- ${cur}) )
            return 0
            ;;
        '--sort-by')
            local sort_keys="timestamp name id"
            COMPREPLY=( $(compgen -W "${sort_keys}" -- ${cur}) )
            return 0
            ;;
        '-o')
            # FIXME This list is probably not full, but I tried to pick only those that are relevant to borg mount -o:
            local fuse_options="ac_attr_timeout= allow_damaged_files allow_other allow_root attr_timeout= auto auto_cache auto_unmount default_permissions entry_timeout= gid= group_id= kernel_cache max_read= negative_timeout= noauto noforget remember= remount rootmode= uid= umask= user user_id= versions"
            COMPREPLY=( $(compgen -W "${fuse_options}" -- ${cur}) )
            return 0
            ;;
        '--recompress')
            local recompress_when="if-different always never"
            COMPREPLY=( $(compgen -W "${recompress_when}" -- ${cur}) )
            return 0
            ;;
        '--upgrader')
            local upgraders="From12To20 NoOp"
            COMPREPLY=( $(compgren -W "${upgraders}" -- ${cur}) )
            return 0
            ;;
    esac

    if [[ ${cur} == -* ]] ; then
        case "${COMP_LINE}" in
            *' rcreate '*)
                local opts="--other-repo -e --encryption --append-only --storage-quota --make-parent-dirs --copy-crypt-key ${common_opts}"
                ;;
            *' rlist '*)
                local opts="--consider-checkpoints --short --format --json ${common_opts} -a --match-archives --sort-by --first --last"
                ;;
            *' rinfo '*)
                local opts="--json ${common_opts}"
                ;;
            *' rcompress '*)
                local opts="-C --compression -s --stats -c --checkpoint-interval ${common_opts}"
                ;;
            *' rdelete '*)
                local opts="-n --dry-run --list --force --cache-only --keep-security-info ${common_opts}"
                ;;
            *' create '*)
                local opts="-n --dry-run -s --stats --list --filter --json --no-cache-sync --stdin-name --stdin-user --stdin-group --stdin-mode --content-from-command --paths-from-stdin --paths-from-command --paths-delimiter -e --exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags --exclude-nodump -x --one-file-system --numeric-ids --atime --noctime --nobirthtime --noflags --noacls --noxattrs --sparse --files-cache --read-special --comment --timestamp -c --checkpoint-interval --chunker-params -C --compression ${common_opts}"
                ;;
            *' extract '*)
                local opts="--list -n --dry-run --numeric-ids --noflags --noacls --noxattrs --stdout --sparse -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
                ;;
            *' check '*)
                local opts="--repository-only --archives-only --verify-data --repair --max-duration -a --match-archives --sort-by --first --last ${common_opts}"
                ;;
            # rename
            #   no specific options
            *" list "*)
                local opts="--short --format --json-lines -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
                ;;
            *' diff '*)
                local opts="--numeric-ids --same-chunker-params --sort --json-lines -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
                ;;
            *' delete '*)
                local opts="-n --dry-run --list --consider-checkpoints -s --stats --cache-only --force -c --checkpoint-interval -a --match-archives --sort-by --first --last ${common_opts}"
                ;;
            *' prune '*)
                local opts="-n --dry-run --force -s --stats --list --keep-within --keep-last --keep-secondly --keep-minutely -H --keep-hourly -d --keep-daily -w --keep-weekly -m --keep-monthly -y --keep-yearly -c --checkpoint-interval -a --match-archives ${common_opts}"
                ;;
            *' compact '*)
                local opts="--threshold ${common_opts}"
                ;;
            *' info '*)
                local opts="--json -a --match-archives --sort-by --first --last ${common_opts}"
                ;;
            *' mount '*)
                local opts="--consider-checkpoints -f --foreground -o --numeric-ids -a --match-archives --sort-by --first --last -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
                ;;
            # umount
            #   no specific options
            # key change-passphrase
            #   no specific options
            *' change-location '*)
                local opts="${common_opts} keyfile repokey --keep"
                ;;
            *' export '*)
                local opts="--paper --qr-html ${common_opts}"
                ;;
            *' import '*)
                local opts="--paper ${common_opts}"
                ;;
            *' recreate '*)
                local opts="--list --filter -n dry-run -s stats -e exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags -a --match-archives --sort-by --first --last --target -c --checkpoint-interval --comment --timestamp -C --compression --recompress --chunker-params ${common_opts}"
                ;;
            *' export-tar '*)
                local opts="--tar-filter --list --tar-format -e exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
                ;;
            *' import-tar '*)
                local opts="--tar-filter -s --stats --list --filter --json ${common_opts} --comment --timestamp -c --checkpoint-interval --chunker-params -C --compression"
                ;;
            *' transfer '*)
                local opts="-n --dry-run --other-repo --upgrader ${common_opts} -a --match-archives --sort-by --first --last"
                ;;
            *' serve '*)
                local opts="--restrict-to-path --restrict-to-repository --append-only --storage-quota ${common_opts}"
                ;;
            *' config '*)
                local opts="-c --cache -d --delete --list ${common_opts}"
                ;;
            # with-lock
            #   no specific options
            # break-lock
            #   no specific options
            # benchmark crud
            #   no specific options
        esac

        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

    # Get the repository name if available
    # If there is a space before the "::" it means that no repository name was typed,
    # so probably $BORG_REPO was set and we can still list the archives.
    local repository_name="${COMP_LINE%%::*}"
    repository_name=${repository_name##* }

    # Listing archives.
    # Since "::" is treated as separate word in Bash,
    #   it is $cur when the cursor is right behind it
    #   and $prev if the user has started to type an archive name.
    local typed_word=${cur}
    local -i list_archives=0
    if [[ ${cur} == "::" ]] ; then
        list_archives=1
        typed_word=""
    fi
    if [[ ${prev} == "::" ]] ; then
        list_archives=1
    fi
    # Second archive listing for borg diff
    if [[ ${COMP_LINE} =~ ^.*\ diff\ .*::[^\ ]+\ ${cur}$ ]] ; then
        list_archives=1
    fi
    # Additional archive listing for borg delete
    if [[ ${COMP_LINE} =~ ^.*\ delete\ .*::[^\ ]+.*${cur}$ ]] ; then
        list_archives=1
    fi
    if (( $list_archives )) ; then
        local archives=$(borg2 list --short "${repository_name}" 2>/dev/null)
        COMPREPLY=( $(compgen -W "${archives}" -- "${typed_word}" ) )
        return 0
    fi

    return 0
}

complete -F _borg2 borg2