Skip to content

Commit

Permalink
attempt fix apt failures:
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 27, 2024
1 parent 25fa014 commit b96a30f
Showing 1 changed file with 75 additions and 71 deletions.
146 changes: 75 additions & 71 deletions commands/sudo-helper
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,18 @@ function sudo_helper() (
fi
}

# determine technique
local technique
if __command_exists -- sudo; then
technique='sudo'
elif __command_exists -- doas; then
technique='doas'
else
technique='none'
fi

# try without sudo
if [[ $option_required == 'no' ]]; then
if [[ $option_required == 'no' || $technique == 'none' ]]; then
__wrap "${option_cmd[@]}"
return
elif [[ $option_optional == 'yes' ]]; then
Expand All @@ -175,88 +185,82 @@ function sudo_helper() (
fi
fi

# prep sudo execution
command_style='sudo'
local sudo_cmd=() home=''
if [[ $option_required == 'yes' ]]; then
if [[ $technique == 'sudo' ]]; then
sudo_cmd+=('sudo')
# don't use is-directory, is-missing, __sudo_mkdirp, as they will cause infinite recursion
if __command_exists -- sudo; then
sudo_cmd+=('sudo')
if [[ -n $option_user ]]; then
sudo_cmd+=("--user=$option_user")
# should coincide with setup-environment-commands
if [[ -d /Users ]]; then
home="/Users/$option_user"
elif [[ -d '/home' ]]; then
home="/home/$option_user"
elif [[ -d "/$option_user" ]]; then
HOME="/$option_user"
fi
elif [[ -n $HOME ]]; then
home="$HOME"
fi

if [[ -n $option_group ]]; then
sudo_cmd+=("--group=$option_group")
if [[ -n $option_user ]]; then
sudo_cmd+=("--user=$option_user")
# should coincide with setup-environment-commands
if [[ -d /Users ]]; then
home="/Users/$option_user"
elif [[ -d '/home' ]]; then
home="/home/$option_user"
elif [[ -d "/$option_user" ]]; then
HOME="/$option_user"
fi
elif [[ -n $HOME ]]; then
home="$HOME"
fi

if [[ -n $home ]]; then
if [[ ! -e $home && ! -L $home ]]; then
sudo mkdir -p "$home"
fs-own --quiet --user="$option_user" --group="$option_group" -- "$home"
fi
sudo_cmd+=('--set-home')
fi
if [[ -n $option_group ]]; then
sudo_cmd+=("--group=$option_group")
fi

if [[ $option_inherit == 'yes' ]]; then
sudo_cmd+=(
'--preserve-env'
'env'
"PATH=$PATH"
)
if [[ -n $home ]]; then
if [[ ! -e $home && ! -L $home ]]; then
sudo mkdir -p "$home"
fs-own --quiet --user="$option_user" --group="$option_group" -- "$home"
fi
sudo_cmd+=('--set-home')
fi

# check if password is required
# [sudo --validate] doesn't work on multipass, so just use [true] as a no-op
if ! sudo --non-interactive true &>/dev/null; then
# password is required, let the user know what they are being prompted for
# [--code=*] better than [echo-style --element/="$(echo-escape-command -- "${sudo_cmd[@]}" | echo-join ' ' --stdin)"] as that is too verbose
# use [--tty] to ensure the prompt is not hidden when otherwise hiding sudo output, such as in [command-working]
# while it would be nice to clear lines, we cannot know how many lines to clear as they could require multiple attempts to enter the password
if [[ -n $option_reason ]]; then
echo-style --tty --notice="$option_reason"
else
echo-style --tty --notice='Your sudo/root/login password is required to execute the command:'
fi
option_wrap='yes'
fi
if [[ $option_inherit == 'yes' ]]; then
sudo_cmd+=(
'--preserve-env'
'env'
"PATH=$PATH"
)
fi

sudo_cmd+=("${option_cmd[@]}")
elif __command_exists -- doas; then
# https://man.archlinux.org/man/doas.1.en
sudo_cmd+=('doas')
if [[ -n $option_user ]]; then
sudo_cmd+=(
'-u'
"$option_user"
)
fi
if [[ -n $option_group ]]; then
help 'doas does not support --group'
fi
if [[ $option_inherit == 'yes' ]]; then
sudo_cmd+=(
'env'
"DOROTHY=$DOROTHY"
"PATH=$PATH"
)
# check if password is required
# [sudo --validate] doesn't work on multipass, so just use [true] as a no-op
if ! sudo --non-interactive true &>/dev/null; then
# password is required, let the user know what they are being prompted for
# [--code=*] better than [echo-style --element/="$(echo-escape-command -- "${sudo_cmd[@]}" | echo-join ' ' --stdin)"] as that is too verbose
# use [--tty] to ensure the prompt is not hidden when otherwise hiding sudo output, such as in [command-working]
# while it would be nice to clear lines, we cannot know how many lines to clear as they could require multiple attempts to enter the password
if [[ -n $option_reason ]]; then
echo-style --tty --notice="$option_reason"
else
echo-style --tty --notice='Your sudo/root/login password is required to execute the command:'
fi
else
# sudo/doas does not exist, probably not needed
option_required='no'
option_wrap='yes'
fi
elif [[ $technique == 'doas' ]]; then
# https://man.archlinux.org/man/doas.1.en
sudo_cmd+=('doas')
if [[ -n $option_user ]]; then
sudo_cmd+=(
'-u'
"$option_user"
)
fi
if [[ -n $option_group ]]; then
help 'doas does not support --group'
fi
if [[ $option_inherit == 'yes' ]]; then
sudo_cmd+=(
'env'
"DOROTHY=$DOROTHY"
"PATH=$PATH"
)
fi
fi

# try sudo
command_style='sudo'
# run with sudo
__wrap "${sudo_cmd[@]}" "${option_cmd[@]}"
return
)
Expand Down

0 comments on commit b96a30f

Please sign in to comment.