pacdiff: Add option to use sudo/sudoedit to manage files

Message ID 20210327182210.19257-1-danielrparks@gmail.com
State New
Headers show
Series pacdiff: Add option to use sudo/sudoedit to manage files | expand

Commit Message

Daniel Parks March 27, 2021, 6:22 p.m. UTC
This allows the user to run their diff program as their own user instead
of root. The advantage of this approach is that they can use their own
configuration for the editor or even use a GUI merge program such as
meld.

Signed-off-by: Daniel Parks <danielrparks@gmail.com>
---
 src/pacdiff.sh.in | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

Comments

Daniel M. Capella March 28, 2021, 6:44 a.m. UTC | #1
On March 27, 2021 2:22:11 PM EDT, Daniel Parks via pacman-contrib <pacman-contrib@lists.archlinux.org> wrote:
> This allows the user to run their diff program as their own user
> instead
> of root. The advantage of this approach is that they can use their own
> configuration for the editor or even use a GUI merge program such as
> meld.
> 
> Signed-off-by: Daniel Parks <danielrparks@gmail.com>
> ---
>  src/pacdiff.sh.in | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/src/pacdiff.sh.in b/src/pacdiff.sh.in
> index c4dbd89..aac88c8 100644
> --- a/src/pacdiff.sh.in
> +++ b/src/pacdiff.sh.in
> @@ -28,6 +28,7 @@ LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
>  diffprog=${DIFFPROG:-'vim -d'}
>  diffsearchpath=${DIFFSEARCHPATH:-/etc}
>  USE_COLOR='y'
> +SUDO=''
>  declare -a oldsaves
>  declare -i USE_FIND=0 USE_LOCATE=0 USE_PACDB=0 OUTPUTONLY=0
>  
> @@ -50,6 +51,7 @@ Search Options:     select one (default: --pacmandb)
>  General Options:
>    -o/--output       print files instead of merging them
>    --nocolor         remove colors from output
> +  -s/--sudo         use sudo to merge/remove files
>  
>  Environment Variables:
>    DIFFPROG          override the merge program: (default: 'vim -d')
> @@ -109,6 +111,8 @@ while [[ -n "$1" ]]; do
>  			OUTPUTONLY=1;;
>  		--nocolor)
>  			USE_COLOR='n';;
> +		-s|--sudo)
> +			SUDO=sudo;;
>  		-V|--version)
>  			version; exit 0;;
>  		-h|--help)
> @@ -168,25 +172,29 @@ while IFS= read -u 3 -r -d '' pacfile; do
>  	msg "%s file found for %s" "$file_type" "$file"
>  	if [ ! -f "$file" ]; then
>  		warning "$file does not exist"
> -		rm -iv "$pacfile"
> +		$SUDO rm -iv "$pacfile"
>  		continue
>  	fi
>  
>  	if cmp -s "$pacfile" "$file"; then
>  		msg2 "Files are identical, removing..."
> -		rm -v "$pacfile"
> +		$SUDO rm -v "$pacfile"
>  	else
> 		ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit:
> [v/s/r/o/q] " "$file_type" "$file_type"
>  		while read c; do
>  			case $c in
>  				q|Q) exit 0;;
> -				r|R) rm -v "$pacfile"; break ;;
> -				o|O) mv -v "$pacfile" "$file"; break ;;
> +				r|R) $SUDO rm -v "$pacfile"; break ;;
> +				o|O) $SUDO mv -v "$pacfile" "$file"; break ;;
>  				v|V)
> -					$diffprog "$pacfile" "$file"
> +					if [[ -n "$SUDO" ]]; then
> +						SUDO_EDITOR="$diffprog" sudoedit "$pacfile" "$file"
> +					else
> +						$diffprog "$pacfile" "$file"
> +					fi
>  					if cmp -s "$pacfile" "$file"; then
>  						msg2 "Files are identical, removing..."
> -						rm -v "$pacfile"
> +						$SUDO rm -v "$pacfile"
>  						break
>  					fi
> 					ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit:
> [v/s/r/o/q] " "$file_type" "$file_type";

Thank you. Rebased, added support for the new merge feature, and pushed.

--
Best,
Daniel <https://danielcapella.com>

Patch

diff --git a/src/pacdiff.sh.in b/src/pacdiff.sh.in
index c4dbd89..aac88c8 100644
--- a/src/pacdiff.sh.in
+++ b/src/pacdiff.sh.in
@@ -28,6 +28,7 @@  LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
 diffprog=${DIFFPROG:-'vim -d'}
 diffsearchpath=${DIFFSEARCHPATH:-/etc}
 USE_COLOR='y'
+SUDO=''
 declare -a oldsaves
 declare -i USE_FIND=0 USE_LOCATE=0 USE_PACDB=0 OUTPUTONLY=0
 
@@ -50,6 +51,7 @@  Search Options:     select one (default: --pacmandb)
 General Options:
   -o/--output       print files instead of merging them
   --nocolor         remove colors from output
+  -s/--sudo         use sudo to merge/remove files
 
 Environment Variables:
   DIFFPROG          override the merge program: (default: 'vim -d')
@@ -109,6 +111,8 @@  while [[ -n "$1" ]]; do
 			OUTPUTONLY=1;;
 		--nocolor)
 			USE_COLOR='n';;
+		-s|--sudo)
+			SUDO=sudo;;
 		-V|--version)
 			version; exit 0;;
 		-h|--help)
@@ -168,25 +172,29 @@  while IFS= read -u 3 -r -d '' pacfile; do
 	msg "%s file found for %s" "$file_type" "$file"
 	if [ ! -f "$file" ]; then
 		warning "$file does not exist"
-		rm -iv "$pacfile"
+		$SUDO rm -iv "$pacfile"
 		continue
 	fi
 
 	if cmp -s "$pacfile" "$file"; then
 		msg2 "Files are identical, removing..."
-		rm -v "$pacfile"
+		$SUDO rm -v "$pacfile"
 	else
 		ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
 		while read c; do
 			case $c in
 				q|Q) exit 0;;
-				r|R) rm -v "$pacfile"; break ;;
-				o|O) mv -v "$pacfile" "$file"; break ;;
+				r|R) $SUDO rm -v "$pacfile"; break ;;
+				o|O) $SUDO mv -v "$pacfile" "$file"; break ;;
 				v|V)
-					$diffprog "$pacfile" "$file"
+					if [[ -n "$SUDO" ]]; then
+						SUDO_EDITOR="$diffprog" sudoedit "$pacfile" "$file"
+					else
+						$diffprog "$pacfile" "$file"
+					fi
 					if cmp -s "$pacfile" "$file"; then
 						msg2 "Files are identical, removing..."
-						rm -v "$pacfile"
+						$SUDO rm -v "$pacfile"
 						break
 					fi
 					ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type";