Skip to content

Instantly share code, notes, and snippets.

@hchbaw
Created February 12, 2011 00:40
Show Gist options
  • Save hchbaw/823343 to your computer and use it in GitHub Desktop.
Save hchbaw/823343 to your computer and use it in GitHub Desktop.
#compdef btrfs
_btrfs () {
_btrfs-help-v0.19 () {
echo "\
Usage:
btrfs subvolume snapshot <source> [<dest>/]<name>
Create a writable snapshot of the subvolume <source> with
the name <name> in the <dest> directory.
btrfs subvolume delete <subvolume>
Delete the subvolume <subvolume>.
btrfs subvolume create [<dest>/]<name>
Create a subvolume in <dest> (or the current directory if
not passed).
btrfs subvolume list <path>
List the snapshot/subvolume of a filesystem.
btrfs subvolume find-new <path> <last_gen>
List the recently modified files in a filesystem.
btrfs filesystem defragment [-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]
Defragment a file or a directory.
btrfs subvolume set-default <id> <path>
Set the subvolume of the filesystem <path> which will be mounted
as default.
btrfs filesystem sync <path>
Force a sync on the filesystem <path>.
btrfs filesystem resize [+/-]<newsize>[gkm]|max <filesystem>
Resize the file system. If 'max' is passed, the filesystem
will occupe all available space on the device.
btrfs filesystem show [<uuid>|<label>]
Show the info of a btrfs filesystem. If no <uuid> or <label>
is passed, info of all the btrfs filesystem are shown.
btrfs filesystem df <path>
Show space usage information for a mount point
.
btrfs filesystem balance <path>
Balance the chunks across the device.
btrfs device scan [<device> [<device>..]
Scan all device for or the passed device for a btrfs
filesystem.
btrfs device add <dev> [<dev>..] <path>
Add a device to a filesystem.
btrfs device delete <dev> [<dev>..] <path>
Remove a device from a filesystem.
btrfs help|--help|-h
Show the help.
Btrfs Btrfs v0.19
"
}
_btrfs-help-readline-loop () {
local buf="${1}"
local func="${2}"
local sub1="${3-}"
local sub2="${4-}"
local help="${5-}"
local argl="${6-}"
[[ -z "$buf" ]] && {
"$func" "$sub1" "$sub2" "$help" "$argl"; return
} || [[ "$buf" == btrfs[[:space:]##](#b)(*[[:space:]##]*) ]] && {
"$func" "$sub1" "$sub2" "$help" "$argl"
argl="$buf"
sub1="${${match[1]}%% *}"
sub2="${${${match[1]}#*[[:space:]]}%%[[:space:]]*}"
help=
} || {
help+=" $buf"
}
local buf2; read buf2; (( $? )) && return || {
"$0" "${${buf2#[[:space:]]##}%[[:space:]]##}" \
"$func" "$sub1" "$sub2" "$help" "$argl"
return $?
}
}
_btrfs-help-collect () {
[[ (-n "$1" || -n "$2" || -n "$3" || -n "$4") ]] || return
local sub1="$1"
local sub2="$2"
local help="$3"
local argl="$4"
local tmp="${btrfs_commands[$sub1]-}"
btrfs_commands+=($sub1 "$tmp $sub2")
btrfs_command_helps+=("$sub1$sub2" "$help")
btrfs_command_arglists+=("$sub1$sub2" "$argl")
}
() {
typeset -gA btrfs_commands; btrfs_commands=()
typeset -gA btrfs_command_helps; btrfs_command_helps=()
typeset -gA btrfs_command_arglists; btrfs_command_arglists=()
emulate -L zsh; setopt extendedglob; local -a match mbegin mend
{
local buf; read buf; read buf;
_btrfs-help-readline-loop "$buf" _btrfs-help-collect
} <<< "$(_call_program help _btrfs-help-v0.19)"
}
_btrfs-commands () {
emulate -L zsh; setopt extendedglob noksharrays nokshzerosubscript
local -a commands
local expl
for k in ${(k)btrfs_commands}; do
commands+="$k:${btrfs_commands[$k]}"
done
_describe -t commands 'btrfs commands' commands -V commands
}
_btrfs-subcommands () {
emulate -L zsh; setopt extendedglob noksharrays nokshzerosubscript
local subcommand="$1"
local -a commands
local k
: ${(A)commands::=${=${btrfs_commands[$subcommand]}}}
for i in {1..$#commands}; do
k="${subcommand}${commands[$i]}"
commands[$i]="${commands[$i]}:${btrfs_command_helps[$k]}"
done
_describe -t commands "btrfs $1 commands" commands -V commands
}
_btrfs-subvolume-snapshot () {
if (( CURRENT == 4 )); then
_arguments '*::<source>:_files'
elif (( CURRENT == 5 )); then
_arguments '*::[<dest>/]<name>:_files'
fi
}
_btrfs-subvolume-delete () {
_arguments '*::<subvolume>:_files'
}
_btrfs-subvolume-create () {
_arguments '*::[<dest>/]<name>:_files'
}
_btrfs-subvolume-list () {
_arguments '*::<path>:_files'
}
_btrfs-subvolume-find-new () {
if (( CURRENT == 4 )); then
_arguments '*::<path>:_files'
elif (( CURRENT == 5 )); then
_arguments '*::<last_gen>:'
fi
}
_btrfs-subvolume-set-default () {
if (( CURRENT == 4 )); then
_arguments '*::<id>:'
elif (( CURRENT == 5 )); then
_arguments '*::<path>:_files'
fi
}
_btrfs-filesystem-defragment () {
# http://article.gmane.org/gmane.comp.file-systems.btrfs/8283
_arguments \
'-v[It just prints the file name as it defrags]' \
'-c[Forces compression, even if you are mounted without compression on]' \
'-s[The starting byte you want to defrag in the file]:start (byte):' \
'-l[The number of bytes you want to compress]:len (byte):' \
'-t[If the extent is bigger than this threshold we consider it defragged already]:extent size' \
'-f[Starts the IO right away]' \
'*:<file>|<dir>[<file>|<dir>...]:_files'
}
_btrfs-filesystem-sync () {
_arguments '*::<path>:_files'
}
_btrfs-filesystem-resize () {
if (( CURRENT == 4 )); then
_arguments '*::[-/+]<newsize>[gkm]|max:'
else
_arguments '*::<filesystem>:_files'
fi
}
_btrfs-filesystem-show () {
_arguments '*::[<uuid>|<label>]:'
}
_btrfs-filesystem-df _btrfs-filesystem-balance () {
_arguments '*::<path>:_files'
}
_btrfs-device-scan () {
_arguments "*::[<device>]:_files -g '/dev/**/*(%)'"
}
_btrfs-device-add _btrfs-device-delete () {
_arguments '*::<path>:_files'
}
local curcontext=$curcontext ret=1
() {
emulate -L zsh; setopt extendedglob noksharrays nokshzerosubscript
if (( CURRENT == 2 )); then
_btrfs-commands && ret=0
elif (( CURRENT == 3 )); then
local errmesg=
local -a tmp; : ${(A)tmp::=${(M)${(@k)btrfs_commands}:#${words[2]}*}}
(( $#tmp != 1 )) && {
errmesg="(fail) btrfs command '${words[2]}'"
(( $#tmp != 0 )) && errmesg+=" ambiguous ${(qq)tmp}"
} || {
words[2]="$tmp[1]"
curcontext="${curcontext%:*:*}:btrfs-$words[2]:"
_btrfs-subcommands "${words[2]}" && ret=0
}
elif (( CURRENT >= 4 )); then
local k= errmesg=
local -a tmp; : ${(A)tmp::=${(M)${(@k)btrfs_commands}:#${words[2]}*}}
(( $#tmp != 1 )) && {
errmesg="(fail) btrfs command '${words[2]}' ${(qq)tmp}"
(( $#tmp != 0 )) && errmesg+=" ambiguous ${(qq)tmp}"
} || {
words[2]="$tmp[1]"; k="$tmp[1]"
: ${(A)tmp::=${(M)${(@)${=btrfs_commands[$k]}}:#${words[3]}*}}
}
[[ ! -n "$k" ]] || (( $#tmp != 1 )) && {
[[ -z "$errmesg" ]] && {
errmesg="(fail) btrfs subcommand '${words[3]}'"
(( $#tmp != 0 )) && errmesg+=" ambiguous ${(qq)tmp}"
}
} || {
words[3]="$tmp[1]"
curcontext="${curcontext%:*:*}:btrfs-$words[2]-$words[3]:"
k="${words[2]}${words[3]}"
[[ -n ${btrfs_command_arglists[$k]} ]] && {
local fn=; zstyle -s ":completion:${curcontext}:" arglist-format fn
[[ -n "$fn" ]] && { _message "$($fn "${btrfs_command_arglists[$k]}")" }
}
_call_function ret _btrfs-${words[2]}-${words[3]}
}
[[ -n "$errmesg" ]] && _message "$errmesg"
fi
}
return ret
}
_btrfs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment