v_list_dns_template 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. # info: listing dns template
  3. #----------------------------------------------------------#
  4. # Variable&Function #
  5. #----------------------------------------------------------#
  6. # Argument defenition
  7. template=$1
  8. format=${2-shell}
  9. # Importing variables
  10. source $VESTA/conf/vars.conf
  11. source $V_FUNC/shared.func
  12. source $V_FUNC/domain.func
  13. #----------------------------------------------------------#
  14. # Verifications #
  15. #----------------------------------------------------------#
  16. # Checking args
  17. check_args '1' "$#" 'template [format]'
  18. # Checking argument format
  19. format_validation 'template'
  20. # Checking template
  21. is_template_valid 'dns'
  22. # Json func
  23. json_list_dns() {
  24. # Print top bracket
  25. echo '{'
  26. # Count fields
  27. fileds_count=$(echo $fields| wc -w )
  28. # Reading file line by line
  29. while read line; do
  30. # New delimeter
  31. IFS=$'\n'
  32. # Assing key=value pair
  33. eval $line
  34. # Closing bracket if there already was output
  35. if [ -n "$data" ]; then
  36. echo -e ' },'
  37. fi
  38. i=1
  39. IFS=' '
  40. for field in $fields; do
  41. eval value=\"$field\"
  42. value=$(echo "$value" | sed -e 's/"/\\"/g' -e "s/%quote%/'/g")
  43. if [ $i -eq 1 ]; then
  44. # Printing parrent
  45. (( ++i))
  46. echo -e "\t\"$value\": {"
  47. else
  48. # Printing child
  49. if [ $i -lt $fileds_count ]; then
  50. (( ++i))
  51. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  52. else
  53. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  54. data=1
  55. fi
  56. fi
  57. done
  58. done < $conf
  59. # Closing bracket if there was output
  60. if [ -n "$data" ]; then
  61. echo -e ' }'
  62. fi
  63. # Printing bottom bracket
  64. echo -e '}'
  65. }
  66. # Shell function
  67. shell_list_dns() {
  68. if [ -z "$nohead" ] ; then
  69. # Print brief info
  70. echo "${fields//$/}"
  71. for a in $fields; do
  72. echo -e "------ \c"
  73. done
  74. echo
  75. fi
  76. # Reading file line by line
  77. while read line ; do
  78. # Assing key=value pair
  79. eval $line
  80. # Print result
  81. eval echo "$fields" | sed -e "s/%quote%/'/g"
  82. done < $conf
  83. }
  84. #----------------------------------------------------------#
  85. # Action #
  86. #----------------------------------------------------------#
  87. # Defining config
  88. conf=$V_DNSTPL/$template.tpl
  89. # Defining fileds to select
  90. fields='$RECORD $TYPE $VALUE'
  91. # Listing domains
  92. case $format in
  93. json) json_list_dns ;;
  94. plain) nohead=1; shell_list_dns ;;
  95. shell) shell_list_dns | column -t ;;
  96. *) check_args '1' '0' 'template [format]';;
  97. esac
  98. #----------------------------------------------------------#
  99. # Vesta #
  100. #----------------------------------------------------------#
  101. exit