-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpoint.bash_completion
More file actions
151 lines (130 loc) · 4.4 KB
/
checkpoint.bash_completion
File metadata and controls
151 lines (130 loc) · 4.4 KB
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
#!/usr/bin/env bash
# shellcheck disable=SC2207 # COMPREPLY assignment is standard bash-completion pattern
# checkpoint.bash_completion - Bash completion for checkpoint utility
#
# Installation:
# System-wide: sudo cp checkpoint.bash_completion /usr/share/bash-completion/completions/checkpoint
# User-only: cp checkpoint.bash_completion ~/.local/share/bash-completion/completions/checkpoint
#
# Or source directly: source checkpoint.bash_completion
#
# Provides tab completion for all checkpoint options and dynamic
# completion of checkpoint IDs for restore/compare operations.
# _checkpoint_find_backup_dir: Locate backup directory from args or defaults
# Searches command line for -d/--backup-dir, falls back to environment/defaults
# Returns: backup directory path on stdout
_checkpoint_find_backup_dir() {
local i dir=""
# Check command line for explicit --backup-dir or -d
for ((i = 1; i < ${#COMP_WORDS[@]}; i++)); do
case "${COMP_WORDS[i]}" in
--backup-dir|--backup-dir=*|-d)
if [[ "${COMP_WORDS[i]}" == *=* ]]; then
dir="${COMP_WORDS[i]#*=}"
elif ((i + 1 < ${#COMP_WORDS[@]})); then
dir="${COMP_WORDS[i+1]}"
fi
break
;;
esac
done
# Fall back to environment variable
if [[ -z "$dir" ]]; then
dir="${CHECKPOINT_BACKUP_DIR:-}"
fi
# Fall back to default locations based on source directory
if [[ -z "$dir" ]]; then
# Try to find source directory from remaining args
local src_dir=""
for ((i = ${#COMP_WORDS[@]} - 1; i >= 1; i--)); do
if [[ "${COMP_WORDS[i]}" != -* ]] && [[ -d "${COMP_WORDS[i]}" ]]; then
src_dir="${COMP_WORDS[i]}"
break
fi
done
[[ -z "$src_dir" ]] && src_dir="$PWD"
local dir_name
dir_name=$(basename "$src_dir")
# Check default locations
if [[ -d "/var/backups/$dir_name" ]]; then
dir="/var/backups/$dir_name"
elif [[ -d "$HOME/.checkpoint/$dir_name" ]]; then
dir="$HOME/.checkpoint/$dir_name"
elif [[ -d "$src_dir/.gudang" ]]; then
dir="$src_dir/.gudang"
fi
fi
echo "$dir"
}
# _checkpoint_list_ids: List available checkpoint IDs for completion
# Uses backup directory to find checkpoint directories (20* timestamp format)
# Returns: space-separated list of checkpoint IDs
_checkpoint_list_ids() {
local backup_dir
backup_dir=$(_checkpoint_find_backup_dir)
if [[ -n "$backup_dir" ]] && [[ -d "$backup_dir" ]]; then
# List directories matching timestamp pattern (with optional suffix)
local ids=()
while IFS= read -r -d '' dir; do
ids+=("$(basename "$dir")")
done < <(find "$backup_dir" -maxdepth 1 -type d -name "20*" -print0 2>/dev/null | sort -z -r)
echo "${ids[*]}"
fi
}
# _checkpoint_completion: Main completion function for checkpoint command
_checkpoint_completion() {
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# All long options
local long_opts="--backup-dir --suffix --no-hardlink --hardlink --quiet
--verbose --list --exclude --no-sudo --version --help
--keep --age --prune-only
--no-lock --lock-timeout --force-unlock
--restore --from --to --dry-run --diff --compare-with --detailed --files"
# All short options
local short_opts="-d -s -n -q -v -l -x -r -f -t -p -V -h"
# Context-sensitive completion based on previous word
case "$prev" in
# Directory completion
--backup-dir|-d|--to|-t)
COMPREPLY=($(compgen -d -- "$cur"))
return 0
;;
# File/pattern completion
--exclude|-x|--files)
COMPREPLY=($(compgen -f -- "$cur"))
return 0
;;
# Dynamic checkpoint ID completion
--from|-f|--compare-with)
local ids
ids=$(_checkpoint_list_ids)
if [[ -n "$ids" ]]; then
COMPREPLY=($(compgen -W "$ids" -- "$cur"))
fi
return 0
;;
# Free text - no completion
--suffix|-s)
return 0
;;
# Numeric values - no completion
--keep|--age|--lock-timeout)
return 0
;;
esac
# Option completion when current word starts with -
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "$long_opts $short_opts" -- "$cur"))
return 0
fi
# Default: directory completion for source directory argument
COMPREPLY=($(compgen -d -- "$cur"))
return 0
}
# Register completion for checkpoint and chkpoint commands
complete -F _checkpoint_completion checkpoint
complete -F _checkpoint_completion chkpoint
#fin