diff --git a/internal/cli/app.go b/internal/cli/app.go index 008171b..9a8cf5f 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -530,8 +530,8 @@ func runStats(ctx context.Context, cfg config.Config, stdout io.Writer, args []s return writeJSON(stdout, stats) } -func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, command string, args []string) error { - fs := flag.NewFlagSet(command, flag.ContinueOnError) +func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, queryKind, cmdName string, args []string) error { + fs := flag.NewFlagSet(cmdName, flag.ContinueOnError) fs.SetOutput(io.Discard) repoRootFlag := fs.String("repo-root", "", "repository root") queryFlag := fs.String("query", "", "query text") @@ -573,10 +573,10 @@ func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, c } defer app.Close() - switch command { + switch queryKind { case "find-symbol": if queryValue == "" { - return errors.New("usage: codegraph find-symbol [--limit N] [--offset N]") + return fmt.Errorf("usage: %s %s [--limit N] [--offset N]", appname.BinaryName, cmdName) } items, err := app.Query.FindSymbol(ctx, repoID, queryValue, *limit, *offset) if err != nil { @@ -585,7 +585,7 @@ func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, c return writeJSON(stdout, map[string]any{"matches": items}) case "search": if queryValue == "" { - return errors.New("usage: codegraph search [--limit N] [--offset N]") + return fmt.Errorf("usage: %s %s [--limit N] [--offset N]", appname.BinaryName, cmdName) } items, err := app.Query.SearchSymbols(ctx, repoID, queryValue, *limit, *offset) if err != nil { @@ -594,7 +594,7 @@ func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, c return writeJSON(stdout, map[string]any{"matches": items}) case "callers": if symbol == "" { - return errors.New("usage: codegraph callers --symbol [--limit N] [--offset N]") + return fmt.Errorf("usage: %s %s --symbol [--limit N] [--offset N]", appname.BinaryName, cmdName) } items, err := app.Query.FindCallers(ctx, repoID, symbol, 0, *limit, *offset) if err != nil { @@ -603,7 +603,7 @@ func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, c return writeJSON(stdout, map[string]any{"callers": items}) case "callees": if symbol == "" { - return errors.New("usage: codegraph callees --symbol [--limit N] [--offset N]") + return fmt.Errorf("usage: %s %s --symbol [--limit N] [--offset N]", appname.BinaryName, cmdName) } items, err := app.Query.FindCallees(ctx, repoID, symbol, 0, *limit, *offset) if err != nil { @@ -615,7 +615,7 @@ func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, c if symbol != "" { symbols = append(symbols, symbol) } else { - return errors.New("usage: codegraph impact [--symbol ]... [--file ]... [--depth N]") + return fmt.Errorf("usage: %s %s [--symbol ]... [--file ]... [--depth N]", appname.BinaryName, cmdName) } } data, err := app.Query.ImpactRadius(ctx, repoID, symbols, files, *depth) @@ -624,7 +624,7 @@ func runQueryCommand(ctx context.Context, cfg config.Config, stdout io.Writer, c } return writeJSON(stdout, data) default: - return fmt.Errorf("unknown query command %q", command) + return fmt.Errorf("unknown query command %q", queryKind) } } @@ -1191,6 +1191,7 @@ func printRootHelp(w io.Writer) { fmt.Fprintf(w, " %s help index\n", appname.BinaryName) fmt.Fprintf(w, " %s index .\n", appname.BinaryName) fmt.Fprintf(w, " %s stats .\n", appname.BinaryName) + fmt.Fprintf(w, " %s find_symbol . MySymbol\n", appname.BinaryName) fmt.Fprintf(w, " %s serve --repo-root .\n", appname.BinaryName) } diff --git a/internal/cli/app_test.go b/internal/cli/app_test.go index 2e3848e..694997d 100644 --- a/internal/cli/app_test.go +++ b/internal/cli/app_test.go @@ -267,11 +267,11 @@ func TestRunHelpCommandWithSubcommand(t *testing.T) { var out bytes.Buffer var errOut bytes.Buffer - if err := Run(context.Background(), []string{"help", "index"}, &out, &errOut); err != nil { - t.Fatalf("Run(help index) error = %v", err) + if err := Run(context.Background(), []string{"help", "find_symbol"}, &out, &errOut); err != nil { + t.Fatalf("Run(help find_symbol) error = %v", err) } - if got := out.String(); !strings.Contains(got, "Usage:") || !strings.Contains(got, "index ") { - t.Fatalf("help index output unexpected, output:\n%s", got) + if got := out.String(); !strings.Contains(got, "Usage:") || !strings.Contains(got, "find_symbol ") { + t.Fatalf("help find_symbol output unexpected, output:\n%s", got) } } @@ -284,11 +284,11 @@ func TestRunCommandHelpFlag(t *testing.T) { var out bytes.Buffer var errOut bytes.Buffer - if err := Run(context.Background(), []string{"find-symbol", ".", "--help"}, &out, &errOut); err != nil { - t.Fatalf("Run(find-symbol --help) error = %v", err) + if err := Run(context.Background(), []string{"find_symbol", ".", "--help"}, &out, &errOut); err != nil { + t.Fatalf("Run(find_symbol --help) error = %v", err) } - if got := out.String(); !strings.Contains(got, "Usage:") || !strings.Contains(got, "find-symbol ") { - t.Fatalf("find-symbol --help output unexpected, output:\n%s", got) + if got := out.String(); !strings.Contains(got, "Usage:") || !strings.Contains(got, "find_symbol ") { + t.Fatalf("find_symbol --help output unexpected, output:\n%s", got) } } diff --git a/internal/cli/commands.go b/internal/cli/commands.go index 57c492e..1063d2a 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -150,50 +150,71 @@ func newCommandList() []*command { }, }, { - name: "find-symbol", + name: "find_symbol", + aliases: []string{"find-symbol"}, description: "find symbols by name", - usageLines: []string{" find-symbol "}, + usageLines: []string{" find_symbol "}, flags: []commandFlag{ {name: "--limit", description: "limit results"}, {name: "--offset", description: "offset into result set"}, }, examples: []string{ - "codegraph find-symbol . HelloWorld", + "codegraph find_symbol . HelloWorld", }, run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { - return runQueryCommand(ctx, cfg, stdout, "find-symbol", args) + return runQueryCommand(ctx, cfg, stdout, "find-symbol", "find_symbol", args) }, }, { - name: "callers", + name: "find_callers", + aliases: []string{"callers"}, description: "find callers of a symbol", - usageLines: []string{" callers --symbol "}, + usageLines: []string{" find_callers --symbol "}, flags: []commandFlag{ {name: "--symbol", description: "symbol name to query (required)"}, {name: "--limit", description: "limit results"}, {name: "--offset", description: "offset into result set"}, }, examples: []string{ - "codegraph callers . --symbol HelloWorld", + "codegraph find_callers . --symbol HelloWorld", }, run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { - return runQueryCommand(ctx, cfg, stdout, "callers", args) + return runQueryCommand(ctx, cfg, stdout, "callers", "find_callers", args) }, }, { - name: "callees", + name: "find_callees", + aliases: []string{"callees"}, description: "find callees of a symbol", - usageLines: []string{" callees --symbol "}, + usageLines: []string{" find_callees --symbol "}, + flags: []commandFlag{ + {name: "--symbol", description: "symbol name to query (required)"}, + {name: "--limit", description: "limit results"}, + {name: "--offset", description: "offset into result set"}, + }, + examples: []string{ + "codegraph find_callees . --symbol HelloWorld", + }, run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { - return runQueryCommand(ctx, cfg, stdout, "callees", args) + return runQueryCommand(ctx, cfg, stdout, "callees", "find_callees", args) }, }, { - name: "impact", + name: "get_impact_radius", + aliases: []string{"impact"}, description: "compute impact radius", - usageLines: []string{" impact [--symbol ] [--file ]"}, + usageLines: []string{" get_impact_radius [--symbol ] [--file ]"}, + flags: []commandFlag{ + {name: "--symbol", description: "symbol name to query"}, + {name: "--file", description: "file path to query"}, + {name: "--depth", description: "limit traversal depth"}, + }, + examples: []string{ + "codegraph get_impact_radius . --symbol HelloWorld", + "codegraph get_impact_radius . --file main.go", + }, run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { - return runQueryCommand(ctx, cfg, stdout, "impact", args) + return runQueryCommand(ctx, cfg, stdout, "impact", "get_impact_radius", args) }, }, { @@ -201,7 +222,7 @@ func newCommandList() []*command { description: "search", usageLines: []string{" search "}, run: func(ctx context.Context, cfg config.Config, stdout, stderr io.Writer, args []string) error { - return runQueryCommand(ctx, cfg, stdout, "search", args) + return runQueryCommand(ctx, cfg, stdout, "search", "search", args) }, }, {