mirror of
https://github.com/serghey-rodin/vesta.git
synced 2026-09-08 16:30:11 +02:00
126 lines
2.7 KiB
Bash
Executable File
126 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# info: list database
|
|
# options: user database [format]
|
|
#
|
|
# The function for obtaining of all database's parameters.
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Variable&Function #
|
|
#----------------------------------------------------------#
|
|
|
|
# Argument defenition
|
|
user=$1
|
|
database=$2
|
|
format=${3-shell}
|
|
|
|
# Importing variables
|
|
source $VESTA/conf/vars.conf
|
|
source $V_FUNC/shared.func
|
|
source $V_FUNC/db.func
|
|
|
|
# Json function
|
|
json_list_db() {
|
|
i=1
|
|
|
|
# Define words number
|
|
last_word=$(echo "$fields" | wc -w)
|
|
|
|
# Reading file line by line
|
|
line=$(grep "DB='$database'" $conf)
|
|
|
|
# Print top bracket
|
|
echo '{'
|
|
|
|
# Parsing key=value
|
|
eval $line
|
|
|
|
# Starting output loop
|
|
for field in $fields; do
|
|
|
|
# Parsing key=value
|
|
eval value=$field
|
|
|
|
# Checking first field
|
|
if [ "$i" -eq 1 ]; then
|
|
echo -e "\t\"$value\": {"
|
|
else
|
|
if [ "$last_word" -eq "$i" ]; then
|
|
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
|
|
else
|
|
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
|
|
fi
|
|
fi
|
|
|
|
# Updating iterator
|
|
(( ++i))
|
|
done
|
|
|
|
# If there was any output
|
|
if [ -n "$value" ]; then
|
|
echo -e "\t}"
|
|
fi
|
|
|
|
# Printing bottom json bracket
|
|
echo -e '}'
|
|
}
|
|
|
|
# Shell list for single database
|
|
shell_list_db() {
|
|
|
|
# Reading file line by line
|
|
line=$(grep "DB='$database'" $conf)
|
|
|
|
# Parsing key=value
|
|
eval $line
|
|
|
|
# Print result line
|
|
for field in $fields; do
|
|
eval key="$field"
|
|
echo "${field//$/}: $key "
|
|
done
|
|
}
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Verifications #
|
|
#----------------------------------------------------------#
|
|
|
|
# Checking args
|
|
check_args '2' "$#" 'user db_name [format]'
|
|
|
|
# Checking argument format
|
|
format_validation 'user' 'database'
|
|
|
|
# Checking user
|
|
is_user_valid
|
|
|
|
# Checking database exist
|
|
is_db_valid
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Action #
|
|
#----------------------------------------------------------#
|
|
|
|
# Defining config
|
|
conf=$V_USERS/$user/db.conf
|
|
|
|
# Defining fileds to select
|
|
fields='$DB $USER $HOST $TYPE $CHARSET $U_DISK $SUSPENDED $DATE'
|
|
|
|
# Listing database
|
|
case $format in
|
|
json) json_list_db ;;
|
|
plain) shell_list_db ;;
|
|
shell) shell_list_db | column -t ;;
|
|
*) check_args '2' '0' 'user database [format]'
|
|
esac
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Vesta #
|
|
#----------------------------------------------------------#
|
|
|
|
exit
|