add prompt verification
This commit is contained in:
parent
3830c28b84
commit
f46f7f83b0
1 changed files with 39 additions and 19 deletions
|
@ -5,12 +5,40 @@ eprintln() {
|
|||
cat <<<"$@" 1>&2
|
||||
}
|
||||
|
||||
join_by() {
|
||||
local d=${1-} f=${2-}
|
||||
if shift 2; then
|
||||
printf %s "$f" "${@/#/$d}"
|
||||
fi
|
||||
}
|
||||
|
||||
prompt() {
|
||||
variable=$1
|
||||
default=$2
|
||||
prompt=$3
|
||||
echo "$prompt"
|
||||
variable="$1"
|
||||
shift
|
||||
default="$1"
|
||||
shift
|
||||
prompt="$1"
|
||||
shift
|
||||
requiments=("$@")
|
||||
|
||||
[[ -z "${requiments[*]}" ]] && {
|
||||
printf "<prompt> requires an array of requirements"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [[ "${requiments[0]}" == "null" ]]; then
|
||||
printf "\n%s\n" "$prompt"
|
||||
read -rep "> " -i "$default" "${variable?}"
|
||||
else
|
||||
# shellcheck disable=SC2086,SC2048
|
||||
printf "\n%s (Accepted values: %s)\n" "$prompt" "$(join_by ' | ' ${requiments[*]})"
|
||||
while :; do
|
||||
read -rep "> " -i "$default" "${variable?}"
|
||||
[[ "${requiments[0]}" == "null" ]] && break
|
||||
[[ -n ${!variable} && ${requiments[*]} =~ ${!variable} ]] && break
|
||||
printf "Invalid option\n"
|
||||
done
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
|
@ -35,31 +63,23 @@ _is_uefi=$([[ -f /sys/firmware/efi/fw_platform_size ]] && echo 1 || echo 0)
|
|||
|
||||
## Disk info
|
||||
mapfile -t devices < <(lsblk --paths --output NAME,SiZE --noheadings --nodeps)
|
||||
|
||||
paste <(printf "%s\n" "${!devices[@]}") <(printf "%s\n" "${devices[@]}")
|
||||
prompt root_drive "" "Which device do you want to be root drive?"
|
||||
|
||||
prompt want_home "" "Do you want to store home on another drive? (0 | 1)"
|
||||
prompt root_drive "" "Which device do you want to be root drive?" "${!devices[@]}"
|
||||
|
||||
prompt want_home "" "Do you want to store home on another drive?" 0 1
|
||||
|
||||
if [[ "${want_home?}" -eq 1 ]]; then
|
||||
prompt home_drive "" "Which device do you want to be home drive?"
|
||||
prompt home_fmt "" "Do you want to format home? (0|1)"
|
||||
prompt home_drive "" "Which device do you want to be home drive?" "${!devices[@]}"
|
||||
prompt home_fmt "" "Do you want to format home?" 0 1
|
||||
fi
|
||||
|
||||
prompt want_swap "" "Do you want a swap space? (0 | 1)"
|
||||
prompt want_swap "" "Do you want a swap space?" 0 1
|
||||
|
||||
if [[ "${want_swap?}" -eq 1 ]]; then
|
||||
prompt swap_size "2g" "Swap size. Default is 2g. (accepting k/m/g/e/p suffix)"
|
||||
prompt swap_size "2g" "Swap size. Default is 2g. (accepting k/m/g/e/p suffix)" null
|
||||
fi
|
||||
|
||||
# Summary of what is going to happen and user confirmation
|
||||
|
||||
# Pre-installation
|
||||
part_boot=",+1G,C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
|
||||
part_root=","
|
||||
root_dev=$(echo ${devices[$root_drive]} | cut -d' ' -f 1)
|
||||
[[ "${want_home:-0}" -eq 1 ]] &&
|
||||
home_dev=$(echo ${devices[$home_drive]} | cut -d' ' -f 1)
|
||||
|
||||
## Partition disk(s)
|
||||
printf "label: GPT\n$part_boot\n$part_root" | sfdisk "$root_dev"
|
||||
|
|
Loading…
Reference in a new issue