Skip to content

Commit

Permalink
Use custom config API provider. Fixes #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
msjyoo committed Mar 17, 2015
1 parent f334450 commit faacfce
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
7 changes: 5 additions & 2 deletions commands
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ set +exo pipefail
# Fail on equation failure
set -e

# Source our custom env config API
source "$(dirname $0)/libconfig"

# Check if name is specified
if [[ $1 == redis:* ]]; then
if [[ -z $2 ]]; then
Expand Down Expand Up @@ -84,7 +87,7 @@ case "$1" in

# Create REDIS_URL env variable for library / connector autoconfiguration
dokku_log_info1 "Creating Redis autoconfiguration environment variable"
dokku config:set $APP REDIS_URL="redis://redis:6379/0"
r_config_set_redis_url

dokku_log_info2 "Redis started:"
dokku_log_verbose "Application: $APP"
Expand Down Expand Up @@ -174,7 +177,7 @@ case "$1" in
fi

# Remove Redis autoconfiguration variable
dokku config:unset $APP REDIS_URL
r_config_unset_redis_url

dokku_log_info2 "Redis container destroyed:"
dokku_log_verbose "Application: $APP"
Expand Down
46 changes: 46 additions & 0 deletions libconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
# Prefix functions with "r_" to prevent conflict with original

ENV_FILE="$DOKKU_ROOT/$APP/ENV"
ENV_FILE_TEMP="$DOKKU_ROOT/$APP/ENV.tmp"

KEY="REDIS_URL"
VALUE="redis://redis:6379/0"

r_config_create() {
[ -f "$ENV_FILE" ] || {
touch "$ENV_FILE"
}
}

r_config_set_redis_url() {
r_config_create
ENV_TEMP=$(cat "${ENV_FILE}")

ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $KEY=/ d")
ENV_TEMP="${ENV_TEMP}\nexport $KEY='$VALUE'"

r_config_write "$ENV_TEMP"
}

r_config_unset_redis_url() {
r_config_create
ENV_TEMP=$(cat "${ENV_FILE}")

ENV_TEMP=$(echo -e "${ENV_TEMP}" | sed "/^export $KEY=/ d")

r_config_write "$ENV_TEMP"
}

r_config_write () {
ENV_TEMP="$1"

echo -e "$ENV_TEMP" | sed '/^$/d' | sort > "$ENV_FILE_TEMP"

if ! cmp -s "$ENV_FILE" "$ENV_FILE_TEMP"; then
cp -f "$ENV_FILE_TEMP" "$ENV_FILE"
chmod 600 "$ENV_FILE"
fi

rm -f "$ENV_FILE_TEMP"
}

0 comments on commit faacfce

Please sign in to comment.