Merge pull request #208 from tobi/fix/json-output-and-index-paths

This commit is contained in:
Tobias Lütke 2026-02-17 20:27:49 -04:00 committed by GitHub
commit 1a67e1a093
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 5 deletions

View File

@ -58,7 +58,16 @@ let currentIndexName: string = "index";
* Config file will be ~/.config/qmd/{indexName}.yml
*/
export function setConfigIndexName(name: string): void {
currentIndexName = name;
// Resolve relative paths to absolute paths and sanitize for use as filename
if (name.includes('/')) {
const { resolve } = require('path');
const { cwd } = require('process');
const absolutePath = resolve(cwd(), name);
// Replace path separators with underscores to create a valid filename
currentIndexName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
} else {
currentIndexName = name;
}
}
function getConfigDir(): string {

View File

@ -120,7 +120,16 @@ function getDbPath(): string {
}
function setIndexName(name: string | null): void {
storeDbPathOverride = name ? getDefaultDbPath(name) : undefined;
let normalizedName = name;
// Normalize relative paths to prevent malformed database paths
if (name && name.includes('/')) {
const { resolve } = require('path');
const { cwd } = require('process');
const absolutePath = resolve(cwd(), name);
// Replace path separators with underscores to create a valid filename
normalizedName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
}
storeDbPathOverride = normalizedName ? getDefaultDbPath(normalizedName) : undefined;
// Reset open handle so next use opens the new index
closeDb();
}
@ -1959,7 +1968,11 @@ function search(query: string, opts: OutputOptions): void {
closeDb();
if (resultsWithContext.length === 0) {
console.log("No results found.");
if (opts.format === "json") {
console.log("[]");
} else {
console.log("No results found.");
}
return;
}
outputResults(resultsWithContext, query, opts);
@ -2013,7 +2026,11 @@ async function vectorSearch(query: string, opts: OutputOptions, _model: string =
closeDb();
if (results.length === 0) {
console.log("No results found.");
if (opts.format === "json") {
console.log("[]");
} else {
console.log("No results found.");
}
return;
}
@ -2072,7 +2089,11 @@ async function querySearch(query: string, opts: OutputOptions, _embedModel: stri
closeDb();
if (results.length === 0) {
console.log("No results found.");
if (opts.format === "json") {
console.log("[]");
} else {
console.log("No results found.");
}
return;
}