Cache lspci output in get_storage_controllers

The lspci command is now called only once on first invocation of
get_storage_controllers, with results cached in LSPCI_CACHE.

Subsequent calls from different layout generators (10bay, large1,
micro) reuse the cached output, reducing subprocess overhead.

Also added function documentation.

Fixes: #17

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 11:42:12 -05:00
parent b61a9305ab
commit 59ecb3998b

View File

@@ -520,12 +520,29 @@ declare -A CHASSIS_TYPES=(
# Core Functions
#------------------------------------------------------------------------------
# Cache for lspci output (populated on first call)
LSPCI_CACHE=""
#------------------------------------------------------------------------------
# get_storage_controllers
#
# Returns a formatted list of storage controllers found via lspci.
# Uses cached output if available to avoid redundant lspci calls.
#
# Output Format: " PCI_ADDR: DESCRIPTION" (one per line)
#------------------------------------------------------------------------------
get_storage_controllers() {
# Returns a formatted list of storage controllers (HBAs, SATA, NVMe)
lspci 2>/dev/null | grep -iE "SAS|SATA|RAID|Mass storage|NVMe" | while read -r line; do
pci_addr=$(echo "$line" | awk '{print $1}')
# Cache lspci output on first call
if [[ -z "$LSPCI_CACHE" ]]; then
LSPCI_CACHE="$(lspci 2>/dev/null | grep -iE "SAS|SATA|RAID|Mass storage|NVMe")"
fi
# Format and return cached output
echo "$LSPCI_CACHE" | while read -r line; do
[[ -z "$line" ]] && continue
pci_addr="$(echo "$line" | awk '{print $1}')"
# Get short description (strip PCI address)
desc=$(echo "$line" | sed 's/^[0-9a-f:.]\+ //')
desc="$(echo "$line" | sed 's/^[0-9a-f:.]\+ //')"
echo " $pci_addr: $desc"
done
}