v_list_db_base 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. # info: listing data base
  3. #----------------------------------------------------------#
  4. # Variable&Function #
  5. #----------------------------------------------------------#
  6. # Argument defenition
  7. user=$1
  8. database=$2
  9. format=${3-shell}
  10. # Importing variables
  11. source $VESTA/conf/vars.conf
  12. source $V_FUNC/shared_func.sh
  13. source $V_FUNC/db_func.sh
  14. # Json function
  15. json_list_db() {
  16. i=1
  17. # Define words number
  18. last_word=$(echo "$fields" | wc -w)
  19. # Reading file line by line
  20. line=$(grep "DB='$database'" $conf)
  21. # Print top bracket
  22. echo '{'
  23. # Parsing key=value
  24. for key in $line; do
  25. eval ${key%%=*}=${key#*=}
  26. done
  27. # Starting output loop
  28. for field in $fields; do
  29. # Parsing key=value
  30. eval value=$field
  31. # Checking first field
  32. if [ "$i" -eq 1 ]; then
  33. echo -e "\t\"$value\": {"
  34. else
  35. if [ "$last_word" -eq "$i" ]; then
  36. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  37. else
  38. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  39. fi
  40. fi
  41. # Updating iterator
  42. (( ++i))
  43. done
  44. # If there was any output
  45. if [ -n "$value" ]; then
  46. echo -e "\t}"
  47. fi
  48. # Printing bottom json bracket
  49. echo -e '}'
  50. }
  51. # Shell list for single database
  52. shell_list_db() {
  53. # Reading file line by line
  54. line=$(grep "DB='$database'" $conf)
  55. # Parsing key=value
  56. for key in $line; do
  57. eval ${key%%=*}=${key#*=}
  58. done
  59. # Print result line
  60. for field in $fields; do
  61. eval key="$field"
  62. echo "${field//$/}: $key "
  63. done
  64. }
  65. #----------------------------------------------------------#
  66. # Verifications #
  67. #----------------------------------------------------------#
  68. # Checking args
  69. check_args '2' "$#" 'user db_name [format]'
  70. # Checking argument format
  71. format_validation 'user' 'database'
  72. # Checking user
  73. is_user_valid
  74. # Checking database exist
  75. is_db_valid
  76. #----------------------------------------------------------#
  77. # Action #
  78. #----------------------------------------------------------#
  79. # Defining config
  80. conf=$V_USERS/$user/db.conf
  81. # Defining fileds to select
  82. fields='$DB $USER $HOST $TYPE $U_DISK $SUSPEND $DATE'
  83. # Listing database
  84. case $format in
  85. json) json_list_db ;;
  86. plain) shell_list_db ;;
  87. shell) shell_list_db | column -t ;;
  88. *) check_args '2' '0' 'user database [format]'
  89. esac
  90. #----------------------------------------------------------#
  91. # Vesta #
  92. #----------------------------------------------------------#
  93. # Logging
  94. log_event 'system' "$V_EVENT"
  95. exit