Implement disk dialogue in a single script

This commit is contained in:
Manuel Thalmann 2024-09-22 18:24:07 +02:00
parent 985a408900
commit 9060a6f304
4 changed files with 54 additions and 102 deletions

View file

@ -0,0 +1,48 @@
#!/bin/env fish
function chooseDisk -a outFile message
set -l disks
lsblk -do NAME,SIZE,TYPE | while read disk
set -l diskInfo (string split -n " " $disk)
if contains "$diskInfo[3]" "TYPE" "disk"
set -a disks "$diskInfo"
end
end
set -l diskCount (count $disks)
set -l padding (math (string length "$diskCount") + 1)
if [ "$diskCount" -gt 0 ]
while true
echo "$message"
set -l prefix
for i in (seq "$diskCount")
if [ "$i" -eq 1 ]
set prefix ""
else
set prefix "$(math "$i" - 1):"
end
printf "%"$padding"s %s\n" "$prefix" "$disks[$i]"
end
read -lP "Your choice: " choice
if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ]
set -l disk (string split -n " " "$disks[$(math "$choice" + 1)]")
echo "/dev/$disk[1]" > $outFile
return
else
echo "The specified choice \"$choice\" is invalid!"
end
end
else
echo "No valid disk found!" >&2
end
return 1
end
chooseDisk $argv

View file

@ -1,59 +0,0 @@
#!/bin/bash
function chooseDisk() {
local -n result="$1";
local message="$2";
local choice;
local disk;
local -a disks;
local i;
disks=();
while read disk
do
local -a diskInfo;
diskInfo=($disk);
if [ "${diskInfo[2]}" == "TYPE" ] || [ "${diskInfo[2]}" == "disk" ]
then
disks+=("$disk");
fi;
done < <(lsblk -do NAME,SIZE,TYPE);
diskCount="$(expr "${#disks[@]}" - 1)";
padding="${#diskCount}";
if [ "$diskCount" -gt 0 ]
then
while true
do
echo "$message";
for i in $(seq 0 "$(expr "$diskCount")")
do
local index;
if [ "$i" -eq 0 ]
then
index="";
else
index="$i:";
fi;
printf "%$(expr "${diskCount}" + 1)s ${disks[$i]}" "$index";
echo "";
done;
read -p "Your choice: " choice;
if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ]
then
disk=(${disks[$choice]});
result="/dev/${disk[0]}";
return;
else
>&2 echo "The specified choice \"$choice\" is invalid!";
fi;
done;
else
>&2 echo "No valid disk found!";
fi;
}

View file

@ -78,8 +78,9 @@
diskVar = ''''${${diskVarName}}'';
diskSelector = ''
. ${./choose-disk.sh};
chooseDisk ${diskVarName} "Which disk do you wish to install the OS on?";
result="$(mktemp)"
fish ${./choose-disk.fish} "$result" "Which disk do you wish to install the OS on?"
${diskVarName}="$(cat "$result")"
'';
partitions = lib.lists.sortOn

View file

@ -1,44 +1,6 @@
#!/bin/env fish
function chooseDisk -a result message
set -l disks
lsblk -do NAME,SIZE,TYPE | while read disk
set -l diskInfo (string split -n " " $disk)
if contains "$diskInfo[3]" "TYPE" "disk"
set -a disks "$diskInfo"
end
end
set -l diskCount (count $disks)
set -l padding (math (string length "$diskCount") + 1)
if [ "$diskCount" -gt 0 ]
while true
echo "$message"
set -l prefix
for i in (seq "$diskCount")
if [ "$i" -eq 1 ]
set prefix ""
else
set prefix "$(math "$i" - 1):"
end
printf "%"$padding"s %s\n" "$prefix" "$disks[$i]"
end
read -lP "Your choice: " choice
if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ]
set -l disk (string split -n " " "$disks[$(math "$choice" + 1)]")
set -g "$result" "/dev/$disk[1]"
return
else
echo "The specified choice \"$choice\" is invalid!" >&2
end
end
else
echo "No valid disk found!" >&2
end
set -l file (mktemp)
fish "$(status dirname)/../../lib/modules/partition/choose-disk.fish" "$file" "$message"
set -g "$result" (cat "$file")
end