Repository navigation
Expand file tree
/
Copy pathcommands.go
More file actions
171 lines (147 loc) · 5.85 KB
/
Copy pathcommands.go
File metadata and controls
171 lines (147 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package contexting
import (
"fmt"
"github.com/spf13/cobra"
)
var configPath string
var noConfigPrompt bool
var createConfig bool
var configJustCreated bool
var agentMode bool
var logToStderr bool
var offlineMode bool
// agentUnsafeCommands lists commands that modify project state.
// When --agent is set, these are blocked to prevent accidental damage.
var agentUnsafeCommands = map[string]bool{
"init": true,
"watch": true,
"sync": true,
"clean": true,
"mcp": true,
}
// anchorsConfigToRoot reports whether a command resolves its default config
// path against a project root argument (see resolveStartupConfigPath). The
// bootstrap prompt must use the same anchoring so it offers to create the
// config where the command will actually load it.
func anchorsConfigToRoot(name string) bool {
return name == "watch"
}
func commandHasAncestor(cmd *cobra.Command, name string) bool {
for current := cmd; current != nil; current = current.Parent() {
if current.Name() == name {
return true
}
}
return false
}
func isAgentUnsafeCommand(cmd *cobra.Command) bool {
for current := cmd; current != nil; current = current.Parent() {
if agentUnsafeCommands[current.Name()] {
return true
}
}
return false
}
func NewRootCommand() *cobra.Command {
rootCmd := &cobra.Command{
Use: "ctxt",
Version: currentVersion(),
Short: "ctxt — concept-based code search for AI-assisted development",
Long: `ctxt builds a searchable index of your codebase — files, symbols (functions/classes/types), and LLM-generated synonyms — letting AI assistants and CLI users find files by what they DO, not what they're named.
Key features:
• Tree-sitter AST extraction — captures nested methods/functions in Python, JS/TS, Rust, Svelte, Astro
• Import-aware synonyms — LLM sees ESM imports to generate domain-accurate names (e.g., "clerk webhook handler" for route.ts)
• Hybrid search — ripgrep content fallback when index results are sparse
• MCP server — integrate with Claude Desktop, Cursor, and other AI assistants
• 7 bench engines — compare ctxt against find, fd, grep, rg
Get started:
ctxt init . # Build index for current project
ctxt watch . # Watch for changes, keep index live
ctxt mcp # Start MCP server for AI assistants
ctxt search-hints "auth middleware" # Find auth-related files
ctxt examples # See more usage patterns`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Skip auto-prompt for help/config management flows.
if agentMode && isAgentUnsafeCommand(cmd) {
return fmt.Errorf("command %q is not available in agent mode; run without --agent or use a human-approved tool", cmd.Name())
}
if cmd.Name() == "help" || cmd.Name() == "version" || cmd.Name() == "config" || cmd.Name() == "clean" || cmd.Name() == "status" || commandHasAncestor(cmd, "mcp") || cmd.Parent() != nil && cmd.Parent().Name() == "config" {
return nil
}
if noConfigPrompt {
return nil
}
// Commands that anchor the default config to a project root argument
// must bootstrap against that same resolved path, otherwise the
// prompt would offer to create the config in the caller's directory
// while the command loads it from the project root.
rootArg := ""
if anchorsConfigToRoot(cmd.Name()) && len(args) > 0 {
rootArg = args[0]
}
resolved, err := resolveStartupConfigPath(configPath, rootArg, cmd.Flags().Changed("config"))
if err != nil {
return err
}
created, err := ensureStarterConfigPrompt(resolved, createConfig)
configJustCreated = created
return err
},
}
rootCmd.PersistentFlags().StringVar(&configPath, "config", ".ctxt/ctx_config.toml", "Path to ctxt TOML config")
rootCmd.PersistentFlags().BoolVar(&noConfigPrompt, "no-config-prompt", false, "Disable interactive starter config prompt when config is missing")
rootCmd.PersistentFlags().BoolVar(&createConfig, "create-config", false, "Auto-create starter config when missing (non-interactive)")
rootCmd.PersistentFlags().BoolVar(&agentMode, "agent", false, "Agent mode: blocks state-modifying commands (init/watch/sync/clean)")
rootCmd.PersistentFlags().BoolVar(&offlineMode, "offline", false, "Disable all LLM requests, including configured API keys")
// Command groups for organized help output
rootCmd.AddGroup(&cobra.Group{ID: "setup", Title: "Setup:"})
rootCmd.AddGroup(&cobra.Group{ID: "query", Title: "Query:"})
rootCmd.AddGroup(&cobra.Group{ID: "analysis", Title: "Analysis:"})
rootCmd.AddGroup(&cobra.Group{ID: "maintenance", Title: "Maintenance:"})
rootCmd.AddGroup(&cobra.Group{ID: "other", Title: "Other:"})
// Setup
initCmd := newInitCommand()
initCmd.GroupID = "setup"
rootCmd.AddCommand(initCmd)
watchCmd := newWatchCommand()
watchCmd.GroupID = "setup"
rootCmd.AddCommand(watchCmd)
syncCmd := newSyncCommand()
syncCmd.GroupID = "setup"
rootCmd.AddCommand(syncCmd)
// Query
mcpCmd := newMCPCommand()
mcpCmd.GroupID = "query"
rootCmd.AddCommand(mcpCmd)
searchCmd := newSearchCommand()
searchCmd.GroupID = "query"
rootCmd.AddCommand(searchCmd)
// Analysis
benchCmd := newBenchCommand()
benchCmd.GroupID = "analysis"
rootCmd.AddCommand(benchCmd)
evalCmd := newEvalCommand()
evalCmd.GroupID = "analysis"
rootCmd.AddCommand(evalCmd)
// Maintenance
doctorCmd := newDoctorCommand()
doctorCmd.GroupID = "maintenance"
rootCmd.AddCommand(doctorCmd)
statusCmd := newStatusCommand()
statusCmd.GroupID = "maintenance"
rootCmd.AddCommand(statusCmd)
configCmd := newConfigCommand()
configCmd.GroupID = "maintenance"
rootCmd.AddCommand(configCmd)
cleanCmd := newCleanCommand()
cleanCmd.GroupID = "maintenance"
rootCmd.AddCommand(cleanCmd)
// Other
examplesCmd := newExamplesCommand()
examplesCmd.GroupID = "other"
rootCmd.AddCommand(examplesCmd)
versionCmd := newVersionCommand()
versionCmd.GroupID = "other"
rootCmd.AddCommand(versionCmd)
return rootCmd
}