[lxc-devel] ZSH completion script for LXC tools

Vincent Rischmann zecmerquise at gmail.com
Tue Apr 13 21:01:13 UTC 2010


Hi,

I created a ZSH completion script for most of LXC tools (using LXC 0.6.5).
I am not a zsh guru, so maybe I did it wrong.

Anyway, at first I only wanted to complete the name of the containers based on 
the content of /var/lib/lxc, but then I decided to write the whole thing.

It should be installed in /usr/share/zsh/site-functions for system-wide 
installation. Alternatively if you want to add it for only your user, you will 
have to modify the "fpath" variable before loading compinit in your .zshrc.
Eg:
fpath=($fpath $HOME/.zsh.comp.d)   # _lxc is in the .zsh.comp.d directory
autoload -U compinit
compinit

I hope this is useful.

Vincent.
-------------- next part --------------
#compdef lxc-start lxc-stop lxc-console lxc-restart lxc-create lxc-destroy lxc-monitor lxc-cgroup lxc-checkpoint lxc-execute lxc-freeze lxc-unfreeze lxc-info lxc-netstat lxc-ps lxc-wait

_lxc_containers() {
	local -a containers
	containers=( $(/bin/ls /var/lib/lxc) )
	compadd "$@" -a containers
}


_lxc_common_opts=(
	'(-o --logfile)'{-o,--logfile=}'[Output log to FILE instead of stderr]:logfile:_files'
	'(-l --logpriority)'{-l,--logpriority=}'[Set log priority to LEVEL]:level:'
	'(-q --quiet)'{-q,--quiet}'[Dont produce any output]'
	'(-h --help)'{-h,--help}'[Help list]'
	'--usage[Give a short usage message]'
)

_lxc_name_opt=(
	'(-n --name)'{-n,--name=}'[Name of the container]:name:_lxc_containers'
)

_lxc_start_opts=(
	'(-d --daemon)'{-d,--daemon}'[Daemonize the container]'
	'(-f --rcfile)'{-f,--rcfile=}'[Load configuration file FILE]:config:_files'
	'(-s --define)'{-s,--define}'[Assign VAL to configuration variable KEY]'
)

_lxc_console_opts=(
	'(-t --tty)'{-t,--tty=}'[Console TTY number]'
	'(-e --escape)'{-e,--escape=}'[Prefix for escape command]'
)

_lxc_restart_opts=(
	'(-p --pause)'{-p,--pause}'[Do not release the container after the restart]'
	'(-d --directory)'{-d,--directory=}'[Name of statefile]:name:_files'
	'(-f --rcfile)'{-f,--rcfile=}'[Load configuration file FILE]:config:_files'
	'(-s --define)'{-s,--define}'[Assign VAL to configuration variable KEY]'
)

_lxc_create_opts=(
	'(-n --name)'{-n,--name=}'[Name of the container]:name'
	'(-f --config)'{-f,--config=}'[Load configuration file FILE]:config:_files'
	'(-t --template)'{-t,--template=}'[LXC template script]:tpl:_files'
	'(-h --help)'{-h,--help}'[Help list]'
)

_lxc_checkpoint_opts=(
	'(-k --kill)'{-k,--kill}'[Stop the container after the checkpoint]'
	'(-p --pause)'{-p,--pause}'[Dont unfreeze the container after the checkpoint]'
	'(-d --directory)'{-d,--directory=}'[Where to store the statefile]:statefile:_files'
)

_lxc_execute_opts=(
	'(-f --rcfile)'{-f,--rcfile=}'[Load configuration file FILE]:config:_files'
	'(-s --define)'{-s,--define}'[Assign VAL to configuration variable KEY]'
)

_lxc_ps_opts=(
	'--help[Display the help]'
	'--usage[Display the command usage]'
	'--name[Display processes related to given containers. Containers are identified by a comma separated list of their names.]'
	'--lxc[Display processes related to all LXC containers]'
)

_lxc_wait_opts=(
	'(-s --state)'{-s,--state=}'[ORed states to wait for: STOPPED, STARTING, RUNNING, STOPPING, ABORTING, FREEZING, FROZEN]:state'
)

# Dispatcher

_lxc() {
	case "$service" in
		lxc-start)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_start_opts[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-stop)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_common_opts[@]" \
			;;
		lxc-console)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_console_opts[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-restart)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_restart_opts[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-create)
			_arguments -s : "$_lxc_create_opts[@]"
			;;
		lxc-destroy)
			_arguments -s : "$_lxc_name_opt[@]"
			;;
		lxc-monitor)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-cgroup)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-checkpoint)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_checkpoint_opts[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-execute)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_execute_opts[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-freeze)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-unfreeze)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-info)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_common_opts[@]"
			;;
		lxc-netstat)
			_arguments -s : "$_lxc_name_opt[@]"
			;;
		lxc-ps)
			_arguments -s : "$_lxc_ps_opts[@]"
			;;
		lxc-wait)
			_arguments -s : \
				"$_lxc_name_opt[@]" \
				"$_lxc_wait_opts[@]"
			;;
		*)
			return 1
	esac
}

_lxc "$@"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20100413/5b888832/attachment.pgp>


More information about the lxc-devel mailing list