From db4d8dba88b795d7bf263b2f3b1b702d2afa67ad Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 29 May 2022 11:28:34 -0400 Subject: [PATCH] refactor: consolidate app name verification into golang binary This change ensures we only have to change this logic once in order to get them to apply to both languages in the codebase. --- plugins/common/functions | 34 +---------------------------- plugins/common/log.go | 13 +++++++++++ plugins/common/src/common/common.go | 5 ++++- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index 0b4b0c0d0..98237caf5 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -250,43 +250,11 @@ is_valid_app_name_old() { verify_app_name() { declare desc="verify app name format and app existence" declare APP="$1" - export SSH_USER=${SSH_USER:=$USER} - export SSH_NAME=${NAME:="default"} - local VALID_NEW=false - local VALID_OLD=false - if fn-is-valid-app-name "$APP" 2>/dev/null; then - VALID_NEW=true - fi - - if fn-is-valid-app-name-old "$APP" 2>/dev/null; then - VALID_OLD=true - fi - - if [[ "$VALID_NEW" == "false" ]] && [[ "$VALID_OLD" == "false" ]]; then - dokku_log_fail "App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, colons, or underscores" - fi - - [[ ! -d "$DOKKU_ROOT/$APP" ]] && DOKKU_FAIL_EXIT_CODE=20 dokku_log_fail "App $APP does not exist" - - local user_auth_count=$(find "$PLUGIN_PATH"/enabled/*/user-auth-app 2>/dev/null | wc -l) - - # no plugin trigger exists - if [[ $user_auth_count == 0 ]]; then + if "$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet verify-app-name "$APP"; then return 0 fi - # this plugin trigger exists in the core `20_events` plugin - if [[ "$user_auth_count" == 1 ]] && [[ -f "$PLUGIN_PATH"/enabled/20_events/user-auth-app ]]; then - return 0 - fi - - if output="$(plugn trigger user-auth-app "$SSH_USER" "$SSH_NAME" "$@" "$APP")"; then - if [[ -n "$output" ]]; then - return 0 - fi - fi - DOKKU_FAIL_EXIT_CODE=20 dokku_log_fail "App $APP does not exist" } diff --git a/plugins/common/log.go b/plugins/common/log.go index d45f26e48..dec3791b1 100644 --- a/plugins/common/log.go +++ b/plugins/common/log.go @@ -61,6 +61,19 @@ func LogFailWithError(err error) { os.Exit(1) } +// LogFailWithErrorQuiet is the failure log formatter (with quiet option) +// prints text to stderr and exits with the specified exit code +// The error message is not printed if DOKKU_QUIET_OUTPUT has any value +func LogFailWithErrorQuiet(err error) { + if os.Getenv("DOKKU_QUIET_OUTPUT") == "" { + fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", err.Error())) + } + if errExit, ok := err.(ErrWithExitCode); ok { + os.Exit(errExit.ExitCode()) + } + os.Exit(1) +} + // LogFailQuiet is the failure log formatter (with quiet option) // prints text to stderr and exits with status 1 func LogFailQuiet(text string) { diff --git a/plugins/common/src/common/common.go b/plugins/common/src/common/common.go index 432aa00e8..4b4c078dc 100644 --- a/plugins/common/src/common/common.go +++ b/plugins/common/src/common/common.go @@ -38,11 +38,14 @@ func main() { appName = "--global" } fmt.Print(common.GetAppScheduler(appName)) + case "verify-app-name": + appName := flag.Arg(1) + err = common.VerifyAppName(appName) default: err = fmt.Errorf("Invalid common command call: %v", cmd) } if err != nil { - common.LogFailQuiet(err.Error()) + common.LogFailWithErrorQuiet(err) } }