Skip to content

Instantly share code, notes, and snippets.

@norrs
Created August 20, 2013 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norrs/6285918 to your computer and use it in GitHub Desktop.
Save norrs/6285918 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
source "$rvm_scripts_path/functions/support"
usage()
{
printf "%b" "
ERROR:
$1
Usage:
db database_file {{key}} {{value}} # set
db database_file {{key}} # get
db database_file {{key}} unset # unset
" >&2
exit 1
}
database_file="${1:-}"
if [[ -n "$database_file" ]]
then shift
else usage "Database file not given."
fi
key="${1:-}"
if [[ -n "$key" ]]
then shift
else usage "Key not given"
fi
value="$*"
escaped_key="$( __rvm_sed -e 's#\\#\\#g' -e 's#/#\\/#g' -e 's#\.#\.#g' <<< "$key" )"
[[ -f "$database_file" ]] ||
{
directory=$(dirname "$database_file")
[[ -d "$directory" ]] || mkdir -p "$directory"
touch "$database_file"
}
db_remove()
{
__rvm_sed -e "s#^$escaped_key=.*\$##" -e '/^$/d' "$database_file" > "$database_file.new"
\mv -f "$database_file.new" "$database_file"
}
case "$value" in
(unset|delete)
db_remove
;;
("") # get
__rvm_awk -F= '/^'"$escaped_key"'=/{print}' "$database_file" | __rvm_sed -e "s#^$escaped_key=##" -e '/^$/d'
;;
(*) # set
db_remove
printf "%b=%b\n" "$escaped_key" "$value" >> "$database_file"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment