From 6bab5bad28fd4838db75d8b16c2d3898c82c6a40 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 30 May 2019 13:15:10 -0400 Subject: [PATCH] feat: re-allow dashes in names This PR allows dashes in service names, while still sanitizing them before they are used as database names. If the datastore is pre-existing, the datatabase name is assumed to be the same as the service name, and returned appropriately. --- common-functions | 24 +++++++++++++++++++----- functions | 2 ++ tests/service_create.bats | 12 +++++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/common-functions b/common-functions index e1aa92f..7948099 100755 --- a/common-functions +++ b/common-functions @@ -19,10 +19,14 @@ get_container_ip() { get_database_name() { declare desc="Retrieves a sanitized database name" - declare DATABASE="$1" - # some datastores do not like special characters in database names - # so we need to normalize them out - echo "$DATABASE" | tr .- _ + declare SERVICE="$1" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + + if [[ ! -f "$SERVICE_ROOT/DATABASE_NAME" ]]; then + echo "$SERVICE" > "$SERVICE_ROOT/DATABASE_NAME" + fi + + cat "$SERVICE_ROOT/DATABASE_NAME" } get_random_ports() { @@ -89,7 +93,7 @@ is_valid_service_name() { declare SERVICE="$1" [[ -z "$SERVICE" ]] && return 1 - if [[ "$SERVICE" =~ ^[A-Za-z0-9_]+$ ]]; then + if [[ "$SERVICE" =~ ^[A-Za-z0-9_-]+$ ]]; then return 0 fi @@ -780,3 +784,13 @@ verify_service_name() { [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist" return 0 } + +write_database_name() { + declare desc="Writes a sanitized database name" + declare SERVICE="$1" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + + # some datastores do not like special characters in database names + # so we need to normalize them out + echo "$SERVICE" | tr .- _ > "$SERVICE_ROOT/DATABASE_NAME" +} diff --git a/functions b/functions index 0103151..e0ff092 100755 --- a/functions +++ b/functions @@ -65,6 +65,8 @@ service_create() { else echo "" >"$SERVICE_ROOT/ENV" fi + + write_database_name "$SERVICE" service_create_container "$SERVICE" } diff --git a/tests/service_create.bats b/tests/service_create.bats index ac5976f..e9ac62d 100755 --- a/tests/service_create.bats +++ b/tests/service_create.bats @@ -7,6 +7,15 @@ load test_helper dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l } +@test "($PLUGIN_COMMAND_PREFIX:create) service with dashes" { + run dokku "$PLUGIN_COMMAND_PREFIX:create" service-with-dashes + assert_contains "${lines[*]}" "container created: service-with-dashes" + assert_contains "${lines[*]}" "dokku-$PLUGIN_COMMAND_PREFIX-service-with-dashes" + assert_contains "${lines[*]}" "service_with_dashes" + + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" service-with-dashes +} + @test "($PLUGIN_COMMAND_PREFIX:create) error when there are no arguments" { run dokku "$PLUGIN_COMMAND_PREFIX:create" assert_contains "${lines[*]}" "Please specify a valid name for the service" @@ -15,7 +24,4 @@ load test_helper @test "($PLUGIN_COMMAND_PREFIX:create) error when there is an invalid name specified" { run dokku "$PLUGIN_COMMAND_PREFIX:create" d.erp assert_failure - - run dokku "$PLUGIN_COMMAND_PREFIX:create" d-erp - assert_failure }