lxcd/lxcd.completion

66 lines
1.8 KiB
Bash

#!/bin/bash
_lxcd_completion() {
local cur prev words cword
_init_completion || return
local commands="create clone rename enter list delete add stop start restart"
if [ $cword -eq 1 ]; then
COMPREPLY=($(compgen -W "${commands}" -- "$cur"))
return 0
fi
local subcommand="${words[1]}"
# Use sudo for lxc if the user isn't in the lxd group
if id -nG | grep -qw lxd 2>/dev/null; then
local LXC="lxc"
else
local LXC="sudo lxc"
fi
# Fetch lxcd-managed container names
local containers=$($LXC list user.lxcd=true -c n --format csv 2>/dev/null | tr '\n' ' ')
case "${subcommand}" in
create)
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "--nested --sshd --git --certs --aptcache --ssh-client" -- "$cur"))
else
COMPREPLY=($(compgen -W "ubuntu: ubuntu/24.04 ubuntu/22.04 debian/12 debian/11" -- "$cur"))
fi
;;
clone)
if [ $cword -eq 2 ]; then
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
fi
;;
rename|mv)
if [ $cword -eq 2 ]; then
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
fi
;;
enter|delete|stop|start|restart)
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
;;
add)
if [ $cword -eq 2 ]; then
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
elif [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "--nested --sshd --git --certs --aptcache --ssh-client" -- "$cur"))
fi
;;
*)
COMPREPLY=()
;;
esac
}
complete -F _lxcd_completion lxcd