Skip to content

Commit bef6eb7

Browse files
committed
Add URL-based indexing command to CLI
Adds command that auto-detects source type (GitHub, GitLab, Bitbucket, or website) from the URL and creates the appropriate source. Features: - Parses URL using parseSourceUrl() to determine source type - Supports --ref option to override URL-detected branch/tag - Supports -i/--index option to override default index name - Supports --store and --store-path options - Default index name derived from repo/project name - Graceful error handling for invalid URLs Agent-Id: agent-c9423996-94bb-4ab3-8311-ca0cc822da14
1 parent d4d00af commit bef6eb7

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/bin/cmd-index.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Indexer } from "../core/indexer.js";
77
import { Source } from "../sources/types.js";
88
import { FilesystemStore } from "../stores/filesystem.js";
99
import { getS3Config } from "../stores/s3-config.js";
10+
import { parseSourceUrl } from "../core/url-parser.js";
1011

1112
// Shared store options
1213
interface StoreOptions {
@@ -203,9 +204,74 @@ websiteCommand.action(async (options) => {
203204
}
204205
});
205206

207+
// URL-based indexing command (auto-detects source type)
208+
const urlCommand = new Command("url")
209+
.description("Index from a URL (auto-detects source type)")
210+
.argument("<url>", "URL of the repository or website to index")
211+
.option("--ref <ref>", "Branch, tag, or commit (overrides URL-detected ref)");
212+
addStoreOptions(urlCommand);
213+
urlCommand.action(async (url: string, options) => {
214+
try {
215+
// Parse the URL to determine source type and config
216+
const parsed = parseSourceUrl(url);
217+
const indexKey = options.index || parsed.defaultIndexName;
218+
219+
let source: Source;
220+
221+
switch (parsed.type) {
222+
case "github": {
223+
const { GitHubSource } = await import("../sources/github.js");
224+
const config = parsed.config as import("../sources/github.js").GitHubSourceConfig;
225+
source = new GitHubSource({
226+
...config,
227+
ref: options.ref || config.ref,
228+
});
229+
break;
230+
}
231+
case "gitlab": {
232+
const { GitLabSource } = await import("../sources/gitlab.js");
233+
const config = parsed.config as import("../sources/gitlab.js").GitLabSourceConfig;
234+
source = new GitLabSource({
235+
...config,
236+
ref: options.ref || config.ref,
237+
});
238+
break;
239+
}
240+
case "bitbucket": {
241+
const { BitBucketSource } = await import("../sources/bitbucket.js");
242+
const config = parsed.config as import("../sources/bitbucket.js").BitBucketSourceConfig;
243+
source = new BitBucketSource({
244+
...config,
245+
ref: options.ref || config.ref,
246+
});
247+
break;
248+
}
249+
case "website": {
250+
const { WebsiteSource } = await import("../sources/website.js");
251+
const config = parsed.config as import("../sources/website.js").WebsiteSourceConfig;
252+
source = new WebsiteSource(config);
253+
break;
254+
}
255+
default:
256+
throw new Error(`Unknown source type: ${parsed.type}`);
257+
}
258+
259+
const store = await createStore(options);
260+
await runIndex(source, store, indexKey, parsed.type);
261+
} catch (error) {
262+
if (error instanceof Error && error.message.includes("Invalid")) {
263+
console.error(`Error parsing URL: ${error.message}`);
264+
} else {
265+
console.error("Indexing failed:", error);
266+
}
267+
process.exit(1);
268+
}
269+
});
270+
206271
// Main index command
207272
export const indexCommand = new Command("index")
208273
.description("Index a data source")
274+
.addCommand(urlCommand)
209275
.addCommand(githubCommand)
210276
.addCommand(gitlabCommand)
211277
.addCommand(bitbucketCommand)

0 commit comments

Comments
 (0)