From a3b3a6a12a2d87f02562054f639e218426e44bce Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 22:51:08 -0500 Subject: [PATCH 1/7] Unify missing app error messages --- plugins/apps/commands | 4 ++-- plugins/config/commands | 2 +- plugins/nginx-vhosts/commands | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/apps/commands b/plugins/apps/commands index 8cf943943..2c8367b39 100755 --- a/plugins/apps/commands +++ b/plugins/apps/commands @@ -9,7 +9,7 @@ case "$1" in apps:create) if [[ -z $2 ]]; then - echo "Please specify an app to create" + echo "Please specify an app to run the command on" exit 1 fi APP="$2" @@ -24,7 +24,7 @@ case "$1" in apps:destroy) if [[ -z $2 ]]; then - echo "Please specify an app to delete" + echo "Please specify an app to run the command on" exit 1 fi APP="$2"; IMAGE="dokku/$APP"; diff --git a/plugins/config/commands b/plugins/config/commands index 2e46d53e4..056bef809 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -4,7 +4,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x # Check if name is specified if [[ $1 == config ]] || [[ $1 == config:* ]]; then if [[ -z $2 ]]; then - echo "You must specify an app name" + echo "Please specify an app to run the command on" exit 1 else APP="$2" diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 97567d2c3..56e95920e 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -4,7 +4,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x case "$1" in nginx:import-ssl) if [[ -z $2 ]]; then - echo "Please specify an app to create" + echo "Please specify an app to run the command on" exit 1 fi APP="$2" From 12194e2cc89eef81ca706134aef6fd7ec678832f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 22:53:55 -0500 Subject: [PATCH 2/7] Add proper error handling to each config command --- plugins/config/commands | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/plugins/config/commands b/plugins/config/commands index 056bef809..ed5c4799b 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -76,7 +76,15 @@ config_write() { case "$1" in config) + if [[ -z $2 ]]; then + echo "Please specify an app to run the command on" + exit 1 + fi APP="$2" + if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then + echo "App $APP does not exist" + exit 1 + fi if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then echo "$APP has no config vars" @@ -97,6 +105,15 @@ case "$1" in ;; config:get) + if [[ -z $2 ]]; then + echo "Please specify an app to run the command on" + exit 1 + fi + APP="$2" + if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then + echo "App $APP does not exist" + exit 1 + fi if [[ -z $3 ]]; then echo "Usage: dokku config:get APP KEY" echo "Must specify KEY." @@ -113,13 +130,21 @@ case "$1" in ;; config:set) + if [[ -z $2 ]]; then + echo "Please specify an app to run the command on" + exit 1 + fi + APP="$2" + if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then + echo "App $APP does not exist" + exit 1 + fi 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 - APP="$2"; APP_DIR="$DOKKU_ROOT/$APP" ENV_ADD="" ENV_TEMP=`cat "${ENV_FILE}"` RESTART_APP=false @@ -157,13 +182,21 @@ ${var}" ;; config:unset) + if [[ -z $2 ]]; then + echo "Please specify an app to run the command on" + exit 1 + fi + APP="$2" + if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then + echo "App $APP does not exist" + exit 1 + fi if [[ -z $3 ]]; then echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]" echo "Must specify KEY to unset." exit 1 fi - APP="$2"; APP_DIR="$DOKKU_ROOT/$APP" ENV_TEMP=`cat "${ENV_FILE}"` VARS="${*:3}" From c6e39c08f0e3faeb4b2e6a0da72a30c6c609035d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 23:00:34 -0500 Subject: [PATCH 3/7] Simplify env var checking in config plugin All checks are now done within the commands, and we only need to ensure the config file exists. --- plugins/config/commands | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/plugins/config/commands b/plugins/config/commands index ed5c4799b..616e5a473 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -1,28 +1,14 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x -# Check if name is specified -if [[ $1 == config ]] || [[ $1 == config:* ]]; then - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - else - APP="$2" - ENV_FILE="$DOKKU_ROOT/$APP/ENV" - ENV_FILE_TEMP="$DOKKU_ROOT/$APP/ENV.tmp" - - # Check if app exists with the same name - if [ ! -d "$DOKKU_ROOT/$APP" ]; then - echo "App $APP does not exist" - exit 1 - fi - - [ -f $ENV_FILE ] || { - echo "-----> Creating $ENV_FILE" - touch $ENV_FILE - } - fi -fi +ENV_FILE="$DOKKU_ROOT/$2/ENV" +ENV_FILE_TEMP="$DOKKU_ROOT/$2/ENV.tmp" +config_create () { + [ -f $ENV_FILE ] || { + echo "-----> Creating $ENV_FILE" + touch $ENV_FILE + } +} config_styled_hash () { vars="$1" @@ -86,6 +72,7 @@ case "$1" in exit 1 fi + config_create if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then echo "$APP has no config vars" exit 1 @@ -120,6 +107,7 @@ case "$1" in exit 1 fi + config_create if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then exit 1 fi @@ -145,6 +133,7 @@ case "$1" in exit 1 fi + config_create ENV_ADD="" ENV_TEMP=`cat "${ENV_FILE}"` RESTART_APP=false @@ -197,6 +186,7 @@ ${var}" exit 1 fi + config_create ENV_TEMP=`cat "${ENV_FILE}"` VARS="${*:3}" From 303bd77226469277eb34d9b5061a426150aa2c08 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 23:01:08 -0500 Subject: [PATCH 4/7] Simplify if statement --- plugins/config/commands | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/config/commands b/plugins/config/commands index 616e5a473..0086a0ed4 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -73,7 +73,7 @@ case "$1" in fi config_create - if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then + if [[ ! -s $ENV_FILE ]] ; then echo "$APP has no config vars" exit 1 fi @@ -108,7 +108,7 @@ case "$1" in fi config_create - if [ ! -f $ENV_FILE ] || [ ! -s $ENV_FILE ] ; then + if [[ ! -s $ENV_FILE ]] ; then exit 1 fi From 56185fc1c2528fa17fdc5054b7f4b69276443f8d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 23:02:22 -0500 Subject: [PATCH 5/7] Correct plugin example $APP is not available until after the if guards --- docs/plugins.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/plugins.md b/docs/plugins.md index c67a780cc..c588af695 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -39,7 +39,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x case "$1" in hello) [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - [[ ! -d "$DOKKU_ROOT/$APP" ]] && echo "App $APP does not exist" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2"; echo "Hello $APP" From 04dbe31919f8909a10dddb2e0e5c8ca38221da14 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 23:31:33 -0500 Subject: [PATCH 6/7] Unify the way in which all error messages are specified Other parts of dokku use one-liners where possible, so these parts should as well --- plugins/00_dokku-standard/commands | 27 +++++------------ plugins/apps/commands | 26 ++++------------ plugins/config/commands | 48 ++++++++---------------------- plugins/nginx-vhosts/commands | 25 ++++------------ 4 files changed, 29 insertions(+), 97 deletions(-) diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index e30fb6eb3..7fd4f9476 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -7,15 +7,9 @@ case "$1" in ;; logs) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2"; - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then CONTAINER=$(<$DOKKU_ROOT/$APP/CONTAINER) @@ -30,15 +24,10 @@ case "$1" in ;; run) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2"; IMAGE="dokku/$APP" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi + shift 2 DOCKER_ARGS=$(: | pluginhook docker-args $APP) @@ -46,11 +35,9 @@ case "$1" in ;; url) + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2"; - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi if [[ -f "$DOKKU_ROOT/$APP/URL" ]]; then echo $(< "$DOKKU_ROOT/$APP/URL") diff --git a/plugins/apps/commands b/plugins/apps/commands index 2c8367b39..2fed52a7e 100755 --- a/plugins/apps/commands +++ b/plugins/apps/commands @@ -8,35 +8,19 @@ case "$1" in ;; apps:create) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ -d "$DOKKU_ROOT/$APP" ]] && echo " ! Name is already taken" && exit 1 APP="$2" - if [[ -d "$DOKKU_ROOT/$APP" ]]; then - echo " ! Name is already taken" - exit 1 - fi mkdir -p "$DOKKU_ROOT/$APP" echo "Creating $APP... done" ;; apps:destroy) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 + [[ "$2" == "tls" ]] && echo "Unable to destroy tls directory" && exit 1 APP="$2"; IMAGE="dokku/$APP"; - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi - - if [[ "$APP" == "tls" ]]; then - echo "Unable to destroy tls directory" - exit 1 - fi echo " ! WARNING: Potentially Destructive Action" echo " ! This command will destroy $APP (including all add-ons)." diff --git a/plugins/config/commands b/plugins/config/commands index 0086a0ed4..90242f56c 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -62,21 +62,12 @@ config_write() { case "$1" in config) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi config_create - if [[ ! -s $ENV_FILE ]] ; then - echo "$APP has no config vars" - exit 1 - fi + [[ ! -s $ENV_FILE ]] && echo "$APP has no config vars" && exit 1 VARS=`cat $ENV_FILE | grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" | cut -d" " -f2-` @@ -92,15 +83,10 @@ case "$1" in ;; config:get) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi + if [[ -z $3 ]]; then echo "Usage: dokku config:get APP KEY" echo "Must specify KEY." @@ -118,15 +104,10 @@ case "$1" in ;; config:set) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi + if [[ -z "${*:3}" ]]; then echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]" echo "Must specify KEY and VALUE to set." @@ -171,15 +152,10 @@ ${var}" ;; config:unset) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 APP="$2" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi + if [[ -z $3 ]]; then echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]" echo "Must specify KEY to unset." diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 56e95920e..e2b4632ac 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -3,31 +3,16 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x case "$1" in nginx:import-ssl) - if [[ -z $2 ]]; then - echo "Please specify an app to run the command on" - exit 1 - fi + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 + [[ -t 0 ]] && echo "Tar archive containing server.crt and server.key expected on stdin" && exit 1 APP="$2" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - echo "App $APP does not exist" - exit 1 - fi - if [[ -t 0 ]]; then - echo "Tar archive containing server.crt and server.key expected on stdin" - exit 1 - fi TEMP_DIR=`mktemp -d` cd $TEMP_DIR tar xvf - <&0 - if [[ -f "$TEMP_DIR/server.crt" ]]; then - echo "Tar archive missing server.crt" - exit 1 - fi - if [[ -f "$TEMP_DIR/server.key" ]]; then - echo "Tar archive missing server.key" - exit 1 - fi + [[ -f "$TEMP_DIR/server.crt" ]] && echo "Tar archive missing server.crt" && exit 1 + [[ -f "$TEMP_DIR/server.key" ]] && echo "Tar archive missing server.key" && exit 1 mkdir -p "$DOKKU_ROOT/$APP/tls" mv "$TEMP_DIR/server.crt" "$DOKKU_ROOT/$APP/tls/server.crt" From 407436b4cccedde641d321fb8900ab19886a1f1a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 23 Nov 2014 23:32:38 -0500 Subject: [PATCH 7/7] Exit 0 if there is no configuration heroku config:get exits 0 in this case --- plugins/config/commands | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/config/commands b/plugins/config/commands index 90242f56c..7592228ee 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -95,7 +95,7 @@ case "$1" in config_create if [[ ! -s $ENV_FILE ]] ; then - exit 1 + exit 0 fi KEY="$3"