From 669b28b666a2763db09a83e046adfb3470cd5024 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 7 Feb 2012 18:01:00 -0500 Subject: [PATCH] common.sh: move output helper funcs up top the top The shflags sourcing logic tries to use `die` before we've defined it, so if there's an issue, we end up with: bash: die: command not found and the shflags code doesn't actually get loaded. So relocate these small helpers to the top so we don't have to worry about when it's safe to use these things. BUG=None TEST=`rm lib/shflags/shflags && (. ./common.sh)` now exits properly Change-Id: Ibdc268e6c081aa07679dc9fce76e5603b7217b20 Reviewed-on: https://gerrit.chromium.org/gerrit/15442 Reviewed-by: Brian Harring Commit-Ready: Mike Frysinger Tested-by: Mike Frysinger --- common.sh | 69 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/common.sh b/common.sh index 4e3d1f3cba..f463d89a4f 100644 --- a/common.sh +++ b/common.sh @@ -35,6 +35,41 @@ else INSIDE_CHROOT=0 fi +# Determine and set up variables needed for fancy color output (if supported). +V_BOLD_RED= +V_BOLD_GREEN= +V_BOLD_YELLOW= +V_REVERSE= +V_VIDOFF= + +if tput colors >/dev/null 2>&1; then + # order matters: we want VIDOFF last so that when we trace with `set -x`, + # our terminal doesn't bleed colors as bash dumps the values of vars. + V_BOLD_RED="$(tput bold; tput setaf 1)" + V_BOLD_GREEN="$(tput bold; tput setaf 2)" + V_BOLD_YELLOW="$(tput bold; tput setaf 3)" + V_REVERSE="$(tput rev)" + V_VIDOFF="$(tput sgr0)" +fi + +# Declare these asap so that code below can safely assume they exist. +function info { + echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" +} + +function warn { + echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" +} + +function error { + echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" +} + +function die { + error "$@" + exit 1 +} + # Construct a list of possible locations for the source tree. This list is # based on various environment variables and globals that may have been set # by the calling script. @@ -244,23 +279,6 @@ FACTORY_SHIM_INSTALL_MASK=" /usr/share/zoneinfo " -# Determine and set up variables needed for fancy color output (if supported). -V_BOLD_RED= -V_BOLD_GREEN= -V_BOLD_YELLOW= -V_REVERSE= -V_VIDOFF= - -if tput colors >/dev/null 2>&1; then - # order matters: we want VIDOFF last so that when we trace with `set -x`, - # our terminal doesn't bleed colors as bash dumps the values of vars. - V_BOLD_RED="$(tput bold; tput setaf 1)" - V_BOLD_GREEN="$(tput bold; tput setaf 2)" - V_BOLD_YELLOW="$(tput bold; tput setaf 3)" - V_REVERSE="$(tput rev)" - V_VIDOFF="$(tput sgr0)" -fi - # ----------------------------------------------------------------------------- # Functions @@ -350,23 +368,6 @@ function check_flags_only_and_allow_null_arg { return $do_shift } -function info { - echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" -} - -function warn { - echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" -} - -function error { - echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}" -} - -function die { - error "$@" - exit 1 -} - # Retry an emerge command according to $FLAGS_retries # The $EMERGE_JOBS flags will only be added the first time the command is run function eretry () {