From 4ae2eaedd9bdcf72b579c8bdd33cfe62c249a7d6 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Tue, 23 Jan 2024 01:18:52 -0800 Subject: [PATCH] [lib] Use function exec_cmd from lib. --- src/dns-lists-downloader.sh | 11 +---------- src/lib-common.sh | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/dns-lists-downloader.sh b/src/dns-lists-downloader.sh index df8285b..e8a0e4c 100755 --- a/src/dns-lists-downloader.sh +++ b/src/dns-lists-downloader.sh @@ -101,16 +101,7 @@ download_from_single_source_file() { continue fi fix_list "${CURRENT_FILE}" - if [ -n "${POST_DOWNLOAD_CMD}" ]; then - local LOG= - log INFO "Run POST_DOWNLOAD_CMD: ${POST_DOWNLOAD_CMD} ${CURRENT_FILE}" - if LOG=$(eval "${POST_DOWNLOAD_CMD} ${CURRENT_FILE}" 2>&1); then - echo "${LOG}" | log_lines INFO - else - log WARN "POST_DOWNLOAD_CMD: \"${POST_DOWNLOAD_CMD} ${CURRENT_FILE}\" returned a non-zero value ${?}." - echo "${LOG}" | log_lines WARN - fi - fi + exec_cmd "post-download" "${POST_DOWNLOAD_CMD} ${CURRENT_FILE}" log DEBUG "Merging ${CURRENT_FILE} to ${ACCUMULATOR_FILE}" # SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects. # shellcheck disable=SC2129 diff --git a/src/lib-common.sh b/src/lib-common.sh index 12e5ece..e248960 100755 --- a/src/lib-common.sh +++ b/src/lib-common.sh @@ -211,6 +211,27 @@ read_env() { return 0 } +exec_cmd() { + local TAG="${1}"; shift; + local CMD="${*}" + [ -z "${CMD}" ] && return 0 + local OLD_LOG_SCOPE="${LOG_SCOPE}" + export LOG_SCOPE="${OLD_LOG_SCOPE} ${TAG}" + local LOG= + local RT=0 + log INFO "Run ${TAG} command: ${CMD}" + if LOG=$(eval "${CMD}"); then + echo "${LOG}" | log_lines INFO + else + RT=$? + echo "${LOG}" | log_lines WARN + log WARN "${TAG} command returned a non-zero value ${RT}." + fi + log INFO "Finish ${TAG} command." + export LOG_SCOPE="${OLD_LOG_SCOPE}" + return "${RT}" +} + swarm_network_arguments() { if [ -z "${NETWORK_NAME}" ]; then echo ""