Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 <repo-path> <query> [--limit N] [--offset N]")
return fmt.Errorf("usage: %s %s <repo-path> <query> [--limit N] [--offset N]", appname.BinaryName, cmdName)
}
items, err := app.Query.FindSymbol(ctx, repoID, queryValue, *limit, *offset)
if err != nil {
Expand All @@ -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 <repo-path> <query> [--limit N] [--offset N]")
return fmt.Errorf("usage: %s %s <repo-path> <query> [--limit N] [--offset N]", appname.BinaryName, cmdName)
}
items, err := app.Query.SearchSymbols(ctx, repoID, queryValue, *limit, *offset)
if err != nil {
Expand All @@ -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 <repo-path> --symbol <name> [--limit N] [--offset N]")
return fmt.Errorf("usage: %s %s <repo-path> --symbol <name> [--limit N] [--offset N]", appname.BinaryName, cmdName)
}
items, err := app.Query.FindCallers(ctx, repoID, symbol, 0, *limit, *offset)
if err != nil {
Expand All @@ -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 <repo-path> --symbol <name> [--limit N] [--offset N]")
return fmt.Errorf("usage: %s %s <repo-path> --symbol <name> [--limit N] [--offset N]", appname.BinaryName, cmdName)
}
items, err := app.Query.FindCallees(ctx, repoID, symbol, 0, *limit, *offset)
if err != nil {
Expand All @@ -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 <repo-path> [--symbol <name>]... [--file <path>]... [--depth N]")
return fmt.Errorf("usage: %s %s <repo-path> [--symbol <name>]... [--file <path>]... [--depth N]", appname.BinaryName, cmdName)
}
}
data, err := app.Query.ImpactRadius(ctx, repoID, symbols, files, *depth)
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}

Expand Down
16 changes: 8 additions & 8 deletions internal/cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <repo-path>") {
t.Fatalf("help index output unexpected, output:\n%s", got)
if got := out.String(); !strings.Contains(got, "Usage:") || !strings.Contains(got, "find_symbol <repo-path> <query>") {
t.Fatalf("help find_symbol output unexpected, output:\n%s", got)
}
}

Expand All @@ -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 <repo-path> <query>") {
t.Fatalf("find-symbol --help output unexpected, output:\n%s", got)
if got := out.String(); !strings.Contains(got, "Usage:") || !strings.Contains(got, "find_symbol <repo-path> <query>") {
t.Fatalf("find_symbol --help output unexpected, output:\n%s", got)
}
}

Expand Down
51 changes: 36 additions & 15 deletions internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,58 +150,79 @@ func newCommandList() []*command {
},
},
{
name: "find-symbol",
name: "find_symbol",
aliases: []string{"find-symbol"},
Comment on lines +153 to +154

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The introduction of underscores in command names (e.g., find_symbol, find_callers, get_impact_radius) is inconsistent with the existing affected-tests command and departs from common CLI naming conventions which typically use hyphens as word separators. Unless there is a specific requirement for underscores (e.g., matching a specific external API), it is recommended to use hyphens to maintain a consistent user interface across the tool.

Suggested change
name: "find_symbol",
aliases: []string{"find-symbol"},
name: "find-symbol",
aliases: []string{"find_symbol"},

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's intentional

description: "find symbols by name",
usageLines: []string{" find-symbol <repo-path> <query>"},
usageLines: []string{" find_symbol <repo-path> <query>"},
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 <repo-path> --symbol <name>"},
usageLines: []string{" find_callers <repo-path> --symbol <name>"},
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 <repo-path> --symbol <name>"},
usageLines: []string{" find_callees <repo-path> --symbol <name>"},
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 <repo-path> [--symbol <name>] [--file <path>]"},
usageLines: []string{" get_impact_radius <repo-path> [--symbol <name>] [--file <path>]"},
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)
},
},
{
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)
return runQueryCommand(ctx, cfg, stdout, "search", "search", args)
},
},
{
Expand Down
Loading