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.
This commit is contained in:
Jose Diaz-Gonzalez
2019-05-30 13:15:10 -04:00
parent 86ff4b4cfa
commit 6bab5bad28
3 changed files with 30 additions and 8 deletions

View File

@@ -19,10 +19,14 @@ get_container_ip() {
get_database_name() { get_database_name() {
declare desc="Retrieves a sanitized database name" declare desc="Retrieves a sanitized database name"
declare DATABASE="$1" declare SERVICE="$1"
# some datastores do not like special characters in database names local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
# so we need to normalize them out
echo "$DATABASE" | tr .- _ if [[ ! -f "$SERVICE_ROOT/DATABASE_NAME" ]]; then
echo "$SERVICE" > "$SERVICE_ROOT/DATABASE_NAME"
fi
cat "$SERVICE_ROOT/DATABASE_NAME"
} }
get_random_ports() { get_random_ports() {
@@ -89,7 +93,7 @@ is_valid_service_name() {
declare SERVICE="$1" declare SERVICE="$1"
[[ -z "$SERVICE" ]] && return 1 [[ -z "$SERVICE" ]] && return 1
if [[ "$SERVICE" =~ ^[A-Za-z0-9_]+$ ]]; then if [[ "$SERVICE" =~ ^[A-Za-z0-9_-]+$ ]]; then
return 0 return 0
fi fi
@@ -780,3 +784,13 @@ verify_service_name() {
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist" [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist"
return 0 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"
}

View File

@@ -65,6 +65,8 @@ service_create() {
else else
echo "" >"$SERVICE_ROOT/ENV" echo "" >"$SERVICE_ROOT/ENV"
fi fi
write_database_name "$SERVICE"
service_create_container "$SERVICE" service_create_container "$SERVICE"
} }

View File

@@ -7,6 +7,15 @@ load test_helper
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l 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" { @test "($PLUGIN_COMMAND_PREFIX:create) error when there are no arguments" {
run dokku "$PLUGIN_COMMAND_PREFIX:create" run dokku "$PLUGIN_COMMAND_PREFIX:create"
assert_contains "${lines[*]}" "Please specify a valid name for the service" 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" { @test "($PLUGIN_COMMAND_PREFIX:create) error when there is an invalid name specified" {
run dokku "$PLUGIN_COMMAND_PREFIX:create" d.erp run dokku "$PLUGIN_COMMAND_PREFIX:create" d.erp
assert_failure assert_failure
run dokku "$PLUGIN_COMMAND_PREFIX:create" d-erp
assert_failure
} }