Skip to content

Commit

Permalink
handle environment variables whin passed in args
Browse files Browse the repository at this point in the history
  • Loading branch information
ubermanu committed Nov 5, 2023
1 parent 14e8b54 commit 9490375
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
22 changes: 19 additions & 3 deletions sudo.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# and then runs doas with the mapped arguments.
# See https://github.com/ubermanu/sudoas

version="1.1"

# shellcheck disable=SC2034
yellow=$(tput setaf 3)
end=$(tput sgr0)
Expand All @@ -15,6 +17,8 @@ TEST=${TEST:-0}

# Parse arguments
doas_args=()
doas_env=()
parse_env=1

while [[ $# -gt 0 ]]; do
key="$1"
Expand All @@ -26,7 +30,7 @@ while [[ $# -gt 0 ]]; do
;;
-V | --version)
echo "${yellow}THIS IS A COMPATIBILITY SCRIPT FOR DOAS.${end}"
echo "Version: 1.0"
echo "Version: ${version}"
exit 0
;;
-n | --non-interactive)
Expand Down Expand Up @@ -67,8 +71,15 @@ while [[ $# -gt 0 ]]; do
;;
*)
# If not a sudo argument, assume it's a command and arguments
doas_args+=("$@")
break
# or an environment variable
if [ $parse_env -eq 1 ] && [[ $key == *=* ]]; then
doas_env+=("$key")
shift
else
parse_env=0
doas_args+=("$@")
break
fi
;;
esac
done
Expand All @@ -86,4 +97,9 @@ if [[ $TEST -eq 1 ]]; then
exit 0
fi

# Run doas with environment variables and arguments
for env_var in "${doas_env[@]}"; do
export "$env_var"
done

doas "${doas_args[@]}"
6 changes: 6 additions & 0 deletions tests/test_sudo.bats
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ export TEST=1
[ "$status" -eq 0 ]
[ "$output" = "echo hello" ]
}

@test "move environment variables before the command" {
run ./sudo.bash TERMINFO=/usr/lib/kitty/terminfo echo hello
[ "$status" -eq 0 ]
[ "$output" = "echo hello" ]
}

0 comments on commit 9490375

Please sign in to comment.