310 lines
13 KiB
Python
310 lines
13 KiB
Python
"""Shell completion script generator for Bash and Zsh."""
|
|
|
|
BASH_COMPLETION = """# bash completion for linux-backuper
|
|
# Generated automatically by linux-backuper completion bash
|
|
|
|
_linux_backuper_get_snapshots() {
|
|
local backup_dir="/var/backups/linux-backuper"
|
|
if [[ -d "$backup_dir" ]]; then
|
|
find "$backup_dir" -maxdepth 1 -name "backup_*.tar.gz" -exec basename {} .tar.gz \\; 2>/dev/null
|
|
fi
|
|
}
|
|
|
|
_linux_backuper() {
|
|
local cur prev words cword
|
|
_init_completion || return
|
|
|
|
local commands="snapshot list show diff extract restore prune push verify completion"
|
|
local profiles="quick system full dr"
|
|
|
|
if [[ $cword -eq 1 ]]; then
|
|
COMPREPLY=( $(compgen -W "$commands -h --help -v --version" -- "$cur") )
|
|
return 0
|
|
fi
|
|
|
|
local cmd="${words[1]}"
|
|
|
|
case "$cmd" in
|
|
snapshot)
|
|
case "$prev" in
|
|
--profile)
|
|
COMPREPLY=( $(compgen -W "$profiles" -- "$cur") )
|
|
return 0
|
|
;;
|
|
--compression)
|
|
COMPREPLY=( $(compgen -W "gzip none" -- "$cur") )
|
|
return 0
|
|
;;
|
|
--target-dir|--config|--pid-path|--ssh-key)
|
|
_filedir
|
|
return 0
|
|
;;
|
|
--paths)
|
|
_filedir
|
|
return 0
|
|
;;
|
|
esac
|
|
local snap_opts="--profile --target-dir --paths --compression --no-compression --sanitize --config --pid-path --keep-last --keep-daily --keep-weekly --keep-monthly --push --ssh-key --remote-keep-last --remote-keep-daily --remote-keep-weekly --remote-keep-monthly --json --verbose -h --help"
|
|
COMPREPLY=( $(compgen -W "$snap_opts" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
list)
|
|
case "$prev" in
|
|
--target-dir)
|
|
_filedir -d
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "--target-dir --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
show|verify)
|
|
case "$prev" in
|
|
show|verify)
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots)" -- "$cur") )
|
|
return 0
|
|
;;
|
|
--target-dir)
|
|
_filedir -d
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots) --packages --files --target-dir --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
diff)
|
|
case "$prev" in
|
|
--target-dir|--target-root)
|
|
_filedir -d
|
|
return 0
|
|
;;
|
|
diff)
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots)" -- "$cur") )
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots) --target-dir --target-root --unified -u --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
extract)
|
|
case "$prev" in
|
|
extract)
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots)" -- "$cur") )
|
|
return 0
|
|
;;
|
|
--file)
|
|
# File inside snapshot
|
|
return 0
|
|
;;
|
|
--out|--target-dir)
|
|
_filedir
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots) --file --out --target-dir --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
restore)
|
|
case "$prev" in
|
|
restore)
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots)" -- "$cur") )
|
|
return 0
|
|
;;
|
|
--target-root|--target-dir)
|
|
_filedir -d
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots) --dry-run --force --target-root --include-fstab --target-dir --pid-path --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
prune)
|
|
case "$prev" in
|
|
--target-dir)
|
|
_filedir -d
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "--keep-last --keep-daily --keep-weekly --keep-monthly --dry-run --force --target-dir --pid-path --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
push)
|
|
case "$prev" in
|
|
push)
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots)" -- "$cur") )
|
|
return 0
|
|
;;
|
|
--ssh-key|--target-dir|--config)
|
|
_filedir
|
|
return 0
|
|
;;
|
|
esac
|
|
COMPREPLY=( $(compgen -W "$(_linux_backuper_get_snapshots) --target --ssh-key --remote-keep-last --remote-keep-daily --remote-keep-weekly --remote-keep-monthly --target-dir --config --json --verbose -h --help" -- "$cur") )
|
|
return 0
|
|
;;
|
|
|
|
completion)
|
|
COMPREPLY=( $(compgen -W "bash zsh" -- "$cur") )
|
|
return 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
complete -F _linux_backuper linux-backuper
|
|
"""
|
|
|
|
ZSH_COMPLETION = """#compdef linux-backuper
|
|
# zsh completion for linux-backuper
|
|
# Generated automatically by linux-backuper completion zsh
|
|
|
|
_linux_backuper_snapshots() {
|
|
local backup_dir="/var/backups/linux-backuper"
|
|
if [[ -d "$backup_dir" ]]; then
|
|
local -a snaps
|
|
snaps=(${(@f)$(find "$backup_dir" -maxdepth 1 -name "backup_*.tar.gz" -exec basename {} .tar.gz \\; 2>/dev/null)})
|
|
_describe -t snapshots 'snapshot' snaps
|
|
fi
|
|
}
|
|
|
|
_linux_backuper() {
|
|
local -a commands
|
|
commands=(
|
|
'snapshot:Capture a new system state snapshot'
|
|
'list:List existing local snapshots'
|
|
'show:Display details and metadata of a snapshot'
|
|
'diff:Compare live system against snapshot, or two snapshots'
|
|
'extract:Selectively extract a single file from a snapshot'
|
|
'restore:Restore system configuration from a snapshot'
|
|
'prune:Prune old snapshots based on retention policies'
|
|
'push:Push an existing snapshot to remote storage'
|
|
'verify:Verify snapshot cryptographic integrity'
|
|
'completion:Generate shell autocompletion script'
|
|
)
|
|
|
|
_arguments -C \\
|
|
'(-v --version)'{-v,--version}'[Show version]' \\
|
|
'(-h --help)'{-h,--help}'[Show help message]' \\
|
|
'--json[Output machine-readable JSON to stdout]' \\
|
|
'--verbose[Enable detailed diagnostic logging]' \\
|
|
'1:command:->command' \\
|
|
'*::arg:->args'
|
|
|
|
case "$state" in
|
|
command)
|
|
_describe -t commands 'linux-backuper commands' commands
|
|
;;
|
|
args)
|
|
case "$words[1]" in
|
|
snapshot)
|
|
_arguments \\
|
|
'--profile[Backup profile]:profile:(quick system full dr)' \\
|
|
'--target-dir[Destination directory]:directory:_files -/' \\
|
|
'--paths[Directories or files to include]:file:_files' \\
|
|
'--compression[Compression algorithm]:compression:(gzip none)' \\
|
|
'--no-compression[Store plain uncompressed tar archive]' \\
|
|
'--sanitize[Redact secrets and private keys for diagnostic export]' \\
|
|
'--config[Path to configuration file]:file:_files' \\
|
|
'--pid-path[Path to lockfile]:file:_files' \\
|
|
'--keep-last[Keep N most recent snapshots]:count:' \\
|
|
'--keep-daily[Keep latest snapshot for D days]:days:' \\
|
|
'--keep-weekly[Keep latest snapshot for W weeks]:weeks:' \\
|
|
'--keep-monthly[Keep latest snapshot for M months]:months:' \\
|
|
'--push[Transfer snapshot to remote destination]:target:' \\
|
|
'--ssh-key[SSH private key]:key:_files' \\
|
|
'--remote-keep-last[Remote retention keep last]:count:' \\
|
|
'--remote-keep-daily[Remote retention keep daily]:days:' \\
|
|
'--remote-keep-weekly[Remote retention keep weekly]:weeks:' \\
|
|
'--remote-keep-monthly[Remote retention keep monthly]:months:'
|
|
;;
|
|
list)
|
|
_arguments \\
|
|
'--target-dir[Directory to scan]:directory:_files -/'
|
|
;;
|
|
show)
|
|
_arguments \\
|
|
'1:snapshot:_linux_backuper_snapshots' \\
|
|
'--packages[List all recorded installed packages]' \\
|
|
'--files[List all archived file paths]' \\
|
|
'--target-dir[Directory containing snapshot]:directory:_files -/'
|
|
;;
|
|
diff)
|
|
_arguments \\
|
|
'1:snapshot 1:_linux_backuper_snapshots' \\
|
|
'2:snapshot 2 (optional):_linux_backuper_snapshots' \\
|
|
'--target-dir[Directory containing snapshots]:directory:_files -/' \\
|
|
'--target-root[Filesystem root to compare against]:directory:_files -/' \\
|
|
'(-u --unified)'{-u,--unified}'[Display unified diff for modified text files]'
|
|
;;
|
|
extract)
|
|
_arguments \\
|
|
'1:snapshot:_linux_backuper_snapshots' \\
|
|
'--file[File path inside snapshot to extract]:path:' \\
|
|
'--out[Destination file or directory]:output:_files' \\
|
|
'--target-dir[Directory containing snapshot]:directory:_files -/'
|
|
;;
|
|
restore)
|
|
_arguments \\
|
|
'1:snapshot:_linux_backuper_snapshots' \\
|
|
'--dry-run[Display execution plan without modifying filesystem]' \\
|
|
'--force[Execute live restoration]' \\
|
|
'--target-root[Target root directory]:directory:_files -/' \\
|
|
'--include-fstab[Allow restoring /etc/fstab]' \\
|
|
'--target-dir[Directory containing snapshot]:directory:_files -/' \\
|
|
'--pid-path[Path to lockfile]:file:_files'
|
|
;;
|
|
prune)
|
|
_arguments \\
|
|
'--keep-last[Keep N most recent snapshots]:count:' \\
|
|
'--keep-daily[Keep D daily snapshots]:days:' \\
|
|
'--keep-weekly[Keep W weekly snapshots]:weeks:' \\
|
|
'--keep-monthly[Keep M monthly snapshots]:months:' \\
|
|
'--dry-run[Simulate pruning plan]' \\
|
|
'--force[Confirm deletion of pruned snapshots]' \\
|
|
'--target-dir[Directory containing snapshots]:directory:_files -/'
|
|
;;
|
|
push)
|
|
_arguments \\
|
|
'1:snapshot:_linux_backuper_snapshots' \\
|
|
'--target[Remote destination (e.g. user@host:/path or alias)]:target:' \\
|
|
'--ssh-key[SSH private key]:key:_files' \\
|
|
'--remote-keep-last[Remote retention keep last]:count:' \\
|
|
'--remote-keep-daily[Remote retention keep daily]:days:' \\
|
|
'--remote-keep-weekly[Remote retention keep weekly]:weeks:' \\
|
|
'--remote-keep-monthly[Remote retention keep monthly]:months:' \\
|
|
'--target-dir[Directory containing snapshot]:directory:_files -/' \\
|
|
'--config[Config path]:file:_files'
|
|
;;
|
|
verify)
|
|
_arguments \\
|
|
'1:snapshot:_linux_backuper_snapshots' \\
|
|
'--target-dir[Directory containing snapshot]:directory:_files -/'
|
|
;;
|
|
completion)
|
|
_arguments '1:shell:(bash zsh)'
|
|
;;
|
|
esac
|
|
;;
|
|
esac
|
|
}
|
|
|
|
_linux_backuper "$@"
|
|
"""
|
|
|
|
|
|
def generate_completion(shell: str) -> str:
|
|
"""Return completion script for specified shell."""
|
|
shell = shell.lower().strip()
|
|
if shell == "bash":
|
|
return BASH_COMPLETION
|
|
elif shell == "zsh":
|
|
return ZSH_COMPLETION
|
|
else:
|
|
raise ValueError(f"Unsupported shell: '{shell}'. Supported shells: bash, zsh")
|