diff --git a/plugins/docker-options/commands b/plugins/docker-options/commands index c9661756c..a1756739c 100755 --- a/plugins/docker-options/commands +++ b/plugins/docker-options/commands @@ -1,118 +1,14 @@ #!/usr/bin/env bash -[[ " docker-options docker-options:add docker-options:remove help docker-options:help " == *" $1 "* ]] || exit $DOKKU_NOT_IMPLEMENTED_EXIT +[[ " docker-options docker-options:add docker-options:remove help docker-options:help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" - -get_app() { - [[ -z $1 ]] && dokku_log_fail "Please specify an app to run the command on" - verify_app_name "$1" - APP="$1" -} - -get_phases() { - local passed_phases_list=$1 - local phase - if [[ -n $passed_phases_list ]]; then - IFS=',' read -ra passed_phases <<< "$passed_phases_list" - for phase in "${passed_phases[@]}"; do - verify_phase $phase - done - fi -} - -verify_phase() { - local phase - local phases=(build deploy run) - for phase in "${phases[@]}"; do - if [[ "$phase" = "$1" ]]; then - return 0 - fi - done - dokku_log_fail "Phase(s) must be one of [${phases[*]}]" -} - -get_phase_file_path() { - local phase=$1 - local prefix="DOCKER_OPTIONS_" - phase_file_path="${DOKKU_ROOT}/${APP}/${prefix}${phase^^}" -} - -create_phase_file_if_required() { - local phase_file_path=$1 - [[ -f $phase_file_path ]] || { - touch $phase_file_path - } -} - -display_phase_options() { - local phase=$1 - local phase_file_path=$2 - echo "${phase^} options:" - sed -e 's/^/ /' $phase_file_path -} - -display_all_phases_options() { - local phases=(build deploy run) - for phase in "${phases[@]}"; do - get_phase_file_path $phase - if [[ -s $phase_file_path ]]; then - display_phase_options $phase $phase_file_path - fi - done -} - -display_passed_phases_options() { - local passed_phases=("${!1}") - local phase - for phase in "${passed_phases[@]}"; do - get_phase_file_path $phase - if [[ ! -s $phase_file_path ]]; then - echo "${phase^} options: none" - else - display_phase_options $phase $phase_file_path - fi - done -} - -get_passed_docker_option() { - [[ -z "$@" ]] && dokku_log_fail "Please specify docker options to add to the phase" - passed_docker_option=("$@") -} - -add_passed_docker_option() { - local passed_phases=("${!1}") - shift - local passed_option_string="$*" - local phase - for phase in "${passed_phases[@]}"; do - get_phase_file_path $phase - create_phase_file_if_required $phase_file_path - echo "${passed_option_string}" >> $phase_file_path - all_phase_options=$(< $phase_file_path) - echo -e "${all_phase_options}" | sed '/^$/d' | sort -u > $phase_file_path - done -} - -remove_passed_docker_option() { - local passed_phases=("${!1}") - shift - local passed_option_string="$*" - local phase - for phase in "${passed_phases[@]}"; do - get_phase_file_path $phase - [[ ! -s $phase_file_path ]] || { - all_phase_options=$(< $phase_file_path) - all_phase_options=$(echo -e "${all_phase_options}" | sed "s#^${passed_option_string}\$##") - echo -e "${all_phase_options}" | sed '/^$/d' | sort -u > $phase_file_path - } - done -} +source "$PLUGIN_CORE_AVAILABLE_PATH/docker-options/functions" case "$1" in # Display applications docker options docker-options) - get_app $2 - get_phases $3 + verify_app_name "$2" && APP="$2" + read -ra passed_phases <<< "$(get_phases "$3")" if [[ ! "${passed_phases[@]}" ]]; then display_all_phases_options else @@ -122,19 +18,20 @@ case "$1" in # Add a docker option to application docker-options:add) - get_app $2 - get_phases $3 + verify_app_name "$2" && APP="$2" + read -ra passed_phases <<< "$(get_phases "$3")" shift 3 # everything else passed is the docker option - get_passed_docker_option "$@" + passed_docker_option="$*" + [[ -z "$passed_docker_option" ]] && dokku_log_fail "Please specify docker options to add to the phase" add_passed_docker_option passed_phases[@] "${passed_docker_option[@]}" ;; # Remove a docker option from application docker-options:remove) - get_app $2 - get_phases $3 + verify_app_name "$2" && APP="$2" + read -ra passed_phases <<< "$(get_phases "$3")" shift 3 # everything else passed is the docker option - get_passed_docker_option "$@" + [[ -z ${passed_docker_option="$@"} ]] && dokku_log_fail "Please specify docker options to add to the phase" remove_passed_docker_option passed_phases[@] "${passed_docker_option[@]}" ;; @@ -149,7 +46,7 @@ EOF ;; *) - exit $DOKKU_NOT_IMPLEMENTED_EXIT + exit "$DOKKU_NOT_IMPLEMENTED_EXIT" ;; esac diff --git a/plugins/docker-options/functions b/plugins/docker-options/functions new file mode 100644 index 000000000..8abd82cb9 --- /dev/null +++ b/plugins/docker-options/functions @@ -0,0 +1,95 @@ +#!/usr/bin/env bash +[[ " docker-options docker-options:add docker-options:remove help docker-options:help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" + +AVAILABLE_PHASES=(build deploy run) + +get_phase_file_path() { + local phase_file_prefix="DOCKER_OPTIONS_" + echo "${DOKKU_ROOT}/${APP}/${phase_file_prefix}${phase^^}" +} + +get_phases() { + local passed_phases_list="$1" + local -r phases_allowed=$(sed -e 's/ /\|/g' <<< "${AVAILABLE_PHASES[@]}") + local phase + local passed_phases + if [[ -n "$passed_phases_list" ]]; then + IFS=',' read -ra passed_phases <<< "$passed_phases_list" + for phase in "${passed_phases[@]}"; do + [[ "$phase" = @($phases_allowed) ]] || dokku_log_fail "Phase(s) must be one of [${AVAILABLE_PHASES[*]}]" + done + echo "${passed_phases[@]}" + fi +} + +create_phase_file_if_required() { + local phase_file_path="$1" + [[ -f "$phase_file_path" ]] || { + touch "$phase_file_path" + } +} + +display_phase_options() { + local phase="$1" + local phase_file_path="$2" + echo "${phase^} options:" + sed -e 's/^/ /' "$phase_file_path" +} + +display_all_phases_options() { + local phases=("${AVAILABLE_PHASES[@]}") + for phase in "${phases[@]}"; do + local phase_file_path + phase_file_path="$(get_phase_file_path)" + if [[ -s "$phase_file_path" ]]; then + display_phase_options "$phase" "$phase_file_path" + fi + done +} + +display_passed_phases_options() { + local passed_phases=("${!1}") + local phase + for phase in "${passed_phases[@]}"; do + local phase_file_path + phase_file_path="$(get_phase_file_path)" + if [[ ! -s "$phase_file_path" ]]; then + echo "${phase^} options: none" + else + display_phase_options "$phase" "$phase_file_path" + fi + done +} + +add_passed_docker_option() { + local passed_phases=("${!1}") + shift + local passed_option_string="$*" + local phase + for phase in "${passed_phases[@]}"; do + local phase_file_path + phase_file_path="$(get_phase_file_path)" + create_phase_file_if_required "$phase_file_path" + echo "${passed_option_string}" >> "$phase_file_path" + all_phase_options="$(< "$phase_file_path")" + echo -e "${all_phase_options}" | sed '/^$/d' | sort -u > "$phase_file_path" + done +} + +remove_passed_docker_option() { + local passed_phases=("${!1}") + shift + local passed_option_string="$*" + local phase + for phase in "${passed_phases[@]}"; do + local phase_file_path + phase_file_path="$(get_phase_file_path)" + [[ ! -s "$phase_file_path" ]] || { + all_phase_options="$(< "$phase_file_path")" + all_phase_options=$(echo -e "${all_phase_options}" | sed "s#^${passed_option_string}\$##") + echo -e "${all_phase_options}" | sed '/^$/d' | sort -u > "$phase_file_path" + } + done +}