-
Notifications
You must be signed in to change notification settings - Fork 0
cli: introduce minimal command registry for dispatch #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ coverage.out | |
| *.swo | ||
| TODO.md | ||
| GEMINI.md | ||
| ANALYSIS.md | ||
| codegraph | ||
| codegraph.exe | ||
| codegraph.sqlite | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/isink17/codegraph/internal/config" | ||
| ) | ||
|
|
||
| type command struct { | ||
| name string | ||
| aliases []string | ||
| description string | ||
| usageLines []string | ||
| run func(context.Context, config.Config, io.Writer, io.Writer, []string) error | ||
| } | ||
|
|
||
| var ( | ||
| commandList = newCommandList() | ||
| commandByName = newCommandRegistry(commandList) | ||
| ) | ||
|
|
||
| func lookupCommand(name string) (*command, bool) { | ||
| c, ok := commandByName[name] | ||
| return c, ok | ||
| } | ||
|
|
||
| func newCommandRegistry(cmds []*command) map[string]*command { | ||
| reg := map[string]*command{} | ||
|
|
||
| for _, c := range cmds { | ||
| registerCommand(reg, c) | ||
| } | ||
|
|
||
| return reg | ||
| } | ||
|
|
||
| func registerCommand(reg map[string]*command, c *command) { | ||
| if c == nil { | ||
| panic("command registry: nil command") | ||
| } | ||
| if c.name == "" { | ||
| panic("command registry: empty command name") | ||
| } | ||
| registerKey := func(key string) { | ||
| if key == "" { | ||
| panic(fmt.Sprintf("command registry: empty key for command %q", c.name)) | ||
| } | ||
| if prev, exists := reg[key]; exists { | ||
| panic(fmt.Sprintf("command registry: duplicate key %q for command %q (already used by %q)", key, c.name, prev.name)) | ||
| } | ||
| reg[key] = c | ||
| } | ||
|
|
||
| registerKey(c.name) | ||
| for _, a := range c.aliases { | ||
| registerKey(a) | ||
| } | ||
| } | ||
|
|
||
| func newCommandList() []*command { | ||
| return []*command{ | ||
| { | ||
| name: "install", | ||
| description: "install codegraph", | ||
| usageLines: []string{" install"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runInstall(stdout) | ||
| }, | ||
| }, | ||
| { | ||
| name: "index", | ||
| description: "index a repository", | ||
| usageLines: []string{" index <repo-path>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runIndex(ctx, cfg, stdout, args, false) | ||
| }, | ||
| }, | ||
| { | ||
| name: "update", | ||
| description: "incrementally update an index", | ||
| usageLines: []string{ | ||
| " update <repo-path>", | ||
| " add --jsonl for streaming line-delimited JSON events", | ||
| }, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runIndex(ctx, cfg, stdout, args, true) | ||
| }, | ||
| }, | ||
| { | ||
| name: "stats", | ||
| description: "show index stats", | ||
| usageLines: []string{" stats <repo-path>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runStats(ctx, cfg, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "find-symbol", | ||
| description: "find symbols by name", | ||
| usageLines: []string{" find-symbol <repo-path> <query>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runQueryCommand(ctx, cfg, stdout, "find-symbol", args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "callers", | ||
| description: "find callers of a symbol", | ||
| usageLines: []string{" callers <repo-path> --symbol <name>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runQueryCommand(ctx, cfg, stdout, "callers", args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "callees", | ||
| description: "find callees of a symbol", | ||
| usageLines: []string{" callees <repo-path> --symbol <name>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runQueryCommand(ctx, cfg, stdout, "callees", args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "impact", | ||
| description: "compute impact radius", | ||
| usageLines: []string{" impact <repo-path> [--symbol <name>] [--file <path>]"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runQueryCommand(ctx, cfg, stdout, "impact", args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "search", | ||
| description: "search", | ||
| usageLines: []string{" search <repo-path> <query>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runQueryCommand(ctx, cfg, stdout, "search", args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "doctor", | ||
| description: "run diagnostics", | ||
| usageLines: []string{ | ||
| " doctor", | ||
| " add --fix for non-destructive autofixes", | ||
| }, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runDoctor(stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "config", | ||
| description: "config commands", | ||
| usageLines: []string{ | ||
| " config <show|edit-path|validate|init>", | ||
| " config init [--repo PATH] [--force]", | ||
| }, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runConfig(cfg, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "benchmark", | ||
| description: "benchmarks", | ||
| usageLines: []string{" benchmark [--count N] [--benchtime DURATION] [--save-baseline]"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runBenchmark(ctx, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "serve", | ||
| description: "start MCP server", | ||
| usageLines: []string{" serve --repo-root <repo-path>"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runServe(ctx, cfg, stdout, stderr, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "watch", | ||
| description: "watch repository and update index", | ||
| usageLines: []string{ | ||
| " watch <repo-path>", | ||
| " add --jsonl for streaming line-delimited JSON events", | ||
| }, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runWatch(ctx, cfg, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "graph", | ||
| description: "graph commands", | ||
| usageLines: []string{" graph export <repo-path> [--format json|dot]"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runGraph(ctx, cfg, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "clean", | ||
| description: "clean index data", | ||
| usageLines: []string{" clean [repo-path] [--vacuum]"}, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runClean(ctx, cfg, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "affected-tests", | ||
| description: "find affected tests", | ||
| usageLines: []string{ | ||
| " affected-tests [--repo-root PATH] [--stdin] [--json] [--limit N] <file>...", | ||
| " find tests affected by changed files; pipe from git diff --name-only", | ||
| }, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runAffectedTests(ctx, cfg, stdout, args) | ||
| }, | ||
| }, | ||
| { | ||
| name: "visualize", | ||
| description: "generate interactive graph HTML", | ||
| usageLines: []string{ | ||
| " visualize [--repo-root PATH] [--symbol NAME] [--depth N] [--output FILE]", | ||
| " interactive D3.js graph visualization; opens browser or writes HTML file", | ||
| }, | ||
| run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { | ||
| return runVisualize(ctx, cfg, stdout, args) | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRegisterCommandPanicsOnDuplicate(t *testing.T) { | ||
| reg := map[string]*command{} | ||
| registerCommand(reg, &command{name: "a"}) | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Fatalf("expected panic on duplicate key, got nil") | ||
| } | ||
| }() | ||
| registerCommand(reg, &command{name: "a"}) | ||
| } | ||
|
|
||
| func TestRegisterCommandPanicsOnAliasDuplicate(t *testing.T) { | ||
| reg := map[string]*command{} | ||
| registerCommand(reg, &command{name: "a", aliases: []string{"x"}}) | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Fatalf("expected panic on duplicate alias key, got nil") | ||
| } | ||
| }() | ||
| registerCommand(reg, &command{name: "b", aliases: []string{"x"}}) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.