-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_handlers.go
More file actions
792 lines (689 loc) · 23.4 KB
/
command_handlers.go
File metadata and controls
792 lines (689 loc) · 23.4 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
// isPathAccessible checks if a path exists and is accessible
func isPathAccessible(path string) bool {
// Check for network paths specially
if len(path) > 2 && (path[0:2] == "\\" || path[0:2] == "//") {
// For network paths, we need to check if they are accessible
// If it's a UNC path, just check if we can stat it
_, err := os.Stat(path)
return err == nil
}
// For local paths
_, err := os.Stat(path)
return err == nil
}
// handleErrorWithUserMessage handles errors with user-friendly messages
// and returns true if the error was handled
func handleErrorWithUserMessage(err error, path string, historyLogger *HistoryLogger) bool {
if err == nil {
return false
}
historyLogger.SetError(err)
errMsg := err.Error()
// Handle common error patterns with user-friendly messages
if strings.Contains(errMsg, "device") && strings.Contains(errMsg, "does not exist") {
fmt.Printf("Info: Device \"%s\" does not exist.\n", path)
return true
} else if strings.Contains(errMsg, "file") && strings.Contains(errMsg, "not found") ||
strings.Contains(errMsg, "system cannot find the file") {
fmt.Printf("Info: File \"%s\" does not exist or is not accessible.\n", path)
return true
} else if strings.Contains(errMsg, "folder") && strings.Contains(errMsg, "not found") ||
strings.Contains(errMsg, "directory") && strings.Contains(errMsg, "not found") ||
strings.Contains(errMsg, "system cannot find the path") {
fmt.Printf("Info: Folder \"%s\" does not exist or is not accessible.\n", path)
return true
} else if strings.Contains(errMsg, "network") && strings.Contains(errMsg, "not accessible") {
fmt.Printf("Info: Network path \"%s\" is not accessible.\n", path)
return true
}
// Default error handling
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return true
}
// redirectSystemDrive redirects C: to user's temp directory for write operations with user confirmation
func redirectSystemDrive(path string) string {
if strings.ToLower(path) == "c:" {
// Check if redirection is disabled by environment variable
if os.Getenv("FILEDO_DISABLE_REDIRECT") == "1" {
fmt.Printf("⚠️ System drive redirection disabled by FILEDO_DISABLE_REDIRECT=1\n")
fmt.Printf(" WARNING: Writing directly to C: - use with caution!\n")
return path
}
// Get user's temp directory from environment
tempDir := os.Getenv("TEMP")
if tempDir == "" {
tempDir = os.Getenv("TMP") // Fallback to TMP
}
if tempDir == "" {
tempDir = "C:\\TEMP" // Final fallback
}
// Create subdirectory for FileDO operations
fileDoTempDir := filepath.Join(tempDir, "FileDO_Operations")
// Show warning and ask for confirmation
fmt.Printf("⚠️ WARNING: Write operation requested on system drive C:\n")
fmt.Printf(" For safety, redirecting to temporary directory:\n")
fmt.Printf(" %s\n\n", fileDoTempDir)
fmt.Printf(" This protects your system from potential issues during testing.\n")
fmt.Printf(" Test files will be created in this safe location instead.\n\n")
var response string
// Check for auto-confirm environment variable for testing
if os.Getenv("FILEDO_AUTO_CONFIRM") == "1" {
fmt.Printf("Auto-confirming redirection (FILEDO_AUTO_CONFIRM=1)\n")
response = "y"
} else {
fmt.Printf("Continue with redirection? (Y/n): ")
fmt.Scanln(&response)
}
response = strings.TrimSpace(strings.ToLower(response))
// Default to Yes if empty input or 'y'
if response == "" || response == "y" || response == "yes" {
// Create the directory if it doesn't exist
if _, err := os.Stat(fileDoTempDir); os.IsNotExist(err) {
if err := os.MkdirAll(fileDoTempDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not create %s: %v\n", fileDoTempDir, err)
fmt.Fprintf(os.Stderr, "Falling back to original path (use at your own risk).\n")
return path
}
fmt.Printf("✓ Created safe directory: %s\n", fileDoTempDir)
}
fmt.Printf("✓ Using safe location: %s\n", fileDoTempDir)
return fileDoTempDir
} else {
fmt.Printf("⚠️ User chose to proceed with system drive C: directly.\n")
fmt.Printf(" WARNING: This may affect system stability or performance.\n")
return path
}
}
return path
}
// CommandType represents the command type
type CommandType int
const (
CommandDevice CommandType = iota
CommandFolder
CommandNetwork
CommandFile
)
// CommandHandler interface for command handlers
type CommandHandler interface {
Info(path string, fullScan bool) (string, error)
SpeedTest(path, size string, noDelete, shortFormat bool) error
Fill(path, size string, autoDelete bool) error
FillClean(path string) error
FillVerify(path string) error
Test(path string, autoDelete bool, maxFiles int) error
Probe(path string, assumeYes bool, autoRepair bool) error
CheckDuplicates(path string, args []string) error
Copy(sourcePath, targetPath string) error
Wipe(path string) error
}
// DeviceHandler implements CommandHandler for devices
type DeviceHandler struct{}
func (h DeviceHandler) Info(path string, fullScan bool) (string, error) {
info, err := getDeviceInfo(path, fullScan)
if err != nil {
return "", err
}
return info.String(), nil
}
func (h DeviceHandler) SpeedTest(path, size string, noDelete, shortFormat bool) error {
return runDeviceSpeedTest(path, size, noDelete, shortFormat)
}
func (h DeviceHandler) Fill(path, size string, autoDelete bool) error {
return runDeviceFill(path, size, autoDelete)
}
func (h DeviceHandler) FillClean(path string) error {
return runDeviceFillClean(path)
}
func (h DeviceHandler) FillVerify(path string) error {
return runDeviceFillVerify(path)
}
func (h DeviceHandler) Test(path string, autoDelete bool, maxFiles int) error {
return runDeviceTest(path, autoDelete, maxFiles)
}
func (h DeviceHandler) Probe(path string, assumeYes bool, autoRepair bool) error {
return runDeviceProbeCheck(path, assumeYes, autoRepair)
}
func (h DeviceHandler) CheckDuplicates(path string, args []string) error {
return runDeviceCheckDuplicates(path, args)
}
func (h DeviceHandler) Copy(sourcePath, targetPath string) error {
// For device operations, delegate to handleCopyCommand
return handleCopyCommand([]string{"copy", sourcePath, targetPath})
}
func (h DeviceHandler) Wipe(path string) error {
return handleWipeCommand([]string{path})
}
// FolderHandler implements CommandHandler for folders
type FolderHandler struct{}
func (h FolderHandler) Info(path string, fullScan bool) (string, error) {
info, err := getFolderInfo(path, fullScan)
if err != nil {
return "", err
}
return info.String(), nil
}
func (h FolderHandler) SpeedTest(path, size string, noDelete, shortFormat bool) error {
return runFolderSpeedTest(path, size, noDelete, shortFormat)
}
func (h FolderHandler) Fill(path, size string, autoDelete bool) error {
return runFolderFill(path, size, autoDelete)
}
func (h FolderHandler) FillClean(path string) error {
return runFolderFillClean(path)
}
func (h FolderHandler) FillVerify(path string) error {
return runDeviceFillVerify(path) // same logic: scan FILL_*.tmp in directory
}
func (h FolderHandler) Test(path string, autoDelete bool, maxFiles int) error {
return runFolderTest(path, autoDelete, maxFiles)
}
func (h FolderHandler) Probe(path string, assumeYes bool, autoRepair bool) error {
return fmt.Errorf("probe requires a drive letter, not a folder path")
}
func (h FolderHandler) CheckDuplicates(path string, args []string) error {
return runFolderCheckDuplicates(path, args)
}
func (h FolderHandler) Copy(sourcePath, targetPath string) error {
// For folder operations, delegate to handleCopyCommand
return handleCopyCommand([]string{"copy", sourcePath, targetPath})
}
func (h FolderHandler) Wipe(path string) error {
return handleWipeCommand([]string{path})
}
// NetworkHandler implements CommandHandler for network
type NetworkHandler struct{}
func (h NetworkHandler) Info(path string, fullScan bool) (string, error) {
info, err := getNetworkInfo(path, fullScan)
if err != nil {
return "", err
}
return info.String(), nil
}
func (h NetworkHandler) SpeedTest(path, size string, noDelete, shortFormat bool) error {
return runNetworkSpeedTest(path, size, noDelete, shortFormat, nil)
}
func (h NetworkHandler) Fill(path, size string, autoDelete bool) error {
return runNetworkFill(path, size, autoDelete, nil)
}
func (h NetworkHandler) FillClean(path string) error {
return runNetworkFillClean(path, nil)
}
func (h NetworkHandler) FillVerify(path string) error {
return runDeviceFillVerify(path) // same logic: scan FILL_*.tmp in directory
}
func (h NetworkHandler) Test(path string, autoDelete bool, maxFiles int) error {
return runNetworkTest(path, autoDelete, maxFiles, nil)
}
func (h NetworkHandler) Probe(path string, assumeYes bool, autoRepair bool) error {
return fmt.Errorf("probe is only supported for local drive letters (e.g. D:)")
}
func (h NetworkHandler) CheckDuplicates(path string, args []string) error {
return runNetworkCheckDuplicates(path, args, nil)
}
func (h NetworkHandler) Copy(sourcePath, targetPath string) error {
// For network operations, delegate to handleCopyCommand
return handleCopyCommand([]string{"copy", sourcePath, targetPath})
}
func (h NetworkHandler) Wipe(path string) error {
return handleWipeCommand([]string{path})
}
// FileHandler implements CommandHandler for files
type FileHandler struct{}
func (h FileHandler) Info(path string, fullScan bool) (string, error) {
info, err := getFileInfo(path, fullScan)
if err != nil {
return "", err
}
return info.String(), nil
}
func (h FileHandler) SpeedTest(path, size string, noDelete, shortFormat bool) error {
return fmt.Errorf("speed test is not supported for files")
}
func (h FileHandler) Fill(path, size string, autoDelete bool) error {
return fmt.Errorf("fill operation is not supported for files")
}
func (h FileHandler) FillClean(path string) error {
return fmt.Errorf("fill clean operation is not supported for files")
}
func (h FileHandler) FillVerify(path string) error {
return fmt.Errorf("fill verify operation is not supported for files")
}
func (h FileHandler) Test(path string, autoDelete bool, maxFiles int) error {
return fmt.Errorf("test operation is not supported for files")
}
func (h FileHandler) Probe(path string, assumeYes bool, autoRepair bool) error {
return fmt.Errorf("probe is only supported for local drive letters (e.g. D:)")
}
func (h FileHandler) CheckDuplicates(path string, args []string) error {
return fmt.Errorf("check-duplicates operation is not supported for individual files")
}
func (h FileHandler) Copy(sourcePath, targetPath string) error {
// For individual files, delegate to handleCopyCommand
return handleCopyCommand([]string{"copy", sourcePath, targetPath})
}
func (h FileHandler) Wipe(path string) error {
// For files, wipe doesn't make sense - you can only delete the file
return fmt.Errorf("wipe command is not applicable to individual files - use delete instead")
}
// getCommandHandler returns the appropriate command handler
func getCommandHandler(cmdType CommandType) CommandHandler {
switch cmdType {
case CommandDevice:
return DeviceHandler{}
case CommandFolder:
return FolderHandler{}
case CommandNetwork:
return NetworkHandler{}
case CommandFile:
return FileHandler{}
default:
return nil
}
}
// runGenericCommand generic function for executing commands
func runGenericCommand(cmd *flag.FlagSet, cmdType CommandType, args []string, historyLogger *HistoryLogger) {
cmd.Parse(args)
if cmd.NArg() < 1 {
fmt.Fprintf(os.Stderr, "Error: '%s' command requires a path argument.\n", cmd.Name())
return
}
path := cmd.Arg(0)
// First check if the path exists (for folders, files and network paths)
if cmdType != CommandDevice && !isPathAccessible(path) {
resourceType := "Path"
if cmdType == CommandFolder {
resourceType = "Folder"
} else if cmdType == CommandFile {
resourceType = "File"
} else if cmdType == CommandNetwork {
resourceType = "Network path"
}
fmt.Printf("Info: %s \"%s\" does not exist or is not accessible.\n", resourceType, path)
return
}
// Redirect system drive for write operations (speed, fill, test)
if cmd.NArg() >= 2 {
operation := strings.ToLower(cmd.Arg(1))
if operation == "speed" || operation == "fill" || operation == "f" || operation == "test" {
path = redirectSystemDrive(path)
}
}
handler := getCommandHandler(cmdType)
// Set basic command info for history
cmdTypeName := map[CommandType]string{
CommandDevice: "device",
CommandFolder: "folder",
CommandNetwork: "network",
CommandFile: "file",
}[cmdType]
historyLogger.SetCommand(cmdTypeName, path, "")
historyLogger.SetParameter("args", args)
// Check if this is a clean command
if cmd.NArg() >= 2 {
cleanParam := strings.ToLower(cmd.Arg(1))
if cleanParam == "cln" || cleanParam == "clean" || cleanParam == "c" {
historyLogger.SetCommand(cmdTypeName, path, "clean")
// Special handling for network clean to pass logger
if cmdTypeName == "network" {
err := runNetworkFillClean(path, historyLogger)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
} else {
err := handler.FillClean(path)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
}
historyLogger.SetSuccess()
return
}
// Check if this is a check-duplicates command
duplicatesParam := strings.ToLower(cmd.Arg(1))
if duplicatesParam == "check-duplicates" || duplicatesParam == "cd" || duplicatesParam == "duplicate" {
historyLogger.SetCommand(cmdTypeName, path, "check-duplicates")
// Collect additional arguments if any
var dupArgs []string
if cmd.NArg() > 2 {
dupArgs = cmd.Args()[2:]
}
err := handler.CheckDuplicates(path, dupArgs)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
historyLogger.SetSuccess()
return
}
}
// Check if this is a speed test
if cmd.NArg() >= 2 && strings.ToLower(cmd.Arg(1)) == "speed" {
historyLogger.SetCommand(cmdTypeName, path, "speed")
sizeParam := "100" // Default size
if cmd.NArg() >= 3 {
sizeParam = cmd.Arg(2)
}
historyLogger.SetParameter("size", sizeParam)
// Check for no-delete option and short format
noDelete := false
shortFormat := false
startIndex := 3
if cmd.NArg() == 2 {
// No size parameter provided, check from index 2
startIndex = 2
}
for i := startIndex; i < cmd.NArg(); i++ {
arg := strings.ToLower(cmd.Arg(i))
if arg == "no" || arg == "nodel" || arg == "nodelete" {
noDelete = true
historyLogger.SetParameter("noDelete", true)
} else if arg == "short" || arg == "s" {
shortFormat = true
historyLogger.SetParameter("shortFormat", true)
}
}
// Handle "max" as size parameter
if strings.ToLower(sizeParam) == "max" {
sizeParam = "10240" // 10GB
historyLogger.SetParameter("actualSize", "10240MB")
}
// Special handling for network speed test to pass logger
if cmdTypeName == "network" {
err := runNetworkSpeedTest(path, sizeParam, noDelete, shortFormat, historyLogger)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
} else {
err := handler.SpeedTest(path, sizeParam, noDelete, shortFormat)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
}
historyLogger.SetSuccess()
return
}
// Check if this is a fill verify command: filedo D: fill verify
if cmd.NArg() >= 3 &&
(strings.ToLower(cmd.Arg(1)) == "fill" || strings.ToLower(cmd.Arg(1)) == "f") &&
(strings.ToLower(cmd.Arg(2)) == "verify" || strings.ToLower(cmd.Arg(2)) == "v") {
historyLogger.SetCommand(cmdTypeName, path, "fill-verify")
err := handler.FillVerify(path)
if err != nil {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
historyLogger.SetSuccess()
return
}
// Check if this is a fill command
if cmd.NArg() >= 2 && (strings.ToLower(cmd.Arg(1)) == "fill" || strings.ToLower(cmd.Arg(1)) == "f") {
historyLogger.SetCommand(cmdTypeName, path, "fill")
sizeParam := "100"
autoDelete := false
if cmd.NArg() >= 3 {
thirdArg := strings.ToLower(cmd.Arg(2))
if thirdArg == "del" || thirdArg == "delete" || thirdArg == "d" {
autoDelete = true
} else {
sizeParam = cmd.Arg(2)
}
}
if cmd.NArg() >= 4 && !autoDelete {
fourthArg := strings.ToLower(cmd.Arg(3))
if fourthArg == "del" || fourthArg == "delete" || fourthArg == "d" {
autoDelete = true
}
}
historyLogger.SetParameter("size", sizeParam)
if autoDelete {
historyLogger.SetParameter("autoDelete", true)
}
// Special handling for network fill to pass logger
if cmdTypeName == "network" {
err := runNetworkFill(path, sizeParam, autoDelete, historyLogger)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
} else {
err := handler.Fill(path, sizeParam, autoDelete)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
}
historyLogger.SetSuccess()
return
}
// Check if this is a test command
if cmd.NArg() >= 2 && strings.ToLower(cmd.Arg(1)) == "test" {
historyLogger.SetCommand(cmdTypeName, path, "test")
// Parse additional arguments: optional N (number of files) and optional "del"
// Supported forms: test | test N | test del | test N del
const defaultMaxFiles = 100
maxFiles := defaultMaxFiles
autoDelete := false
for i := 2; i < cmd.NArg(); i++ {
arg := strings.ToLower(strings.TrimSpace(cmd.Arg(i)))
if arg == "del" || arg == "delete" || arg == "d" {
autoDelete = true
} else if n, err := strconv.Atoi(arg); err == nil && n > 0 {
maxFiles = n
}
}
if autoDelete {
historyLogger.SetParameter("autoDelete", true)
}
if maxFiles != defaultMaxFiles {
historyLogger.SetParameter("maxFiles", maxFiles)
}
// Special handling for network test to pass logger
if cmdTypeName == "network" {
err := runNetworkTest(path, autoDelete, maxFiles, historyLogger)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
} else {
err := handler.Test(path, autoDelete, maxFiles)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
}
historyLogger.SetSuccess()
return
}
// Check if this is a probe command
if cmd.NArg() >= 2 && strings.ToLower(cmd.Arg(1)) == "probe" {
historyLogger.SetCommand(cmdTypeName, path, "probe")
// Optional flags:
// yes|y|allright|force -> skip interactive confirmations
// fix|repair|format -> offer/perform quick format if drive becomes unreadable after probe
assumeYes := false
autoRepair := false
for i := 2; i < cmd.NArg(); i++ {
arg := strings.ToLower(strings.TrimSpace(cmd.Arg(i)))
switch arg {
case "yes", "y", "allright", "force":
assumeYes = true
case "fix", "repair", "format":
autoRepair = true
}
}
if assumeYes {
historyLogger.SetParameter("assumeYes", true)
}
if autoRepair {
historyLogger.SetParameter("autoRepair", true)
}
err := handler.Probe(path, assumeYes, autoRepair)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
historyLogger.SetSuccess()
return
}
// Check if this is a recover command (device only)
if cmd.NArg() >= 2 {
recoverParam := strings.ToLower(cmd.Arg(1))
if recoverParam == "recover" || recoverParam == "repair" {
historyLogger.SetCommand(cmdTypeName, path, "recover")
if cmdTypeName != "device" {
err := fmt.Errorf("recover command is only supported for local drive letters (e.g. D:)")
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
// Optional flags:
// yes|y|allright|force -> skip interactive confirmations
// format|fmt|forceformat -> force quick format path
assumeYes := false
forceFormat := false
for i := 2; i < cmd.NArg(); i++ {
arg := strings.ToLower(strings.TrimSpace(cmd.Arg(i)))
switch arg {
case "yes", "y", "allright", "force":
assumeYes = true
case "format", "fmt", "forceformat":
forceFormat = true
}
}
if assumeYes {
historyLogger.SetParameter("assumeYes", true)
}
if forceFormat {
historyLogger.SetParameter("forceFormat", true)
}
err := runDeviceRecoverCheck(path, assumeYes, forceFormat)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
historyLogger.SetSuccess()
return
}
}
// Check if this is a copy command
if cmd.NArg() >= 3 {
copyParam := strings.ToLower(cmd.Arg(1))
if copyParam == "copy" || copyParam == "cp" || copyParam == "c" {
historyLogger.SetCommand(cmdTypeName, path, "copy")
targetPath := cmd.Arg(2)
historyLogger.SetParameter("targetPath", targetPath)
err := handler.Copy(path, targetPath)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
historyLogger.SetSuccess()
return
}
}
// Check if this is a wipe command
if cmd.NArg() >= 2 {
wipeParam := strings.ToLower(cmd.Arg(1))
if wipeParam == "wipe" || wipeParam == "w" {
historyLogger.SetCommand(cmdTypeName, path, "wipe")
err := handler.Wipe(path)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
}
historyLogger.SetSuccess()
return
}
}
// Regular info command
historyLogger.SetCommand(cmdTypeName, path, "info")
fullScan := cmd.NArg() > 1 && (strings.ToLower(cmd.Arg(1)) == "info" || strings.ToLower(cmd.Arg(1)) == "i")
shortFormat := cmd.NArg() > 1 && (strings.ToLower(cmd.Arg(1)) == "short" || strings.ToLower(cmd.Arg(1)) == "s")
if fullScan {
historyLogger.SetParameter("fullScan", true)
}
if shortFormat {
historyLogger.SetParameter("shortFormat", true)
}
// Special handling for folder short format
if cmdType == CommandFolder && shortFormat {
fullScan = true
}
result, err := handler.Info(path, fullScan)
if err != nil {
if !handleErrorWithUserMessage(err, path, historyLogger) {
historyLogger.SetError(err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
return
}
// Special handling for folder and device short format
if shortFormat && (cmdType == CommandFolder || cmdType == CommandDevice) {
switch cmdType {
case CommandFolder:
info, _ := getFolderInfo(path, fullScan)
fmt.Print(info.StringShort())
case CommandDevice:
info, _ := getDeviceInfo(path, fullScan)
fmt.Print(info.StringShort())
}
} else {
fmt.Print(result)
}
historyLogger.SetSuccess()
}