vesta/bin/v-list-databases
2016-06-09 16:26:54 +03:00

107 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# info: listing databases
# options: USER [FORMAT]
#
# The function for obtaining the list of all user's databases.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument definition
user=$1
format=${2-shell}
# Includes
source $VESTA/func/main.sh
# JSON list function
json_list() {
IFS=$'\n'
i=1
objects=$(grep "DB=" $USER_DATA/db.conf |wc -l)
echo "{"
while read str; do
eval $str
echo -n ' "'$DB'": {
"DATABASE": "'$DB'",
"DBUSER": "'$DBUSER'",
"HOST": "'$HOST'",
"TYPE": "'$TYPE'",
"CHARSET": "'$CHARSET'",
"U_DISK": "'$U_DISK'",
"SUSPENDED": "'$SUSPENDED'",
"TIME": "'$TIME'",
"DATE": "'$DATE'"
}'
if [ "$i" -lt "$objects" ]; then
echo ','
else
echo
fi
((i++))
done < <(cat $USER_DATA/db.conf)
echo '}'
}
# SHELL list function
shell_list() {
IFS=$'\n'
echo "DATABASE USER HOST TYPE DISK SPND DATE"
echo "-------- ---- ---- ---- ---- ---- ----"
while read str; do
eval $str
echo "$DB $DBUSER $HOST $TYPE $U_DISK $SUSPENDED $DATE"
done < <(cat $USER_DATA/db.conf)
}
# PLAIN list function
plain_list() {
IFS=$'\n'
while read str; do
eval $str
echo -ne "$DB\t$DBUSER\t$HOST\t$TYPE\t$CHARSET\t$U_DISK\t"
echo -e "$SUSPENDED\t$TIME\t$DATE"
done < <(cat $USER_DATA/db.conf)
}
# CSV list function
csv_list() {
IFS=$'\n'
echo "DATABASE,DBUSER,HOST,TYPE,CHARSET,U_DISK,SUSPENDED,TIME,DATE"
while read str; do
eval $str
echo "$DB,$DBUSER,$HOST,$TYPE,$CHARSET,$U_DISK,$SUSPENDED,$TIME,$DATE"
done < <(cat $USER_DATA/db.conf)
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'USER [FORMAT]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Listing data
case $format in
json) json_list ;;
plain) plain_list ;;
csv) csv_list ;;
shell) shell_list |column -t ;;
esac
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit