From 853b60d7623ff7eacd0cf621a09f001f7f53f3a2 Mon Sep 17 00:00:00 2001 From: Charlotte Croce Date: Sun, 20 Apr 2025 18:03:55 -0400 Subject: [PATCH] add search CLI MVP, needs refactoring --- src/fylgja-cli.js | 45 +++++++++++++++++----- src/handlers/sigma/sigma_search_handler.js | 2 + 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/fylgja-cli.js b/src/fylgja-cli.js index db63a7d..3722877 100644 --- a/src/fylgja-cli.js +++ b/src/fylgja-cli.js @@ -569,19 +569,11 @@ async function processCommand(input) { } } -/** - * Custom respond function for handling results - * @param {string} action The action being performed - * @param {string} module The module being used - * @param {Array} params The parameters for the action - * @returns {Function} A respond function for handling results - */ function createRespondFunction(action, module, params) { // Keep track of whether we're waiting for results let isWaitingForResults = false; return async (response) => { - // Check if this is a progress message const isProgressMessage = typeof response === 'object' && response.text && @@ -604,12 +596,46 @@ function createRespondFunction(action, module, params) { // First check for the responseData property (directly from service) if (response.responseData) { + // Format the data using the appropriate formatter if (module === 'sigma') { let formattedData; if (action === 'search' || action === 'complexSearch') { - formattedData = formatSigmaSearchResults(response.responseData); + + /* + This conversion functionality exists because the Fylgja CLI's formatting + system expects search results in a specific structure with results and + totalCount properties, while the underlying sigma search service + returns results as a direct array of rule objects. This adapter pattern allows + the system to handle different response formats from various backend services + without requiring extensive changes to either the service layer or the presentation + layer. It essentially serves as a compatibility layer between components that were + likely developed independently or evolved at different times in the project's + lifecycle, maintaining backward compatibility while allowing for flexibility + in how data is processed throughout the application. + */ + + // Try to adapt data structure if needed + let dataToFormat = response.responseData; + + // If responseData is just an array, wrap it in proper structure + if (Array.isArray(dataToFormat)) { + dataToFormat = { + results: dataToFormat, + totalCount: dataToFormat.length + }; + } + // If missing totalCount but has pagination info, adapt + else if (dataToFormat.results && + !dataToFormat.totalCount && + dataToFormat.pagination && + dataToFormat.pagination.totalResults) { + dataToFormat.totalCount = dataToFormat.pagination.totalResults; + } + + formattedData = formatSigmaSearchResults(dataToFormat); + formatOutput(formattedData, 'search_results'); } else if (action === 'details') { formattedData = formatSigmaDetails(response.responseData); @@ -638,6 +664,7 @@ function createRespondFunction(action, module, params) { }; } + /** * Display help text */ diff --git a/src/handlers/sigma/sigma_search_handler.js b/src/handlers/sigma/sigma_search_handler.js index 2932b8d..7613239 100644 --- a/src/handlers/sigma/sigma_search_handler.js +++ b/src/handlers/sigma/sigma_search_handler.js @@ -147,6 +147,7 @@ const handleCommand = async (command, respond) => { // Respond with the search results await respond({ blocks: blocks, + responseData: searchResult.results, response_type: isEphemeral ? 'ephemeral' : 'in_channel' }); @@ -271,6 +272,7 @@ const handleComplexSearch = async (command, respond) => { // Respond with the search results await respond({ blocks: blocks, + responseData: searchResult.results, response_type: 'ephemeral' // Complex searches are usually more specific to the user });