dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

asuser (1930B)


      1 #!/bin/sh
      2 set -e
      3 
      4 version=0.1.0
      5 
      6 die() {
      7 	printf '%s\n' "$1"
      8 	exit 1
      9 }
     10 
     11 help() {
     12 	echo "usage: ${0##*/} [OPTIONS] USER CMD"
     13 	echo "run CMD as USER."
     14 	echo
     15 	echo "OPTIONS are one or more of the following:"
     16 	echo "  -h   show this message"
     17 	echo "  -v   show version and license information"
     18 	echo "  -V   show verbose information during execution"
     19 	echo "  -t   transfer any output files from CMD to current directory"
     20 	echo "  -X   run graphical command (limited performance, limited X access)"
     21 	echo "  -Y   run graphical command (native performance, full X access)"
     22 	echo "  --   do not consider any following args as options"
     23 }
     24 
     25 show_version() {
     26 	echo "${0##*/} version $version"
     27 	echo "Licensed under the ISC License"
     28 	echo "written by Anders Damsgaard, anders@adamsgaard.dk"
     29 	echo "https://src.adamsgaard.dk/dotfiles"
     30 }
     31 
     32 if command -v doas >/dev/null 2>&1; then
     33 	doas=doas
     34 else
     35 	doas=sudo
     36 fi
     37 
     38 if [ $# -lt 1 ]; then
     39 	die 'error: no USER specified'
     40 	exit 1
     41 fi
     42 
     43 verbose=0
     44 transfer=0
     45 ssh=""
     46 while :; do
     47 	case "$1" in
     48 		-h)
     49 			help
     50 			exit 0
     51 			;;
     52 		-v)
     53 			show_version
     54 			exit 0
     55 			;;
     56 		-V)
     57 			verbose=1
     58 			;;
     59 		-t)
     60 			transfer=1
     61 			;;
     62 		-X)
     63 			ssh="-X"
     64 			;;
     65 		-Y)
     66 			ssh="-Y"
     67 			;;
     68 		--)
     69 			shift
     70 			break
     71 			;;
     72 		-?*)
     73 			die 'error: unknown option specified'
     74 			;;
     75 		*)
     76 			break;
     77 	esac
     78 	shift
     79 done
     80 
     81 run_as_user() {
     82 	u="$1"
     83 	shift
     84 	orig="$PWD"
     85 	if [ "$transfer" = 1 ]; then
     86 		d="$(mktemp -d)"
     87 		mkdir -p "$d"
     88 		chmod 777 "$d"
     89 		cd "$d"
     90 	else
     91 		d="$orig"
     92 	fi
     93 	if [ "$verbose" = 1 ]; then
     94 		printf 'executing "%s" as %s in %s\n' "$*" "$u" "$d"
     95 	fi
     96 	if [ -n "$ssh" ]; then
     97 		ssh "$ssh" $u@localhost "$*"
     98 	else
     99 		$doas -u "$u" sh -c "cd '$d' && \
    100 			PATH=/home/'$u'/.local/bin:/home/'$u'/bin:\$PATH && \
    101 			$*"
    102 	fi
    103 	if [ "$transfer" = 1 ]; then
    104 		if ls -lqA "$d" | grep -q .; then
    105 			if cp -prf "$d"/* "$orig"; then
    106 				rm -rf "$d"
    107 			else
    108 				die "could not transfer files from '$d' to '$orig'"
    109 			fi
    110 		fi
    111 	fi
    112 }
    113 
    114 run_as_user "$@"