Skip to content

Commit

Permalink
feat: properly handle custom aliases and error states for alias usage…
Browse files Browse the repository at this point in the history
… when calling link subcommand. Refs dokku/dokku-redis#64
  • Loading branch information
josegonzalez committed Apr 24, 2018
1 parent 5f1ceff commit 6d3a524
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
23 changes: 16 additions & 7 deletions common-functions
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,29 @@ service_link() {
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXISTING_CONFIG=$(config_all "$APP")
local LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1) || true
local DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
local SERVICE_DNS_HOSTNAME=$(service_dns_hostname "$SERVICE")
local LINKS_FILE="$SERVICE_ROOT/LINKS"
local ALIAS="$PLUGIN_DEFAULT_ALIAS"
local DEFAULT_ALIAS

if [[ -n "$SERVICE_ALIAS" ]]; then
ALIAS="$SERVICE_ALIAS"
ALIAS_IN_USE=$(echo "$EXISTING_CONFIG" | grep "${ALIAS}_URL") || true
[[ -n "$ALIAS_IN_USE" ]] && dokku_log_fail "Specified alias $ALIAS already in use"
else
DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
if [[ -n "$DEFAULT_ALIAS" ]]; then
ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
fi
[[ -z "$ALIAS" ]] && dokku_log_fail "Unable to use default or generated URL alias"
fi

[[ -n $LINK ]] && dokku_log_fail "Already linked as $LINK"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$LINKS_FILE"
echo "$APP" >> "$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"

local ALIAS="$PLUGIN_DEFAULT_ALIAS"
if [[ -n $DEFAULT_ALIAS ]]; then
ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
fi

if declare -f -F add_passed_docker_option > /dev/null; then
# shellcheck disable=SC2034
local passed_phases=(build deploy run)
Expand Down Expand Up @@ -443,7 +451,8 @@ service_parse_args() {
OPTIND=1
while getopts "a:c:C:d:i:I:m:p:q:r:u:" opt; do
case "$opt" in
a) export SERVICE_ALIAS=$OPTARG
a)
SERVICE_ALIAS="${SERVICE_ALIAS^^}"; export SERVICE_ALIAS="${SERVICE_ALIAS%_URL}"
;;
c) export PLUGIN_CONFIG_OPTIONS=$OPTARG
;;
Expand Down
2 changes: 1 addition & 1 deletion subcommands/clone
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ service-clone-cmd() {

service_parse_args "${@:3}"

dokku_log_info2 "Cloning $SERVICE to $NEW_SERVICE"
dokku_log_info2 "Cloning $SERVICE to $NEW_SERVICE @ $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION"
service_create "$NEW_SERVICE" "${@:3}"
dokku_log_info1 "Copying data from $SERVICE to $NEW_SERVICE"
service_export "$SERVICE" | service_import "$NEW_SERVICE" > /dev/null 2>&1 || true
Expand Down
2 changes: 2 additions & 0 deletions subcommands/link
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ service-link-cmd() {
#E ${PLUGIN_SCHEME}2://lolipop:SOME_PASSWORD@dokku-${PLUGIN_COMMAND_PREFIX}-lolipop:${PLUGIN_DATASTORE_PORTS[0]}/lolipop
#A service, service to run command against
#A app, app to run command against
#F -a|--alias "BLUE_DATABASE", an alternative alias to use for linking to an app via environment variable
#F -q|--querystring "pool=5", ampersand delimited querystring arguments to append to the service link
declare desc="link the $PLUGIN_SERVICE service to the app"
local cmd="$PLUGIN_COMMAND_PREFIX:link" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
declare SERVICE="$1" APP="$2" LINK_FLAGS_LIST="${@:3}"
Expand Down

0 comments on commit 6d3a524

Please sign in to comment.