From a422060414670bb49d2422a38467b73ae01e7ecb Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Fri, 13 Aug 2010 09:47:31 +0200 Subject: Use common functions to print messages, warnings and errors These functions are copied from makepkg --- db-functions | 70 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 16 deletions(-) (limited to 'db-functions') diff --git a/db-functions b/db-functions index f22567b..0c96913 100644 --- a/db-functions +++ b/db-functions @@ -16,11 +16,48 @@ restore_umask () { WORKDIR=$(mktemp -d /tmp/$(basename $0).XXXXXXXXXX) LOCKS=() +# check if messages are to be printed using color +unset ALL_OFF BOLD BLUE GREEN RED YELLOW +if [[ -t 2 ]]; then + ALL_OFF="$(tput sgr0)" + BOLD="$(tput bold)" + BLUE="${BOLD}$(tput setaf 4)" + GREEN="${BOLD}$(tput setaf 2)" + RED="${BOLD}$(tput setaf 1)" + YELLOW="${BOLD}$(tput setaf 3)" +fi +readonly ALL_OFF BOLD BLUE GREEN RED YELLOW + +plain() { + local mesg=$1; shift + printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +msg() { + local mesg=$1; shift + printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +msg2() { + local mesg=$1; shift + printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +warning() { + local mesg=$1; shift + printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 +} + +error() { + local mesg=$1; shift + printf "${RED}==> ERROR${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 +} + script_lock() { local LOCKDIR="$TMPDIR/.scriptlock.$(basename $0)" if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then local _owner="$(/usr/bin/stat -c %U $LOCKDIR)" - echo "Error: Script $(basename $0) is already locked by $_owner." >&2 + error "Script $(basename $0) is already locked by $_owner." exit 1 else set_umask @@ -31,7 +68,7 @@ script_lock() { script_unlock() { local LOCKDIR="$TMPDIR/.scriptlock.$(basename $0)" if [ ! -d "$LOCKDIR" ]; then - echo "Warning: Script $(basename $0) was not locked!" >&2 + warning "Script $(basename $0) was not locked!" restore_umask return 1 else @@ -51,12 +88,12 @@ cleanup() { repo=${l%.*} arch=${l#*.} if [ -d "$TMPDIR/.repolock.$repo.$arch" ]; then - echo "Removing left over lock from $repo/$arch" >&2 + msg "Removing left over lock from $repo/$arch" repo_unlock $repo $arch fi done if [ -d "$TMPDIR/.scriptlock.$(basename $0)" ]; then - echo "Removing left over lock from $(basename $0)" >&2 + msg "Removing left over lock from $(basename $0)" script_unlock fi rm -rf "$WORKDIR" @@ -64,12 +101,12 @@ cleanup() { } abort() { - echo 'Aborting...' >&2 + msg 'Aborting...' cleanup 0 } die() { - echo "$*" >&2 + error "$*" cleanup 1 } @@ -99,7 +136,8 @@ repo_lock () { while [ $_count -le $_trial ] || $_lockblock ; do if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then _owner="$(/usr/bin/stat -c %U $LOCKDIR)" - echo "Warning: Repo $1-$2 is already locked by $_owner. Retrying in $LOCK_DELAY seconds..." >&2 + warning "Repo $1-$2 is already locked by $_owner. " \ + "Retrying in $LOCK_DELAY seconds..." else LOCKS[${#LOCKS[*]}]="$1.$2" set_umask @@ -109,14 +147,14 @@ repo_lock () { let _count=$_count+1 done - echo "Error: Repo $1-$2 is already locked by $_owner. Giving up!" >&2 + error "Repo $1-$2 is already locked by $_owner. Giving up!" return 1 } repo_unlock () { #repo_unlock local LOCKDIR="$TMPDIR/.repolock.$1.$2" if [ ! -d "$LOCKDIR" ]; then - echo "Warning: Repo lock $1-$2 was not locked!" >&2 + warning "Repo lock $1-$2 was not locked!" restore_umask return 1 else @@ -153,7 +191,7 @@ getpkgname() { _name="$(_grep_pkginfo "$1" "^pkgname")" if [ -z "$_name" ]; then - echo "Error: Package '$1' has no pkgname in the PKGINFO. Fail!" >&2 + error "Package '$1' has no pkgname in the PKGINFO. Fail!" exit 1 fi @@ -166,7 +204,7 @@ getpkgver() { _ver="$(_grep_pkginfo "$1" "^pkgver")" if [ -z "$_ver" ]; then - echo "Error: Package '$1' has no pkgver in the PKGINFO. Fail!" >&2 + error "Package '$1' has no pkgver in the PKGINFO. Fail!" exit 1 fi @@ -175,10 +213,10 @@ getpkgver() { getpkgfile() { if [[ ${#} -ne 1 ]]; then - echo 'Error: No canonical package found!' >&2 + error 'No canonical package found!' exit 1 elif [ ! -f "${1}" ]; then - echo "Error: Package ${1} not found!" >&2 + error "Package ${1} not found!" exit 1 fi @@ -188,13 +226,13 @@ getpkgfile() { getpkgfiles() { local f if [ ! -z "$(echo ${@%\.*} | sed "s/ /\n/g" | sort | uniq -D)" ]; then - echo 'Error: Duplicate packages found!'>&2 + error 'Duplicate packages found!' exit 1 fi for f in ${@}; do if [ ! -f "${f}" ]; then - echo "Error: Package ${f} not found!" >&2 + error "Package ${f} not found!" exit 1 fi done @@ -208,7 +246,7 @@ check_pkg_arch () { _arch="$(_grep_pkginfo "$1" "^arch")" if [ -z "$_arch" ]; then - echo "Error: Package '$1' has no arch in the PKGINFO. Fail!" >&2 + error "Package '$1' has no arch in the PKGINFO. Fail!" return 1 fi if [ "$_arch" = "$2" ]; then -- cgit v1.2.3-24-g4f1b