Options parser, --userspec

This commit is contained in:
DarkCat09 2024-04-10 09:32:08 +04:00
parent 2931330ad4
commit c2d852bfb0
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3

35
run.sh
View file

@ -1,7 +1,11 @@
#!/usr/bin/env bash #!/usr/bin/env bash
help () { help () {
echo 'dchr-run <chroot-dir> [command]' echo 'dchr-run [options] <chroot-dir> [command]'
echo '-- options:'
echo ' -u uid[:gid] invoke command in chroot as a different user'
# TODO:
#echo ' -x11 apply options for correctly starting GUI apps'
echo 'https://dchr.dc09.ru' echo 'https://dchr.dc09.ru'
} }
@ -28,22 +32,43 @@ setup () {
runcmd () { runcmd () {
echo -e '-- \e[1;32mOK\e[0m -- running chroot' echo -e '-- \e[1;32mOK\e[0m -- running chroot'
chrdir="$1" userspec="--userspec=$1"
shift chrdir="$2"
shift 2
chrcmd=("$@") chrcmd=("$@")
chroot "$chrdir" "${chrcmd[@]}" chroot "$userspec" "$chrdir" "${chrcmd[@]}"
} }
main () { main () {
[[ "$1" == "--help" || "$1" == "" ]] && help && exit 0 [[ "$1" == "--help" || "$1" == "" ]] && help && exit 0
userspec="$(id -u):$(id -g)"
while [[ "$1" == -* ]]
do
case "$1" in
-u)
userspec="$2"
shift 2
;;
#-x11);; TODO
*)
echo -ne '-- \e[1mInvalid option:\e[0m '
echo "$1"
echo '-- Use ./ or full path if your chroot-dir starts with dash (-)'
help
exit 2
;;
esac
done
chrdir=$(readlink -f "$1") chrdir=$(readlink -f "$1")
shift shift
trap "clean \"$chrdir\"" EXIT trap "clean \"$chrdir\"" EXIT
setup "$chrdir" setup "$chrdir"
runcmd "$chrdir" "$@" runcmd "$userspec" "$chrdir" "$@"
exit $? exit $?
} }