about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-12-06 18:27:17 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-12-06 18:27:17 +0100
commite346e22d8fb15ffd099939c4f8052bcb02f8960d (patch)
treec6c2f42d671ce10af3c2c0538d3187660105c922
parent0dd716d6cfb1a32f5fd5dff8ec0047df6a90dca6 (diff)
downloadxtools-e346e22d8fb15ffd099939c4f8052bcb02f8960d.tar.gz
xtools-e346e22d8fb15ffd099939c4f8052bcb02f8960d.tar.xz
xtools-e346e22d8fb15ffd099939c4f8052bcb02f8960d.zip
xi: refactor
Suggestions by kerframil:

- Fix the original su invocation bug. The original approach is veering very
  close to eval territory. Let's just not overload SUDO quite so hard and
  keep it coherent.
- Use id -u (posix) instead of whoami (not posix).
- Rather than try to make a scalar behave as if it were an array (it's not)
  then unsafely expand it, compose the positional parameter list as needed.
  Note that XBPS_BINPKGS and BRANCH are now expanded safely in the course of
  doing so.
- Don't include improperly formed repository paths as parameters if git fails
  or prints nothing.
- Have su pass sh as the zeroeth arg, not a dash so that, if an error in the
  shell context ever happens again, the resulting message will be less
      confusing. As an aside, if you want the environment to be as it normally
  would be for the root user, there should be a dash (or -l) before -c, not
  after --. Could help protect against issues of environmental pollution down
  the line.
- Don't squash the exit status of the first call to xbps-install in the case
  that it's not zero and also not 16.
- Make the fallback invocation be contingent upon -u xbps succeeding. Note
  that the exit status of either will be properly conveyed by the script i.e.
  we don't have to capture the exit value there.
-rw-r--r--[-rwxr-xr-x]xi67
1 files changed, 42 insertions, 25 deletions
diff --git a/xi b/xi
index 0541b3c..df328f1 100755..100644
--- a/xi
+++ b/xi
@@ -1,38 +1,55 @@
 #!/bin/sh
 # xi PKGS... - like xbps-install -S, but take cwd repo and sudo/su into account
 
-BRANCH=$(git symbolic-ref -q --short HEAD 2>/dev/null)
 if [ -n "$XBPS_HOSTDIR" ]; then
 	XBPS_BINPKGS="$XBPS_HOSTDIR/binpkgs"
 else
 	XBPS_DISTDIR="$(xdistdir 2>/dev/null)" || XBPS_DISTDIR=.
 	XBPS_BINPKGS="$XBPS_DISTDIR/hostdir/binpkgs"
 fi
-ADDREPO="
-	--repository=$XBPS_BINPKGS/$BRANCH
-	--repository=$XBPS_BINPKGS/$BRANCH/nonfree
-	--repository=$XBPS_BINPKGS/$BRANCH/multilib
-	--repository=$XBPS_BINPKGS/$BRANCH/multilib/nonfree
-	--repository=$XBPS_BINPKGS/$BRANCH/debug
-	--repository=$XBPS_BINPKGS
-	--repository=$XBPS_BINPKGS/nonfree
-	--repository=$XBPS_BINPKGS/multilib
-	--repository=$XBPS_BINPKGS/multilib/nonfree
-	--repository=$XBPS_BINPKGS/debug
-"
 
-SUDO=
-if command -v sudo >/dev/null &&
-   sudo -l | grep -q -e ' ALL$' -e xbps-install; then
-	SUDO=sudo
-elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then
-	SUDO=doas
-elif [ "$(whoami)" != root ]; then
-	SUDO='su root -c '\''"$@"'\'' -- -'
+set -- \
+	--repository="$XBPS_BINPKGS" \
+	--repository="$XBPS_BINPKGS/nonfree" \
+	--repository="$XBPS_BINPKGS/multilib" \
+	--repository="$XBPS_BINPKGS/multilib/nonfree" \
+	--repository="$XBPS_BINPKGS/debug" \
+	"$@"
+
+if BRANCH=$(git symbolic-ref -q --short HEAD 2>/dev/null) && [ -n "$BRANCH" ]; then
+	set -- \
+		--repository="$XBPS_BINPKGS/$BRANCH" \
+		--repository="$XBPS_BINPKGS/$BRANCH/nonfree" \
+		--repository="$XBPS_BINPKGS/$BRANCH/multilib" \
+		--repository="$XBPS_BINPKGS/$BRANCH/multilib/nonfree" \
+		--repository="$XBPS_BINPKGS/$BRANCH/debug" \
+		"$@"
 fi
 
-$SUDO xbps-install $ADDREPO -S "$@"
-if [ $? -eq 16 ]; then
-	$SUDO xbps-install -u xbps
-	$SUDO xbps-install $ADDREPO -S "$@"
+which_sudo() {
+	if command -v sudo >/dev/null && sudo -l | grep -q -e ' ALL$' -e xbps-install; then
+		echo sudo
+	elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then
+		echo doas
+	elif [ "$(id -u)" != 0 ]; then
+		echo su
+	fi
+}
+
+do_install() {
+	if [ "$SUDO" = su ]; then
+		su root -c 'xbps-install "$@"' -- sh "$@"
+	else
+		$SUDO xbps-install "$@"
+	fi
+}
+
+SUDO=$(which_sudo)
+do_install -S "$@"
+status=$?
+if [ $status -eq 16 ]; then
+	do_install -u xbps &&
+	do_install -S "$@"
+else
+	exit $status
 fi