From 95ea21b845ca8cf0ea3c5b73113a83e1f63f28b8 Mon Sep 17 00:00:00 2001 From: SvDp Date: Wed, 3 Dec 2025 15:25:33 +0530 Subject: [PATCH 1/2] feat: add --json option to search command --- src/commands/search.ts | 111 ++++++++++++++++++++++++++++------------- test/test.bats | 10 ++++ 2 files changed, 85 insertions(+), 36 deletions(-) diff --git a/src/commands/search.ts b/src/commands/search.ts index 9ef3338..b8b34c1 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -144,6 +144,7 @@ export const search: Command = new CommanderCommand("search") "Disable reranking of search results", parseBooleanEnv(process.env.MGREP_RERANK, true), // `true` here means that reranking is enabled by default ) + .option("--json", "Output results in JSON format", false) .argument("", "The pattern to search for") .argument("[path]", "The path to search in") .allowUnknownOption(true) @@ -157,6 +158,7 @@ export const search: Command = new CommanderCommand("search") sync: boolean; dryRun: boolean; rerank: boolean; + json: boolean; } = cmd.optsWithGlobals(); if (exec_path?.startsWith("--")) { exec_path = ""; @@ -202,44 +204,81 @@ export const search: Command = new CommanderCommand("search") ? exec_path : normalize(join(root, exec_path ?? "")); - let response: string; - if (!options.answer) { - const results = await store.search( - options.store, - pattern, - parseInt(options.maxCount, 10), - { rerank: options.rerank }, - { - all: [ - { - key: "path", - operator: "starts_with", - value: search_path, - }, - ], - }, - ); - response = formatSearchResponse(results, options.content); + if (options.json) { + if (!options.answer) { + const results = await store.search( + options.store, + pattern, + parseInt(options.maxCount, 10), + { rerank: options.rerank }, + { + all: [ + { + key: "path", + operator: "starts_with", + value: search_path, + }, + ], + }, + ); + console.log(JSON.stringify(results, null, 2)); + } else { + const results = await store.ask( + options.store, + pattern, + parseInt(options.maxCount, 10), + { rerank: options.rerank }, + { + all: [ + { + key: "path", + operator: "starts_with", + value: search_path, + }, + ], + }, + ); + console.log(JSON.stringify(results, null, 2)); + } } else { - const results = await store.ask( - options.store, - pattern, - parseInt(options.maxCount, 10), - { rerank: options.rerank }, - { - all: [ - { - key: "path", - operator: "starts_with", - value: search_path, - }, - ], - }, - ); - response = formatAskResponse(results, options.content); + let response: string; + if (!options.answer) { + const results = await store.search( + options.store, + pattern, + parseInt(options.maxCount, 10), + { rerank: options.rerank }, + { + all: [ + { + key: "path", + operator: "starts_with", + value: search_path, + }, + ], + }, + ); + response = formatSearchResponse(results, options.content); + } else { + const results = await store.ask( + options.store, + pattern, + parseInt(options.maxCount, 10), + { rerank: options.rerank }, + { + all: [ + { + key: "path", + operator: "starts_with", + value: search_path, + }, + ], + }, + ); + response = formatAskResponse(results, options.content); + } + console.log(response); } - - console.log(response); } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; console.error("Failed to search:", message); diff --git a/test/test.bats b/test/test.bats index fe91c21..96522d4 100755 --- a/test/test.bats +++ b/test/test.bats @@ -298,3 +298,13 @@ teardown() { assert_success refute_output --partial 'test-2.txt' } + +@test "Search with json output" { + run mgrep search --json test + + assert_success + assert_output --partial '"type": "text"' + assert_output --partial '"text":' + assert_output --partial '"metadata":' +} + From ddeb8e3d66576606803004560249ac23bc22995c Mon Sep 17 00:00:00 2001 From: SvDp Date: Wed, 3 Dec 2025 15:36:52 +0530 Subject: [PATCH 2/2] test: add comprehensive json output tests --- test/test.bats | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/test.bats b/test/test.bats index 96522d4..ea098f0 100755 --- a/test/test.bats +++ b/test/test.bats @@ -308,3 +308,20 @@ teardown() { assert_output --partial '"metadata":' } +@test "Search with json output and answer" { + run mgrep search --json --answer test + + assert_success + assert_output --partial '"answer":' + assert_output --partial '"sources":' + assert_output --partial '"type": "text"' +} + +@test "Search with json output and no results" { + run mgrep search --json "nonexistentstring12345" + + assert_success + assert_output --partial '"data": []' +} + +