Merge pull request #1291 from progrium/1261_mh-config-args-refactor
initial pass at config plugin arguments refactor
This commit is contained in:
commit
4629a9909b
2 files changed
+113
-57
No files matched your search
+70
-56
@@ -2,8 +2,6 @@
|
|||||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||||
source "$(dirname $0)/../common/functions"
|
source "$(dirname $0)/../common/functions"
|
||||||
|
|
||||||
ENV_FILE="$DOKKU_ROOT/$2/ENV"
|
|
||||||
ENV_FILE_TEMP="$DOKKU_ROOT/$2/ENV.tmp"
|
|
||||||
config_create () {
|
config_create () {
|
||||||
[ -f $ENV_FILE ] || {
|
[ -f $ENV_FILE ] || {
|
||||||
touch $ENV_FILE
|
touch $ENV_FILE
|
||||||
@@ -24,7 +22,7 @@ config_styled_hash () {
|
|||||||
|
|
||||||
while read -r word; do
|
while read -r word; do
|
||||||
KEY=$(echo $word | cut -d"=" -f1)
|
KEY=$(echo $word | cut -d"=" -f1)
|
||||||
VALUE=$(echo $word | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//")
|
VALUE=$(echo $word | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//" -e "s/\$$//g")
|
||||||
|
|
||||||
num_zeros=$((${#longest} - ${#KEY}))
|
num_zeros=$((${#longest} - ${#KEY}))
|
||||||
zeros=" "
|
zeros=" "
|
||||||
@@ -38,6 +36,8 @@ config_styled_hash () {
|
|||||||
|
|
||||||
config_write() {
|
config_write() {
|
||||||
ENV_TEMP="$1"
|
ENV_TEMP="$1"
|
||||||
|
ENV_FILE_TEMP="${ENV_FILE}.tmp"
|
||||||
|
|
||||||
echo "$ENV_TEMP" | sed '/^$/d' | sort > $ENV_FILE_TEMP
|
echo "$ENV_TEMP" | sed '/^$/d' | sort > $ENV_FILE_TEMP
|
||||||
if ! cmp -s $ENV_FILE $ENV_FILE_TEMP; then
|
if ! cmp -s $ENV_FILE $ENV_FILE_TEMP; then
|
||||||
cp -f $ENV_FILE_TEMP $ENV_FILE
|
cp -f $ENV_FILE_TEMP $ENV_FILE
|
||||||
@@ -46,14 +46,43 @@ config_write() {
|
|||||||
rm -f $ENV_FILE_TEMP
|
rm -f $ENV_FILE_TEMP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parse_config_args() {
|
||||||
|
case "$2" in
|
||||||
|
--global)
|
||||||
|
ENV_FILE="$DOKKU_ROOT/ENV"
|
||||||
|
DOKKU_CONFIG_TYPE="global"
|
||||||
|
DOKKU_CONFIG_RESTART=false
|
||||||
|
;;
|
||||||
|
--no-restart)
|
||||||
|
APP="$3"
|
||||||
|
ENV_FILE="$DOKKU_ROOT/$APP/ENV"
|
||||||
|
DOKKU_CONFIG_RESTART=false
|
||||||
|
DOKKU_CONFIG_TYPE="app"
|
||||||
|
set -- "${@:1:1}" "${@:3}"
|
||||||
|
esac
|
||||||
|
|
||||||
|
APP=${APP:="$2"}
|
||||||
|
ENV_FILE=${ENV_FILE:="$DOKKU_ROOT/$APP/ENV"}
|
||||||
|
DOKKU_CONFIG_TYPE=${DOKKU_CONFIG_TYPE:="app"}
|
||||||
|
DOKKU_CONFIG_RESTART=${DOKKU_CONFIG_RESTART:=true}
|
||||||
|
|
||||||
|
if [[ "$DOKKU_CONFIG_TYPE" = "app" ]]; then
|
||||||
|
if [[ -z $APP ]]; then
|
||||||
|
echo "Please specify an app to run the command on" >&2 && exit 1
|
||||||
|
else
|
||||||
|
verify_app_name "$2"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
export APP ENV_FILE DOKKU_CONFIG_TYPE DOKKU_CONFIG_RESTART
|
||||||
|
}
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
config)
|
config)
|
||||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
parse_config_args "$@"
|
||||||
verify_app_name "$2"
|
|
||||||
APP="$2"
|
|
||||||
|
|
||||||
config_create
|
config_create
|
||||||
[[ ! -s $ENV_FILE ]] && echo "$APP has no config vars" && exit 1
|
|
||||||
|
[[ "$APP" ]] && DOKKU_CONFIG_TYPE=$APP
|
||||||
|
[[ ! -s $ENV_FILE ]] && echo "no config vars for $DOKKU_CONFIG_TYPE" && exit 1
|
||||||
|
|
||||||
VARS=$(grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" $ENV_FILE | cut -d" " -f2-)
|
VARS=$(grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" $ENV_FILE | cut -d" " -f2-)
|
||||||
|
|
||||||
@@ -64,14 +93,13 @@ case "$1" in
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
dokku_log_info2_quiet "$APP config vars"
|
|
||||||
|
dokku_log_info2_quiet "$DOKKU_CONFIG_TYPE config vars"
|
||||||
config_styled_hash "$VARS"
|
config_styled_hash "$VARS"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
config:get)
|
config:get)
|
||||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
parse_config_args "$@"
|
||||||
verify_app_name "$2"
|
|
||||||
APP="$2"
|
|
||||||
|
|
||||||
if [[ -z $3 ]]; then
|
if [[ -z $3 ]]; then
|
||||||
echo "Usage: dokku config:get APP KEY"
|
echo "Usage: dokku config:get APP KEY"
|
||||||
@@ -90,26 +118,8 @@ case "$1" in
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
config:set)
|
config:set)
|
||||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
parse_config_args "$@"
|
||||||
verify_app_name "$2"
|
[[ "$2" = "--no-restart" ]] && set -- "${@:1:1}" "${@:3}"
|
||||||
APP="$2"
|
|
||||||
|
|
||||||
if [[ -z "${*:3}" ]]; then
|
|
||||||
echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
|
|
||||||
echo "Must specify KEY and VALUE to set."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
shift 2
|
|
||||||
dokku config:set-norestart $APP "$@"
|
|
||||||
dokku_log_info1 "Restarting app $APP"
|
|
||||||
dokku ps:restart $APP
|
|
||||||
;;
|
|
||||||
|
|
||||||
config:set-norestart)
|
|
||||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
|
||||||
verify_app_name "$2"
|
|
||||||
APP="$2"
|
|
||||||
|
|
||||||
if [[ -z "${*:3}" ]]; then
|
if [[ -z "${*:3}" ]]; then
|
||||||
echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
|
echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]"
|
||||||
@@ -153,29 +163,16 @@ ${var}"
|
|||||||
|
|
||||||
config_write "$ENV_TEMP"
|
config_write "$ENV_TEMP"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ "$DOKKU_CONFIG_RESTART" == "true" ]]; then
|
||||||
|
dokku_log_info1 "Restarting app $APP"
|
||||||
|
dokku ps:restart $APP
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
|
|
||||||
config:unset)
|
config:unset)
|
||||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
parse_config_args "$@"
|
||||||
verify_app_name "$2"
|
[[ "$2" = "--no-restart" ]] && set -- "${@:1:1}" "${@:3}"
|
||||||
APP="$2"
|
|
||||||
|
|
||||||
if [[ -z $3 ]]; then
|
|
||||||
echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
|
|
||||||
echo "Must specify KEY to unset."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
shift 2
|
|
||||||
dokku config:unset-norestart $APP "$@"
|
|
||||||
dokku_log_info1 "Restarting app $APP"
|
|
||||||
dokku ps:restart $APP
|
|
||||||
;;
|
|
||||||
|
|
||||||
config:unset-norestart)
|
|
||||||
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
|
||||||
verify_app_name "$2"
|
|
||||||
APP="$2"
|
|
||||||
|
|
||||||
if [[ -z $3 ]]; then
|
if [[ -z $3 ]]; then
|
||||||
echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
|
echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"
|
||||||
@@ -193,14 +190,31 @@ ${var}"
|
|||||||
|
|
||||||
config_write "$ENV_TEMP"
|
config_write "$ENV_TEMP"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [[ "$DOKKU_CONFIG_RESTART" == "true" ]]; then
|
||||||
|
dokku_log_info1 "Restarting app $APP"
|
||||||
|
dokku ps:restart $APP
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
config:set-norestart)
|
||||||
|
dokku_log_info2 "$1 is deprecated as of v0.3.22"
|
||||||
|
shift 1
|
||||||
|
dokku config:set --no-restart "$@"
|
||||||
|
;;
|
||||||
|
|
||||||
|
config:unset-norestart)
|
||||||
|
dokku_log_info2 "$1 is deprecated as of v0.3.22"
|
||||||
|
shift 1
|
||||||
|
dokku config:unset --no-restart "$@"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
help | config:help)
|
help | config:help)
|
||||||
cat && cat<<EOF
|
cat && cat<<EOF
|
||||||
config <app> Display the config vars for an app
|
config (<app>|--global) Display all global or app-specific config vars
|
||||||
config:get <app> KEY Display a config value for an app
|
config:get (<app>|--global) KEY Display a global or app-specific config value
|
||||||
config:set <app> KEY1=VALUE1 [KEY2=VALUE2 ...] Set one or more config vars
|
config:set (<app>|--global) KEY1=VALUE1 [KEY2=VALUE2 ...] Set one or more config vars
|
||||||
config:unset <app> KEY1 [KEY2 ...] Unset one or more config vars
|
config:unset (<app>|--global) KEY1 [KEY2 ...] Unset one or more config vars
|
||||||
EOF
|
EOF
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|||||||
+43
-1
@@ -3,13 +3,55 @@
|
|||||||
load test_helper
|
load test_helper
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
|
[[ -f $DOKKU_ROOT/ENV ]] && mv -f $DOKKU_ROOT/ENV $DOKKU_ROOT/ENV.bak
|
||||||
sudo -H -u dokku /bin/bash -c "echo 'export global_test=true' > $DOKKU_ROOT/ENV"
|
sudo -H -u dokku /bin/bash -c "echo 'export global_test=true' > $DOKKU_ROOT/ENV"
|
||||||
create_app
|
create_app
|
||||||
}
|
}
|
||||||
|
|
||||||
teardown() {
|
teardown() {
|
||||||
destroy_app
|
destroy_app
|
||||||
rm -f "$DOKKU_ROOT/ENV"
|
[[ -f $DOKKU_ROOT/ENV.bak ]] && mv -f $DOKKU_ROOT/ENV.bak $DOKKU_ROOT/ENV
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "(config) config:set --global" {
|
||||||
|
run ssh dokku@dokku.me config:set --global test_var=true test_var2=\"hello world\"
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_success
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "(config) config:get --global" {
|
||||||
|
run ssh dokku@dokku.me config:set --global test_var=true test_var2=\"hello world\" test_var3=\"with\\nnewline\"
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_success
|
||||||
|
run dokku config:get --global test_var2
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_output 'hello world'
|
||||||
|
run dokku config:get --global test_var3
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_output 'with\nnewline'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "(config) config:unset --global" {
|
||||||
|
run ssh dokku@dokku.me config:set --global test_var=true test_var2=\"hello world\"
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_success
|
||||||
|
run dokku config:get --global test_var
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_success
|
||||||
|
run dokku config:unset --global test_var
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_success
|
||||||
|
run dokku config:get --global test_var
|
||||||
|
echo "output: "$output
|
||||||
|
echo "status: "$status
|
||||||
|
assert_output ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "(config) config:set" {
|
@test "(config) config:set" {
|
||||||
|
|||||||
Reference in new issue
Block a user