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:
parent
8997d9cae0
commit
a2509a88b5
@ -1,46 +1,42 @@
|
||||
#compdef sandbox
|
||||
|
||||
__get_sandboxes() {
|
||||
/bin/ls $SANDBOX_ROOT 2> /dev/null
|
||||
}
|
||||
|
||||
__sandboxes() {
|
||||
local -a sandboxes
|
||||
sandboxes=(${(fo)"$(__get_sandboxes)"})
|
||||
sandboxes=(${(fo)"$(ls $SANDBOX_ROOT 2> /dev/null)"})
|
||||
_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() {
|
||||
local context curcontext="$curcontext" state line
|
||||
typeset -A opt_args
|
||||
|
||||
_arguments -C \
|
||||
'1: :_sandbox_cmds' \
|
||||
'*::arg:->args'
|
||||
'1: :->cmd' \
|
||||
'*:: :->args'
|
||||
|
||||
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)
|
||||
curcontext="${curcontext%:*:*}:sandbox-cmd-$words[1]:"
|
||||
case $line[1] in
|
||||
(create|list|disable)
|
||||
(create)
|
||||
_arguments -C '--git[repository to clone]: :'
|
||||
;;
|
||||
(enable|destroy)
|
||||
(rename|enable|destroy)
|
||||
_arguments -C '1:: :__sandboxes'
|
||||
;;
|
||||
(rename)
|
||||
_arguments -C '1:: :__sandboxes'
|
||||
(list|disable)
|
||||
;;
|
||||
esac
|
||||
esac
|
||||
|
@ -3,95 +3,127 @@ if [[ "" == $SANDBOX_ROOT ]]; then
|
||||
fi
|
||||
|
||||
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
|
||||
echo $usage
|
||||
return 1
|
||||
fi
|
||||
error() { print -P "%F{red}error:%f $1" }
|
||||
|
||||
case $1 in
|
||||
local cmd=$1
|
||||
[[ -z "$cmd" ]] && \
|
||||
error "missing command\n$usage" && return 1
|
||||
shift 1
|
||||
|
||||
case $cmd in
|
||||
create)
|
||||
if [[ "" == $2 ]]; then
|
||||
echo $usage
|
||||
return 1
|
||||
# Parse command arguments.
|
||||
local git=false
|
||||
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
|
||||
|
||||
local sandbox=$SANDBOX_ROOT/$2
|
||||
if [[ -d $sandbox ]]; then
|
||||
echo "Sandbox '$2' already exists"
|
||||
return 2
|
||||
fi
|
||||
echo "SANDBOX_HOME=\$(dirname -- "\$0:a")" >> $sandbox/.enter
|
||||
echo "SANDBOX_NAME=$name" >> $sandbox/.enter
|
||||
|
||||
mkdir -p $sandbox &> /dev/null
|
||||
begin=$PWD
|
||||
cd $sandbox
|
||||
echo "unset SANDBOX_NAME" >> $sandbox/.exit
|
||||
echo "unset SANDBOX_HOME" >> $sandbox/.exit
|
||||
|
||||
echo "SANDBOX_HOME=\$(dirname -- "\$0:a")" >> .enter
|
||||
echo "SANDBOX_NAME=$2" >> .enter
|
||||
|
||||
echo "unset SANDBOX_NAME" >> .exit
|
||||
echo "unset SANDBOX_HOME" >> .exit
|
||||
|
||||
git init &> /dev/null
|
||||
|
||||
cd $begin
|
||||
cd $sandbox
|
||||
_autoenv_enter $sandbox
|
||||
;;
|
||||
rename)
|
||||
if [[ "" == $2 || "" == $3 ]]; then
|
||||
echo $usage
|
||||
return 1
|
||||
fi
|
||||
local old_name=$1 new_name=$2
|
||||
[[ -z "$old_name" ]] && \
|
||||
error "missing argument <old-name>\n$usage" && return 1
|
||||
[[ -z "$new_name" ]] && \
|
||||
error "missing argument <new-name>\n$usage" && return 1
|
||||
|
||||
mv $SANDBOX_ROOT/$2 $SANDBOX_ROOT/$3
|
||||
sed -i "" "s/$2/$3/g" $SANDBOX_ROOT/$3/.enter
|
||||
local old=$SANDBOX_ROOT/$old_name new=$SANDBOX_ROOT/$new_name
|
||||
[[ ! -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)
|
||||
if [[ "" == $2 ]]; then
|
||||
echo $usage
|
||||
return 1
|
||||
fi
|
||||
local name=$1
|
||||
[[ -z "$name" ]] && \
|
||||
error "missing argument <name>\n$usage" && return 1
|
||||
|
||||
local sandbox=$SANDBOX_ROOT/$2
|
||||
if [[ ! -d $sandbox ]]; then
|
||||
echo "Sandbox '$2' does not exist"
|
||||
return 2
|
||||
fi
|
||||
local sandbox=$SANDBOX_ROOT/$name
|
||||
[[ ! -d $sandbox ]] && \
|
||||
error "sandbox does not exist $name" && return 1
|
||||
|
||||
cd -
|
||||
if [[ "${SANDBOX_ROOT##$PWD}" = "${SANDBOX_ROOT}" ]]; then
|
||||
cd $HOME
|
||||
fi
|
||||
[[ "$PWD" = "$sandbox"* ]] && cd ~
|
||||
|
||||
rm -rf $sandbox
|
||||
;;
|
||||
list)
|
||||
/bin/ls -1 $SANDBOX_ROOT | less -F -K -R -X
|
||||
ls -1 $SANDBOX_ROOT | less -F -K -R -X
|
||||
;;
|
||||
enable)
|
||||
if [[ "" == $2 ]]; then
|
||||
echo $usage
|
||||
return 1
|
||||
fi
|
||||
local name=$1
|
||||
[[ -z "$name" ]] && \
|
||||
error "missing argument <name>\n$usage" && return 1
|
||||
|
||||
local sandbox=$SANDBOX_ROOT/$2
|
||||
if [[ ! -d $sandbox ]]; then
|
||||
echo "Sandbox '$2' does not exist"
|
||||
return 2
|
||||
fi
|
||||
local sandbox=$SANDBOX_ROOT/$name
|
||||
[[ ! -d $sandbox ]] && \
|
||||
error "sandbox does not exist $name" && return 1
|
||||
|
||||
export SANDBOX_RETURN=$PWD
|
||||
cd $sandbox
|
||||
;;
|
||||
disable)
|
||||
if [[ -z $SANDBOX_RETURN ]]; then
|
||||
echo "Sandbox is not currently active"
|
||||
return 2
|
||||
fi
|
||||
[[ -z "$SANDBOX_RETURN" ]] && \
|
||||
error "sandbox is not currently active" && return 1
|
||||
|
||||
cd $SANDBOX_RETURN
|
||||
unset $SANDBOX_RETURN
|
||||
unset SANDBOX_RETURN
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user