Add git clone support to sandbox create

Partially address #13 by adding the `--git <repo>` option to `sandbox
create`. This enables the quick cloning of a remote repository instead
of doing the tedious dance of creating a sandbox, cloning the remote
repo, deleting the sandbox repo them moving the `.git` directory before
doing a `git reset --hard HEAD`.

Additionally, improve the error messages for all `sandbox` commands,
clean up the completions a little, and use a consistent style in the
scripts.
This commit is contained in:
Kenneth Benzie 2019-08-29 00:56:26 +01:00
parent 8997d9cae0
commit a2509a88b5
2 changed files with 112 additions and 84 deletions

View File

@ -1,46 +1,42 @@
#compdef sandbox #compdef sandbox
__get_sandboxes() {
/bin/ls $SANDBOX_ROOT 2> /dev/null
}
__sandboxes() { __sandboxes() {
local -a sandboxes local -a sandboxes
sandboxes=(${(fo)"$(__get_sandboxes)"}) sandboxes=(${(fo)"$(ls $SANDBOX_ROOT 2> /dev/null)"})
_describe 'in' sandboxes _describe 'in' sandboxes
} }
_sandbox_cmds() {
local commands; commands=(
'create:Create a new sandbox'
'rename:Rename an existing sandbox'
'destroy:Destroy an existing sandbox'
'list:Show all existing sandboxes'
'enable:Enable an existing sandbox'
'disable:Disable the current sandbox'
)
_describe -t commands 'sandbox command' commands "$@"
}
_sandbox() { _sandbox() {
local context curcontext="$curcontext" state line local context curcontext="$curcontext" state line
typeset -A opt_args typeset -A opt_args
_arguments -C \ _arguments -C \
'1: :_sandbox_cmds' \ '1: :->cmd' \
'*::arg:->args' '*:: :->args'
case $state in case $state in
(cmd)
local commands; commands=(
'create:Create a new sandbox'
'rename:Rename an existing sandbox'
'destroy:Destroy an existing sandbox'
'list:Show all existing sandboxes'
'enable:Enable an existing sandbox'
'disable:Disable the current sandbox'
)
_describe -t commands 'sandbox command' commands "$@"
;;
(args) (args)
curcontext="${curcontext%:*:*}:sandbox-cmd-$words[1]:" curcontext="${curcontext%:*:*}:sandbox-cmd-$words[1]:"
case $line[1] in case $line[1] in
(create|list|disable) (create)
_arguments -C '--git[repository to clone]: :'
;; ;;
(enable|destroy) (rename|enable|destroy)
_arguments -C '1:: :__sandboxes' _arguments -C '1:: :__sandboxes'
;; ;;
(rename) (list|disable)
_arguments -C '1:: :__sandboxes'
;; ;;
esac esac
esac esac

View File

@ -3,95 +3,127 @@ if [[ "" == $SANDBOX_ROOT ]]; then
fi fi
sandbox() { sandbox() {
local usage="usage: sandbox {create,destroy,enable,disable} [name]" local usage="\
usage: sandbox [-h] {create,rename,destroy,enable,disable,list} ..
sandbox create [--git <repo>] <name>
sandbox rename <old-name> <new-name>
sandbox destroy <name>
sandbox enable <name>
sandbox disable
sandbox list"
if [[ "" == $1 ]]; then error() { print -P "%F{red}error:%f $1" }
echo $usage
return 1
fi
case $1 in local cmd=$1
[[ -z "$cmd" ]] && \
error "missing command\n$usage" && return 1
shift 1
case $cmd in
create) create)
if [[ "" == $2 ]]; then # Parse command arguments.
echo $usage local git=false
return 1 for arg in $@; do
if [ "${arg[1]}" = - ]; then
if [ "$git" = true ]; then
error "invalid --git <repo> $arg\n$usage" && return 1
elif [ "$arg" = --git ]; then
git=true
else
error "invalid option $arg\n$usage" && return 1
fi
else
if [ "$git" = true ]; then
local repo=$arg
git=false
elif [[ -n "$name" ]]; then
error "invalid argument $arg\n$usage" && return 1
else
local name=$arg
fi
fi
done
unset git
[[ -z "$name" ]] && \
error "missing argument <name>\n$usage" && return 1
local sandbox=$SANDBOX_ROOT/$name
[[ -d "$sandbox" ]] && \
error "sandbox already exists $name" && return 1
if [[ -n "$repo" ]]; then
mkdir -p $SANDBOX_ROOT &> /dev/null
git clone $repo $sandbox &> /dev/null
cd $sandbox
else
mkdir -p $sandbox &> /dev/null
cd $sandbox
git init &> /dev/null
fi fi
local sandbox=$SANDBOX_ROOT/$2 echo "SANDBOX_HOME=\$(dirname -- "\$0:a")" >> $sandbox/.enter
if [[ -d $sandbox ]]; then echo "SANDBOX_NAME=$name" >> $sandbox/.enter
echo "Sandbox '$2' already exists"
return 2
fi
mkdir -p $sandbox &> /dev/null echo "unset SANDBOX_NAME" >> $sandbox/.exit
begin=$PWD echo "unset SANDBOX_HOME" >> $sandbox/.exit
cd $sandbox
echo "SANDBOX_HOME=\$(dirname -- "\$0:a")" >> .enter _autoenv_enter $sandbox
echo "SANDBOX_NAME=$2" >> .enter
echo "unset SANDBOX_NAME" >> .exit
echo "unset SANDBOX_HOME" >> .exit
git init &> /dev/null
cd $begin
cd $sandbox
;; ;;
rename) rename)
if [[ "" == $2 || "" == $3 ]]; then local old_name=$1 new_name=$2
echo $usage [[ -z "$old_name" ]] && \
return 1 error "missing argument <old-name>\n$usage" && return 1
fi [[ -z "$new_name" ]] && \
error "missing argument <new-name>\n$usage" && return 1
mv $SANDBOX_ROOT/$2 $SANDBOX_ROOT/$3 local old=$SANDBOX_ROOT/$old_name new=$SANDBOX_ROOT/$new_name
sed -i "" "s/$2/$3/g" $SANDBOX_ROOT/$3/.enter [[ ! -d "$old" ]] && \
error "sandbox does not exist $old_name" && return 1
[[ -d "$new" ]] && \
error "sandbox already exists $new_name" && return 1
[[ "$PWD" = "$old"* ]] && _autoenv_exit $PWD
mv $old $new
sed -i "s/$old_name/$new_name/g" $new/.enter
[[ "$PWD" = "$old"* ]] && cd $new
;; ;;
destroy) destroy)
if [[ "" == $2 ]]; then local name=$1
echo $usage [[ -z "$name" ]] && \
return 1 error "missing argument <name>\n$usage" && return 1
fi
local sandbox=$SANDBOX_ROOT/$2 local sandbox=$SANDBOX_ROOT/$name
if [[ ! -d $sandbox ]]; then [[ ! -d $sandbox ]] && \
echo "Sandbox '$2' does not exist" error "sandbox does not exist $name" && return 1
return 2
fi
cd - [[ "$PWD" = "$sandbox"* ]] && cd ~
if [[ "${SANDBOX_ROOT##$PWD}" = "${SANDBOX_ROOT}" ]]; then
cd $HOME
fi
rm -rf $sandbox rm -rf $sandbox
;; ;;
list) list)
/bin/ls -1 $SANDBOX_ROOT | less -F -K -R -X ls -1 $SANDBOX_ROOT | less -F -K -R -X
;; ;;
enable) enable)
if [[ "" == $2 ]]; then local name=$1
echo $usage [[ -z "$name" ]] && \
return 1 error "missing argument <name>\n$usage" && return 1
fi
local sandbox=$SANDBOX_ROOT/$2 local sandbox=$SANDBOX_ROOT/$name
if [[ ! -d $sandbox ]]; then [[ ! -d $sandbox ]] && \
echo "Sandbox '$2' does not exist" error "sandbox does not exist $name" && return 1
return 2
fi
export SANDBOX_RETURN=$PWD export SANDBOX_RETURN=$PWD
cd $sandbox cd $sandbox
;; ;;
disable) disable)
if [[ -z $SANDBOX_RETURN ]]; then [[ -z "$SANDBOX_RETURN" ]] && \
echo "Sandbox is not currently active" error "sandbox is not currently active" && return 1
return 2
fi
cd $SANDBOX_RETURN cd $SANDBOX_RETURN
unset $SANDBOX_RETURN unset SANDBOX_RETURN
;; ;;
esac esac
} }