[v2] checkupdates: add option to download updates using -Swu

Message ID 20190424174444.25275-1-eschwartz@archlinux.org
State Accepted, archived
Headers show
Series [v2] checkupdates: add option to download updates using -Swu | expand

Commit Message

Eli Schwartz April 24, 2019, 5:44 p.m. UTC
This is safer than -Sywu, and should be included here for the same
reason we wish to encourage people not to use -Sy && -Qu.

Since an option was added, we now need option parsing, so I converted
over to using parseopts.

Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
---

v2: do not rely on libmakepkg, yet -- cf. discussion re: datarootdir vs.
getting libmakepkgdir from pkg-config.

 src/checkupdates.sh.in | 61 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 50 insertions(+), 11 deletions(-)

Comments

Johannes Löthberg April 24, 2019, 7:27 p.m. UTC | #1
LGTM, thanks.

Excerpts from Eli Schwartz's message of April 24, 2019 19:44:
> This is safer than -Sywu, and should be included here for the same
> reason we wish to encourage people not to use -Sy && -Qu.
> 
> Since an option was added, we now need option parsing, so I converted
> over to using parseopts.
> 
> Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
> ---
> 
> v2: do not rely on libmakepkg, yet -- cf. discussion re: datarootdir vs.
> getting libmakepkgdir from pkg-config.
> 
>  src/checkupdates.sh.in | 61 ++++++++++++++++++++++++++++++++++--------
>  1 file changed, 50 insertions(+), 11 deletions(-)
> 
> diff --git a/src/checkupdates.sh.in b/src/checkupdates.sh.in
> index 1d01fda..8eb6e21 100644
> --- a/src/checkupdates.sh.in
> +++ b/src/checkupdates.sh.in
> @@ -21,19 +21,51 @@
>  declare -r myname='checkupdates'
>  declare -r myver='@PACKAGE_VERSION@'
>  
> +DOWNLOAD_CACHE=0
> +
>  m4_include(../lib/output_format.sh)
>  m4_include(../lib/term_colors.sh)
> +m4_include(../lib/parseopts.sh)
> +
> +usage() {
> +	cat << __EOF__
> +${myname} v${myver}
> +
> +Safely print a list of pending updates
> +
> +Usage: ${myname} [options]
> +
> +  Options:
> +    -d, --download        download pending updates to the pacman cache.
> +    -h, --help            display this help message and exit.
> +
> +Note: Export the "CHECKUPDATES_DB" variable to change the path of the temporary database.
>  
> -if (( $# > 0 )); then
> -	echo "${myname} v${myver}"
> -	echo
> -	echo "Safely print a list of pending updates"
> -	echo
> -	echo "Usage: ${myname}"
> -	echo
> -	echo 'Note: Export the "CHECKUPDATES_DB" variable to change the path of the temporary database.'
> -	exit 0
> +__EOF__
> +}
> +
> +OPT_SHORT='dh'
> +OPT_LONG=('download' 'help')
> +
> +if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
> +	exit 1
>  fi
> +set -- "${OPTRET[@]}"
> +unset OPT_SHORT OPT_LONG OPTRET
> +
> +while :; do
> +	case $1 in
> +		-d|--download)
> +			DOWNLOAD_CACHE=1 ;;
> +		-h|--help)
> +			usage
> +			exit 0 ;;
> +		--)
> +			shift
> +			break ;;
> +	esac
> +	shift
> +done
>  
>  if ! type -P fakeroot >/dev/null; then
>  	error 'Cannot find the fakeroot binary.'
> @@ -57,8 +89,15 @@ if ! fakeroot -- pacman -Sy --dbpath "$CHECKUPDATES_DB" --logfile /dev/null &> /
>         error 'Cannot fetch updates'
>         exit 1
>  fi
> -pacman -Qu --dbpath "$CHECKUPDATES_DB" 2> /dev/null | grep -v '\[.*\]'
> +mapfile -t updates < <(pacman -Qu --dbpath "$CHECKUPDATES_DB" 2> /dev/null | grep -v '\[.*\]')
>  
> -exit 0
> +if (( ${#updates[@]} )); then
> +	printf '%s\n' "${updates[@]}"
> +	if (( DOWNLOAD_CACHE )); then
> +		sudo pacman -Sw --noconfirm "${updates[@]%% *}" --dbpath "$CHECKUPDATES_DB" --logfile /dev/null
> +	fi
> +else
> +	exit 1
> +fi
>  
>  # vim: set noet:
> -- 
> 2.21.0
>

Patch

diff --git a/src/checkupdates.sh.in b/src/checkupdates.sh.in
index 1d01fda..8eb6e21 100644
--- a/src/checkupdates.sh.in
+++ b/src/checkupdates.sh.in
@@ -21,19 +21,51 @@ 
 declare -r myname='checkupdates'
 declare -r myver='@PACKAGE_VERSION@'
 
+DOWNLOAD_CACHE=0
+
 m4_include(../lib/output_format.sh)
 m4_include(../lib/term_colors.sh)
+m4_include(../lib/parseopts.sh)
+
+usage() {
+	cat << __EOF__
+${myname} v${myver}
+
+Safely print a list of pending updates
+
+Usage: ${myname} [options]
+
+  Options:
+    -d, --download        download pending updates to the pacman cache.
+    -h, --help            display this help message and exit.
+
+Note: Export the "CHECKUPDATES_DB" variable to change the path of the temporary database.
 
-if (( $# > 0 )); then
-	echo "${myname} v${myver}"
-	echo
-	echo "Safely print a list of pending updates"
-	echo
-	echo "Usage: ${myname}"
-	echo
-	echo 'Note: Export the "CHECKUPDATES_DB" variable to change the path of the temporary database.'
-	exit 0
+__EOF__
+}
+
+OPT_SHORT='dh'
+OPT_LONG=('download' 'help')
+
+if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
+	exit 1
 fi
+set -- "${OPTRET[@]}"
+unset OPT_SHORT OPT_LONG OPTRET
+
+while :; do
+	case $1 in
+		-d|--download)
+			DOWNLOAD_CACHE=1 ;;
+		-h|--help)
+			usage
+			exit 0 ;;
+		--)
+			shift
+			break ;;
+	esac
+	shift
+done
 
 if ! type -P fakeroot >/dev/null; then
 	error 'Cannot find the fakeroot binary.'
@@ -57,8 +89,15 @@  if ! fakeroot -- pacman -Sy --dbpath "$CHECKUPDATES_DB" --logfile /dev/null &> /
        error 'Cannot fetch updates'
        exit 1
 fi
-pacman -Qu --dbpath "$CHECKUPDATES_DB" 2> /dev/null | grep -v '\[.*\]'
+mapfile -t updates < <(pacman -Qu --dbpath "$CHECKUPDATES_DB" 2> /dev/null | grep -v '\[.*\]')
 
-exit 0
+if (( ${#updates[@]} )); then
+	printf '%s\n' "${updates[@]}"
+	if (( DOWNLOAD_CACHE )); then
+		sudo pacman -Sw --noconfirm "${updates[@]%% *}" --dbpath "$CHECKUPDATES_DB" --logfile /dev/null
+	fi
+else
+	exit 1
+fi
 
 # vim: set noet: