forked from cybrota/recaller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
497 lines (431 loc) · 16.8 KB
/
Copy pathmain.go
File metadata and controls
497 lines (431 loc) · 16.8 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright 2025 Naren Yellavula
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
func main() {
asciiLogo := `
██████╗ ███████╗ ██████╗ █████╗ ██╗ ██╗ ███████╗██████╗
██╔══██╗██╔════╝██╔════╝██╔══██╗██║ ██║ ██╔════╝██╔══██╗
██████╔╝█████╗ ██║ ███████║██║ ██║ █████╗ ██████╔╝
██╔══██╗██╔══╝ ██║ ██╔══██║██║ ██║ ██╔══╝ ██╔══██╗
██║ ██║███████╗╚██████╗██║ ██║███████╗███████╗███████╗██║ ██║
╚═╝ ╚═╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝
Blazing-fast command history search with instant documentation and terminal execution [Version: %s%s%s]
Copyright @ Naren Yellavula (Please give us a star ⭐ here: https://github.com/cybrota/recaller)
`
asciiLogo = fmt.Sprintf(asciiLogo, Green, version, Reset)
var cmdRun = &cobra.Command{
Use: "run",
Short: "Launches recaller UI for search & documentation",
Long: fmt.Sprintf("%s\n%s", asciiLogo, `Run command opens Recaller UI with search from history`),
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
// Parse the command-line flags
helpCache := NewOptimizedHelpCache()
tree := NewAVLTree()
if err := readHistoryAndPopulateTree(tree); err != nil {
log.Fatalf("Error reading history: %v", err)
}
run(tree, helpCache)
},
}
var cmdUsage = &cobra.Command{
Use: "usage",
Short: "Print Recaller usage guide",
Long: fmt.Sprintf("%s\n%s", asciiLogo, `Usage displays the recaller CLI usage guide`),
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getHelpMessage())
},
}
var cmdHistory = &cobra.Command{
Use: "history",
Short: "Fetch history sorted by time and frequency. Pass a string to find a match. Ex: recaller history s3api",
Long: fmt.Sprintf("%s\n%s", asciiLogo, "Suggest list of past %d most frequently used commands"),
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
// Parse the command-line flags
tree := NewAVLTree()
if err := readHistoryAndPopulateTree(tree); err != nil {
log.Fatalf("Error reading history: %v", err)
}
// Load configuration for fuzzy search
config, err := LoadConfig()
if err != nil {
log.Printf("Failed to load configuration: %v. Using default settings.", err)
config = &Config{History: HistoryConfig{EnableFuzzing: true}}
}
res := getSuggestions(cmd.Flag("match").Value.String(), tree, config.History.EnableFuzzing)
fmt.Println(strings.Join(res, "\n"))
},
}
cmdHistory.Flags().String("match", "", "match string prefix to look in history")
var cmdFs = &cobra.Command{
Use: "fs",
Short: "Filesystem search commands",
Long: fmt.Sprintf("%s\n%s", asciiLogo, `Launch filesystem search UI using existing index, or use subcommands to manage the index. Use 'recaller fs index [path]' to index directories first.`),
Run: func(cmd *cobra.Command, args []string) {
// Load configuration
config, err := LoadConfig()
if err != nil {
log.Printf("Failed to load configuration: %v. Using default settings.", err)
config = cloneDefaultConfig()
}
if !config.Filesystem.Enabled {
fmt.Printf("❌ Filesystem search is disabled. Enable it in configuration:\n")
fmt.Printf("Edit ~/.recaller.yaml and set:\n")
fmt.Printf("filesystem:\n enabled: true\n\n")
fmt.Printf("Or run: recaller settings list\n")
return
}
// Create filesystem indexer
fsIndexer := NewFilesystemIndexer(config.Filesystem)
// Load existing index
if err := fsIndexer.LoadOrCreateIndex(!config.Quiet); err != nil {
fmt.Printf("❌ Failed to load filesystem index: %v\n", err)
fmt.Printf("💡 Run 'recaller fs index [path]' to create an index first.\n")
return
}
// Check if index has any data
if !fsIndexer.HasIndexedFiles() {
fmt.Printf("📂 No files found in index.\n")
fmt.Printf("💡 Run 'recaller fs index [path]' to index directories first.\n")
return
}
// Auto re-index existing paths to discover new files
if len(fsIndexer.GetRootPaths()) > 0 {
if err := fsIndexer.RefreshIndex(!config.Quiet, false); err != nil {
log.Printf("Warning: Re-indexing completed with errors: %v", err)
}
}
// Show index statistics
fmt.Printf("📊 %s\n", fsIndexer.GetIndexStats())
// Launch filesystem search UI
fmt.Printf("🚀 Launching filesystem search UI...\n")
runFilesystemSearch(fsIndexer, config)
},
}
var cmdFsIndex = &cobra.Command{
Use: "index [path1] [path2] ...",
Short: "Index directories for filesystem search",
Long: `Index one or more directories for filesystem search without launching the UI. Optional paths to index (defaults to current directory if none provided).`,
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
// Load configuration
config, err := LoadConfig()
if err != nil {
log.Printf("Failed to load configuration: %v. Using default settings.", err)
config = cloneDefaultConfig()
}
if !config.Filesystem.Enabled {
fmt.Printf("❌ Filesystem search is disabled. Enable it in configuration:\n")
fmt.Printf("Edit ~/.recaller.yaml and set:\n")
fmt.Printf("filesystem:\n enabled: true\n\n")
fmt.Printf("Or run: recaller settings list\n")
return
}
// Determine paths to index
pathsToIndex := []string{"."}
if len(args) > 0 {
pathsToIndex = args
}
// Process each path: expand tilde, convert to absolute path, and verify existence
var validPaths []string
for _, pathToIndex := range pathsToIndex {
// Expand tilde in path
if strings.HasPrefix(pathToIndex, "~/") {
homeDir, err := os.UserHomeDir()
if err == nil {
pathToIndex = filepath.Join(homeDir, pathToIndex[2:])
}
}
// Convert to absolute path
absPath, err := filepath.Abs(pathToIndex)
if err != nil {
fmt.Printf("❌ Invalid path: %s\n", pathToIndex)
continue
}
// Verify path exists
if _, err := os.Stat(absPath); os.IsNotExist(err) {
fmt.Printf("❌ Path does not exist: %s\n", absPath)
continue
}
validPaths = append(validPaths, absPath)
}
if len(validPaths) == 0 {
fmt.Printf("❌ No valid paths to index\n")
return
}
// Create filesystem indexer
fsIndexer := NewFilesystemIndexer(config.Filesystem)
// Load existing index if available
if err := fsIndexer.LoadOrCreateIndex(!config.Quiet); err != nil {
log.Printf("Failed to load filesystem index: %v", err)
}
// Index the specified directories with progress
if len(validPaths) == 1 {
fmt.Printf("🔍 Starting filesystem indexing for: %s\n", validPaths[0])
if err := fsIndexer.IndexDirectoryWithProgress(validPaths[0], true); err != nil {
if err.Error() == "max indexed files limit reached" {
fmt.Printf("⚠️ Reached maximum file limit (%d files)\n", config.Filesystem.MaxIndexedFiles)
} else {
log.Printf("Warning: Indexing completed with errors: %v", err)
}
}
} else {
fmt.Printf("🔍 Starting filesystem indexing for %d directories:\n", len(validPaths))
for i, path := range validPaths {
fmt.Printf(" %d. %s\n", i+1, path)
}
fmt.Println()
if err := fsIndexer.IndexDirectoriesWithProgress(validPaths, true); err != nil {
if err.Error() == "max indexed files limit reached" {
fmt.Printf("⚠️ Reached maximum file limit (%d files)\n", config.Filesystem.MaxIndexedFiles)
} else {
log.Printf("Warning: Indexing completed with errors: %v", err)
}
}
}
// Persist the index
fmt.Printf("\n💾 Saving index to disk...")
if err := fsIndexer.PersistIndex(!config.Quiet); err != nil {
log.Printf("Warning: Failed to persist index: %v", err)
} else {
fmt.Printf(" ✅\n")
}
// Show index statistics
fmt.Printf("\n%s\n", fsIndexer.GetIndexStats())
fmt.Printf("\n💡 Run 'recaller fs' to launch the search UI.\n")
},
}
var cmdFsClean = &cobra.Command{
Use: "clean [path]",
Short: "Clean filesystem index",
Long: `Clean the filesystem index by removing stale entries, old entries, or entries matching a specific path.`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Load configuration
config, err := LoadConfig()
if err != nil {
log.Printf("Failed to load configuration: %v. Using default settings.", err)
config = cloneDefaultConfig()
}
if !config.Filesystem.Enabled {
fmt.Printf("❌ Filesystem search is disabled. Enable it first.\n")
return
}
// Create filesystem indexer
fsIndexer := NewFilesystemIndexer(config.Filesystem)
// Load existing index
if err := fsIndexer.LoadOrCreateIndex(!config.Quiet); err != nil {
fmt.Printf("❌ Failed to load filesystem index: %v\n", err)
return
}
// Get current index stats
initialSize, _ := fsIndexer.GetIndexFileSize()
fmt.Printf("📊 Current index: %s\n", fsIndexer.GetIndexStats())
if initialSize > 0 {
fmt.Printf("💾 Index file size: %.2f KB\n\n", float64(initialSize)/1024)
}
// Parse flags
removeStale, _ := cmd.Flags().GetBool("stale")
olderThanDays, _ := cmd.Flags().GetInt("older-than")
clearAll, _ := cmd.Flags().GetBool("clear")
dryRun, _ := cmd.Flags().GetBool("dry-run")
if clearAll {
if dryRun {
fmt.Printf("🔍 [DRY RUN] Would clear entire index (%d entries)\n", len(fsIndexer.pathRecords))
return
}
fmt.Printf("⚠️ This will clear the entire filesystem index. Continue? [y/N]: ")
var response string
fmt.Scanln(&response)
if strings.ToLower(response) != "y" && strings.ToLower(response) != "yes" {
fmt.Printf("❌ Operation cancelled.\n")
return
}
if err := fsIndexer.ClearIndex(); err != nil {
fmt.Printf("❌ Failed to clear index: %v\n", err)
return
}
if err := fsIndexer.PersistIndex(!config.Quiet); err != nil {
fmt.Printf("❌ Failed to persist cleared index: %v\n", err)
return
}
fmt.Printf("✅ Index cleared successfully!\n")
return
}
// Determine cleanup options
var pathPrefix string
if len(args) > 0 {
pathPrefix = args[0]
// Expand tilde and convert to absolute path
if strings.HasPrefix(pathPrefix, "~/") {
if homeDir, err := os.UserHomeDir(); err == nil {
pathPrefix = filepath.Join(homeDir, pathPrefix[2:])
}
}
if absPath, err := filepath.Abs(pathPrefix); err == nil {
pathPrefix = absPath
}
}
options := CleanupOptions{
Path: pathPrefix,
RemoveStale: removeStale,
OlderThanDays: olderThanDays,
ShowProgress: !dryRun, // Show progress only for actual cleanup
}
// Perform dry run first if requested
if dryRun {
fmt.Printf("🔍 [DRY RUN] Analyzing what would be cleaned...\n")
options.ShowProgress = false
} else {
fmt.Printf("🧹 Starting cleanup...\n")
}
// Run cleanup
stats, err := fsIndexer.CleanupIndex(options)
if err != nil {
fmt.Printf("❌ Cleanup failed: %v\n", err)
return
}
// Display results
fmt.Printf("\n📈 Cleanup Results:\n")
fmt.Printf(" Total entries: %d\n", stats.TotalEntries)
if stats.StaleFiles > 0 {
fmt.Printf(" Stale files removed: %d\n", stats.StaleFiles)
}
if stats.OldFiles > 0 {
fmt.Printf(" Old entries removed: %d\n", stats.OldFiles)
}
if pathPrefix != "" {
fmt.Printf(" Path-filtered entries removed: %d\n", stats.RemovedEntries)
}
fmt.Printf(" Total removed: %d entries\n", stats.RemovedEntries)
fmt.Printf(" Memory freed: %.2f KB\n", stats.FreedKB)
if !dryRun && stats.RemovedEntries > 0 {
// Persist changes
fmt.Printf("\n💾 Saving cleaned index...")
if err := fsIndexer.PersistIndex(!config.Quiet); err != nil {
fmt.Printf(" ❌ Failed: %v\n", err)
} else {
fmt.Printf(" ✅\n")
// Show new stats
newSize, _ := fsIndexer.GetIndexFileSize()
fmt.Printf("\n📊 Updated index: %s\n", fsIndexer.GetIndexStats())
if initialSize > 0 && newSize > 0 {
freed := float64(initialSize-newSize) / 1024
fmt.Printf("💾 Disk space freed: %.2f KB\n", freed)
}
}
} else if dryRun {
fmt.Printf("\n💡 Run without --dry-run to actually perform the cleanup.\n")
} else {
fmt.Printf("\n✅ No cleanup needed - index is already clean!\n")
}
},
}
// Add flags for clean command
cmdFsClean.Flags().Bool("stale", false, "Remove entries for files that no longer exist")
cmdFsClean.Flags().Int("older-than", 0, "Remove entries older than N days")
cmdFsClean.Flags().Bool("clear", false, "Clear the entire index (requires confirmation)")
cmdFsClean.Flags().Bool("dry-run", false, "Show what would be cleaned without making changes")
var cmdFsRefresh = &cobra.Command{
Use: "refresh",
Short: "Re-index all tracked paths to discover new files",
Long: `Re-index all previously indexed directories to discover new files and directories without launching the search UI. This is useful for manually updating your index.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Load configuration
config, err := LoadConfig()
if err != nil {
log.Printf("Failed to load configuration: %v. Using default settings.", err)
config = cloneDefaultConfig()
}
if !config.Filesystem.Enabled {
fmt.Printf("❌ Filesystem search is disabled. Enable it in configuration:\n")
fmt.Printf("Edit ~/.recaller.yaml and set:\n")
fmt.Printf("filesystem:\n enabled: true\n\n")
fmt.Printf("Or run: recaller settings list\n")
return
}
// Create filesystem indexer
fsIndexer := NewFilesystemIndexer(config.Filesystem)
// Load existing index
if err := fsIndexer.LoadOrCreateIndex(!config.Quiet); err != nil {
fmt.Printf("❌ Failed to load filesystem index: %v\n", err)
fmt.Printf("💡 Run 'recaller fs index [path]' to create an index first.\n")
return
}
// Refresh the index using the shared function
if err := fsIndexer.RefreshIndex(!config.Quiet, true); err != nil {
if err.Error() == "no tracked paths found in index" {
fmt.Printf("📂 No tracked paths found in index.\n")
fmt.Printf("💡 Run 'recaller fs index [path]' to index directories first.\n")
} else if err.Error() == "max indexed files limit reached" {
fmt.Printf("⚠️ Reached maximum file limit (%d files)\n", config.Filesystem.MaxIndexedFiles)
} else {
fmt.Printf("❌ Refresh failed: %v\n", err)
}
return
}
fmt.Printf("✔️ Refresh completed successfully!\n")
},
}
var cmdSettingsList = &cobra.Command{
Use: "list",
Short: "List current configuration settings",
Long: "Display all current configuration settings with their values",
Run: func(cmd *cobra.Command, args []string) {
displaySettings()
},
}
var cmdSettings = &cobra.Command{
Use: "settings",
Short: "Manage Recaller configuration settings",
Long: "Commands for viewing and managing Recaller configuration",
}
var cmdVersion = &cobra.Command{
Use: "version",
Short: "Print Recaller version",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
var rootCmd = &cobra.Command{
Use: "recaller",
Version: version,
Long: asciiLogo,
Run: func(cmd *cobra.Command, args []string) {
// Default to run command when no subcommand is provided
helpCache := NewOptimizedHelpCache()
tree := NewAVLTree()
if err := readHistoryAndPopulateTree(tree); err != nil {
log.Fatalf("Error reading history: %v", err)
}
run(tree, helpCache)
},
}
cmdSettings.AddCommand(cmdSettingsList)
cmdFs.AddCommand(cmdFsIndex, cmdFsClean, cmdFsRefresh)
rootCmd.AddCommand(cmdRun, cmdUsage, cmdVersion, cmdHistory, cmdFs, cmdSettings)
rootCmd.Execute()
}