db 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. # Query the rvm key-value database for a specific key
  3. # allow system specific overrides + warnings / errors
  4. __rvm_db_system()
  5. {
  6. \typeset __key __message
  7. for __key in "${_system_name}_${_system_version}_$1" "${_system_name}_$1" "$1"
  8. do
  9. if __rvm_db "${__key}_error" __message
  10. then rvm_error "${__message}"
  11. fi
  12. if __rvm_db "${__key}_warn" __message
  13. then rvm_warn "${__message}"
  14. fi
  15. if __rvm_db "${__key}" "$2"
  16. then return 0
  17. fi
  18. done
  19. true # for OSX
  20. }
  21. # Query the rvm key-value database for a specific key
  22. # Allow overrides from user specifications in $rvm_user_path/db
  23. __rvm_db()
  24. {
  25. \typeset value key variable
  26. key="${1:-}"
  27. variable="${2:-}"
  28. value=""
  29. if [[ -f "$rvm_user_path/db" ]]
  30. then value="$( __rvm_db_ "$rvm_user_path/db" "$key" )"
  31. fi
  32. if [[ -z "$value" && -f "$rvm_path/config/db" ]]
  33. then value="$( __rvm_db_ "$rvm_path/config/db" "$key" )"
  34. fi
  35. [[ -n "$value" ]] || return 1
  36. if [[ -n "$variable" ]]
  37. then eval "$variable='$value'"
  38. else echo "$value"
  39. fi
  40. true # for OSX
  41. }
  42. __rvm_db_remove()
  43. {
  44. if
  45. [[ -f "$1" ]]
  46. then
  47. __rvm_sed -e "\#^$2=# d" -e '/^$/d' "$1" > "$1.new"
  48. \command \mv -f "$1.new" "$1"
  49. fi
  50. }
  51. __rvm_db_add()
  52. {
  53. \typeset __dir="${1%/*}"
  54. if [[ -f "${1}" ]]
  55. then __rvm_db_remove "${1}" "${2}"
  56. elif [[ ! -d "${__dir}" ]]
  57. then mkdir -p "${__dir}"
  58. fi
  59. printf "%b=%b\n" "$2" "$3" >> "$1"
  60. }
  61. __rvm_db_get()
  62. {
  63. if [[ -f "$1" ]]
  64. then __rvm_sed -n -e "\#^$2=# { s#^$2=##;; p; }" -e '/^$/d' < "$1"
  65. else echo -n ""
  66. fi
  67. }
  68. __rvm_db_()
  69. {
  70. \typeset __db __key __value
  71. __db="$1"
  72. __key="${2%%\?*}" # remove ?x=y from urls
  73. shift 2
  74. __value="$*"
  75. case "${__value}" in
  76. (unset|delete)
  77. __rvm_db_remove "${__db}" "${__key}"
  78. ;;
  79. ("")
  80. __rvm_db_get "${__db}" "${__key}"
  81. ;;
  82. (*)
  83. __rvm_db_add "${__db}" "${__key}" "${__value}"
  84. ;;
  85. esac
  86. }