initial testing for list-usb-drives

This commit is contained in:
2025-08-19 01:43:29 -04:00
parent 65760006ba
commit eecef04065
3 changed files with 83 additions and 0 deletions

View File

@@ -43,4 +43,40 @@ final: prev: {
}
);
};
list-usb-drives = prev.writeShellApplication {
name = "list-usb-drives";
runtimeInputs = with prev; [
findutils
gawk
coreutils
gnugrep
util-linux
];
excludeShellChecks = [
"SC2086"
"SC2157"
"SC2155"
];
text = ''
# Allow overriding the disk-by-id directory for testing
DISK_BY_ID_DIR="''${LIST_USB_DRIVES_DISK_DIR:-/dev/disk/by-id}"
# Mock lsblk for testing
if [ -n "''${LIST_USB_DRIVES_TEST_MODE:-}" ]; then
lsblk() {
echo "''$LIST_USB_DRIVES_MOCK_DATA" | tr '|' '\n' | while IFS=: read -r pattern response; do
case "$(basename "$3")" in *"$pattern"*) echo "$response"; return ;; esac
done || echo "UNKNOWN_MODEL UNKNOWN_SERIAL"
}
fi
# Scan for USB devices in the specified directory
if [ -d "$DISK_BY_ID_DIR" ]; then
find "$DISK_BY_ID_DIR" -name "usb*" | grep -v "part[0-9]\$" | while read -r drive; do lsblk -no model,serial "$drive" | head -n1 | tr -d '\n' | tr " " "_" && echo -e " $(echo \"$drive\" | cut -d':' -f2-)"; done | column -t --table-columns=DRIVE,BAY | sort -n -k 2
fi
'';
};
}