Refactor evogalaxy
* extract functions * use local variables
This commit is contained in:
parent
5bd1c56275
commit
8672d6c658
|
@ -3,8 +3,40 @@
|
|||
# PoC to handle Evolix ansible-roles
|
||||
# WARN: we use -b unstable for now
|
||||
|
||||
subcommand=$1
|
||||
option=$2
|
||||
readonly PROGNAME=$(basename $0)
|
||||
readonly PROGDIR=$(readlink -m $(dirname $0))
|
||||
readonly ARGS="$@"
|
||||
|
||||
main(){
|
||||
local subcommand=$1
|
||||
|
||||
case $subcommand in
|
||||
"help")
|
||||
sub_help
|
||||
;;
|
||||
|
||||
"list")
|
||||
sub_list
|
||||
;;
|
||||
|
||||
"install")
|
||||
local role=$2
|
||||
|
||||
sub_install $role
|
||||
;;
|
||||
|
||||
"update")
|
||||
local role=$2
|
||||
|
||||
sub_update $role
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "error: unknwon command"
|
||||
sub_help
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
sub_help(){
|
||||
echo ""
|
||||
|
@ -17,33 +49,73 @@ sub_help(){
|
|||
echo ""
|
||||
}
|
||||
|
||||
case $subcommand in
|
||||
"help")
|
||||
sub_help
|
||||
;;
|
||||
"list")
|
||||
tmpdir=`mktemp -d`
|
||||
git clone -q -b unstable https://forge.evolix.org/ansible-roles.git $tmpdir
|
||||
ls -1 $tmpdir | egrep -v '(.md|.yml|Vagrant)'
|
||||
;;
|
||||
"install")
|
||||
test -d jessie/$option && echo "error: already installed" && exit
|
||||
tmpdir=`mktemp -d`
|
||||
git clone -q -b unstable https://forge.evolix.org/ansible-roles.git $tmpdir
|
||||
mkdir -p jessie
|
||||
mv $tmpdir/$option jessie/
|
||||
;;
|
||||
"update")
|
||||
! test -d jessie/$option && echo "error: not installed" && exit
|
||||
tmpdir=`mktemp -d`
|
||||
git clone -q -b unstable https://forge.evolix.org/ansible-roles.git $tmpdir
|
||||
mv jessie/$option $tmpdir/$option-old
|
||||
mv $tmpdir/$option jessie/
|
||||
;;
|
||||
*)
|
||||
echo "error: unknwon command"
|
||||
sub_help
|
||||
;;
|
||||
esac
|
||||
sub_list(){
|
||||
local clone_dir=$(clone_git_repository)
|
||||
|
||||
list_roles $clone_dir
|
||||
}
|
||||
|
||||
sub_install(){
|
||||
local role=$1
|
||||
|
||||
test -d jessie/$role \
|
||||
&& exit_role_already_installed $role
|
||||
|
||||
local clone_dir=$(clone_git_repository)
|
||||
|
||||
test -d $clone_dir/$role \
|
||||
|| exit_role_unknown $role
|
||||
|
||||
mkdir -p jessie \
|
||||
&& mv $clone_dir/$role jessie/$role
|
||||
}
|
||||
|
||||
sub_update(){
|
||||
local role=$1
|
||||
|
||||
test -d jessie/$role \
|
||||
|| exit_role_not_installed $role
|
||||
|
||||
local clone_dir=$(clone_git_repository)
|
||||
|
||||
mv jessie/$role $clone_dir/$role-old \
|
||||
&& mv $clone_dir/$role jessie/$role
|
||||
}
|
||||
|
||||
clone_git_repository() {
|
||||
local clone_dir=$(mktemp -d)
|
||||
local git_repo=https://forge.evolix.org/ansible-roles.git
|
||||
local git_branch=unstable
|
||||
|
||||
git clone -q -b $git_branch $git_repo $clone_dir
|
||||
|
||||
echo $clone_dir
|
||||
}
|
||||
|
||||
list_roles(){
|
||||
local roles_dir=$1
|
||||
|
||||
ls -1 $roles_dir \
|
||||
| egrep -v '(.md|.yml|Vagrant)'
|
||||
}
|
||||
|
||||
exit_role_already_installed(){
|
||||
local role=$1
|
||||
|
||||
echo "error: role '${role}' already installed"
|
||||
exit 1
|
||||
}
|
||||
exit_role_not_installed(){
|
||||
local role=$1
|
||||
|
||||
echo "error: role '${role}' not installed"
|
||||
exit 2
|
||||
}
|
||||
exit_role_unknown(){
|
||||
local role=$1
|
||||
|
||||
echo "error: unknown role '${role}'"
|
||||
exit 3
|
||||
}
|
||||
|
||||
main $ARGS
|
||||
|
|
Loading…
Reference in a new issue