lxcd/lxcd.completion

84 lines
2.4 KiB
Bash

#!/bin/bash
_lxcd_completion() {
local cur prev words cword
_init_completion || return
local commands="create clone rename enter list delete stop start restart"
local init_opts="-f --force -w --workspace -e --edit -p --packages -n --nested -i --image --aptcache --ssh"
local create_opts="--nested --aptcache -i --image -w --workspace -p --packages --ssh"
if [ $cword -eq 1 ]; then
COMPREPLY=($(compgen -W "-v --verbose ${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
init)
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "${init_opts}" -- "$cur"))
fi
;;
create)
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "${create_opts}" -- "$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)
if [ $cword -eq 2 ]; then
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
fi
;;
delete|stop|start|restart)
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "-a --all" -- "$cur"))
else
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
fi
;;
list|ls)
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "-b --brief" -- "$cur"))
else
COMPREPLY=($(compgen -W "ipv4 status,ipv4 image,ipv4 packages,ipv4 opts image,packages,opts ipv4,image,packages" -- "$cur"))
fi
;;
*)
COMPREPLY=()
;;
esac
}
complete -F _lxcd_completion lxcd