Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bin/idstack-learnings-search
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ for m in matches[-limit:]:
" 2>/dev/null || {
# Fallback: basic grep
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
cat $SOURCES 2>/dev/null | grep -i -- "$KEYWORD" | tail -n "$LIMIT"
elif [ -n "$TYPE" ]; then
cat $SOURCES 2>/dev/null | grep "\"type\":\"$TYPE\"" | tail -"$LIMIT"
cat $SOURCES 2>/dev/null | grep -- "\"type\":\"$TYPE\"" | tail -n "$LIMIT"
else
cat $SOURCES 2>/dev/null | tail -"$LIMIT"
cat $SOURCES 2>/dev/null | tail -n "$LIMIT"
fi
}
else
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
cat $SOURCES 2>/dev/null | grep -i -- "$KEYWORD" | tail -n "$LIMIT"
elif [ -n "$TYPE" ]; then
cat $SOURCES 2>/dev/null | grep "\"type\":\"$TYPE\"" | tail -"$LIMIT"
cat $SOURCES 2>/dev/null | grep -- "\"type\":\"$TYPE\"" | tail -n "$LIMIT"
else
cat $SOURCES 2>/dev/null | tail -"$LIMIT"
cat $SOURCES 2>/dev/null | tail -n "$LIMIT"
fi
fi
Comment on lines 72 to 90

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The fallback basic grep logic is duplicated across the Python execution failure block (lines 74-80) and the else block when Python is not available (lines 83-89). This duplication makes the script harder to maintain and prone to errors if the fallback logic needs to be updated in the future (as seen in this PR where changes had to be applied in both places).

We can eliminate this duplication by setting a flag RUN_FALLBACK=1 and executing the fallback logic once after the if-else block.

" 2>/dev/null || RUN_FALLBACK=1
else
  RUN_FALLBACK=1
fi

if [ "$RUN_FALLBACK" = "1" ]; then
  if [ -n "$KEYWORD" ]; then
    cat $SOURCES 2>/dev/null | grep -i -- "$KEYWORD" | tail -n "$LIMIT"
  elif [ -n "$TYPE" ]; then
    cat $SOURCES 2>/dev/null | grep -- "\"type\":\"$TYPE\"" | tail -n "$LIMIT"
  else
    cat $SOURCES 2>/dev/null | tail -n "$LIMIT"
  fi
fi