[pacman-dev] [PATCHv2 08/10] contrib: add colored output to bacman

William Giokas 1007380 at gmail.com
Sun Mar 3 13:48:23 EST 2013


Rewrote all messages into output_format.sh formats. QUIET is only kind
of supported, as some messages are multiline warning/error mesages, with
every line but the first being `plain`, and those would get hidden if
QUIET is set.

Signed-off-by: William Giokas <1007380 at gmail.com>
---
 contrib/bacman.sh.in | 62 ++++++++++++++++++++++++++++------------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/contrib/bacman.sh.in b/contrib/bacman.sh.in
index 512de63..6bd684a 100644
--- a/contrib/bacman.sh.in
+++ b/contrib/bacman.sh.in
@@ -26,13 +26,16 @@ shopt -s nullglob
 
 declare -r myname='bacman'
 declare -r myver='@PACKAGE_VERSION@'
+USE_COLOR='y'
+
+m4_include(../scripts/library/output_format.sh)
 
 #
 # User Friendliness
 #
 usage() {
 	echo "This program recreates a package using pacman's db and system files"
-	echo "Usage:   $myname <installed package name>"
+	echo "Usage:   $myname [-m|--no-color] <installed package name>"
 	echo "Example: $myname kernel26"
 }
 
@@ -41,6 +44,13 @@ version() {
 	echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>'
 }
 
+if [[ $1 == "--no-color" || $1 == "-m" ]]; then
+	USE_COLOR='n'
+	shift
+fi
+
+m4_include(../scripts/library/term_colors.sh)
+
 if (( $# != 1 )); then
 	usage
 	exit 1
@@ -59,14 +69,13 @@ fi
 #
 if (( EUID )); then
 	if [[ -f /usr/bin/fakeroot ]]; then
-		echo "Entering fakeroot environment"
+		msg "Entering fakeroot environment"
 		export INFAKEROOT="1"
 		/usr/bin/fakeroot -u -- "$0" "$@"
 		exit $?
 	else
-		echo "WARNING: installing fakeroot or running $myname as root is required to"
-		echo "         preserve the ownership permissions of files in some packages"
-		echo ""
+		warning "installing fakeroot or running $myname as root is required to"
+		plain   "         preserve the ownership permissions of files in some packages\n"
 	fi
 fi
 
@@ -74,7 +83,7 @@ fi
 # Setting environmental variables
 #
 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
-	echo "ERROR: unable to read @sysconfdir@/pacman.conf"
+	error "unable to read @sysconfdir@/pacman.conf"
 	exit 1
 fi
 
@@ -82,7 +91,7 @@ eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
 pac_db="${DBPath:- at localstatedir@/lib/pacman/}/local"
 
 if [[ ! -r @sysconfdir@/makepkg.conf ]]; then
-	echo "ERROR: unable to read @sysconfdir@/makepkg.conf"
+	error "unable to read @sysconfdir@/makepkg.conf"
 	exit 1
 fi
 
@@ -102,34 +111,34 @@ pkg_namver=("${pkg_dir[@]##*/}")
 # Checks everything is in place
 #
 if [[ ! -d $pac_db ]]; then
-	echo "ERROR: pacman database directory ${pac_db} not found"
+	error "pacman database directory ${pac_db} not found"
 	exit 1
 fi
 
 if (( ${#pkg_dir[@]} != 1 )); then
-	printf "ERROR: %d entries for package %s found in pacman database\n" \
+	error "%d entries for package %s found in pacman database" \
 		${#pkg_dir[@]} "${pkg_name}"
-	printf "%s\n" "${pkg_dir[@]}"
+	msg2 "%s" "${pkg_dir[@]}"
 	exit 1
 fi
 
 if [[ ! -d $pkg_dir ]]; then
-	printf "ERROR: package %s is found in pacman database,\n" "${pkg_name}"
-	printf "       but \`%s' is not a directory\n" "${pkg_dir}"
+	error "package %s is found in pacman database," "${pkg_name}"
+	plain "       but \`%s' is not a directory" "${pkg_dir}"
 	exit 1
 fi
 
 #
 # Begin
 #
-echo "Package: ${pkg_namver}"
+msg "Package: ${pkg_namver}"
 work_dir=$(mktemp -d --tmpdir bacman.XXXXXXXXXX)
 cd "$work_dir" || exit 1
 
 #
 # File copying
 #
-echo "Copying package files..."
+msg2 "Copying package files..."
 
 cat "$pkg_dir"/files |
 while read i; do
@@ -150,17 +159,14 @@ while read i; do
 
 				# Workaround to bsdtar not reporting a missing file as an error
 				if ! [[ -e $work_dir/$i || -L $work_dir/$i ]]; then
-					echo ""
-					echo "ERROR: unable to add /$i to the package"
-					echo "       If your user does not have permssion to read this file then"
-					echo "       you will need to run $myname as root"
+					error "unable to add /$i to the package"
+					plain "       If your user does not have permssion to read this file then"
+					plain "       you will need to run $myname as root"
 					rm -rf "$work_dir"
 					exit 1
 				fi
 			else
-				echo ""
-				echo "WARNING: package file /$i is missing"
-				echo ""
+				warning "package file /$i is missing"
 			fi
 			;;
 	esac
@@ -178,7 +184,7 @@ pkg_size=$(du -sk | awk '{print $1 * 1024}')
 # .PKGINFO stuff
 # TODO adopt makepkg's write_pkginfo() into this or scripts/library
 #
-echo Generating .PKGINFO metadata...
+msg2 "Generating .PKGINFO metadata..."
 echo "# Generated by $myname $myver"    > .PKGINFO
 if [[ $INFAKEROOT == "1" ]]; then
 	echo "# Using $(fakeroot -v)"    >> .PKGINFO
@@ -273,7 +279,7 @@ chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
 #
 # Generate the package
 #
-echo "Generating the package..."
+msg2 "Generating the package..."
 
 pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
 ret=0
@@ -287,20 +293,20 @@ case "$PKGEXT" in
 	*tar.xz)  xz -c -z - ;;
 	*tar.Z)   compress -c -f ;;
 	*tar)     cat ;;
-	*) echo "WARNING: '%s' is not a valid archive extension." \
-	"$PKGEXT" >&2; cat ;;
+	*) warning "'%s' is not a valid archive extension." \
+	"$PKGEXT"; cat ;;
 esac > "${pkg_file}"; ret=$?
 
 if (( ret )); then
-	echo "ERROR: unable to write package to $pkg_dest"
-	echo "       Maybe the disk is full or you do not have write access"
+	error "Unable to write package to $pkg_dest"
+	plain "       Maybe the disk is full or you do not have write access"
 	rm -rf "$work_dir"
 	exit 1
 fi
 
 rm -rf "$work_dir"
 
-echo Done
+msg "Done."
 
 exit 0
 
-- 
1.8.2.rc1.24.g06d67b8



More information about the pacman-dev mailing list