-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_patterns.go
128 lines (115 loc) · 3.37 KB
/
regex_patterns.go
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
package main
import (
"errors"
"fmt"
"strconv"
)
func crudPatterns(flags flagPointers) {
options := []string{"Add Pattern", "Edit Pattern", "Delete Pattern", "Exit"}
choice := choice("Enter an option to edit: ", options)
switch choice {
case 1:
addPattern(flags)
case 2:
editPattern()
case 3:
deletePattern()
case 4:
clearScreen()
main()
default:
println("Invalid choice. Exiting...")
}
}
func addPattern(flags flagPointers) {
var pattern string
var ageThreshold int
var destination string
var deleteFlag bool
patterns := getSettings(patternsPath)
if flags.Pattern == nil {
pattern = input("Enter the pattern (regex): ", func(input string) (string, error) {
return input, nil // No conversion needed for string
})
} else {
pattern = *flags.Pattern
}
if flags.AgeThreshold == nil {
ageThreshold = input("Enter the age threshold (in days): ", strconv.Atoi)
} else {
ageThreshold = *flags.AgeThreshold
}
if flags.Destination == nil {
destination = input("Enter the destination folder: ", func(input string) (string, error) {
return input, nil // No conversion needed for string
})
} else {
destination = *flags.Destination
}
if flags.DeleteFlag == nil {
deleteFlag = input("Delete the file? (true/false): ", strconv.ParseBool)
} else {
deleteFlag = *flags.DeleteFlag
}
delete(patterns, pattern)
patterns[pattern] = regexInfo{AgeThreshold: ageThreshold, Destination: destination, DeleteFlag: deleteFlag}
writePatternsToFile(patterns)
}
func editPattern() {
patterns := getSettings(patternsPath)
keys := make([]string, 0, len(patterns))
for key := range patterns {
keys = append(keys, key)
}
patternToEdit := choice("Choose a pattern to edit: ", keys)
oldPattern := keys[patternToEdit-1]
options := []string{"Pattern", "Age Threshold", "Destination", "Delete Flag"}
optionToEdit := choice("Choose an option to edit: ", options)
var ageThreshold int
var destination string
var deleteFlag bool
var pattern string
switch optionToEdit {
case 1:
newPattern := input("Enter the new pattern (regex or simple string): ", func(input string) (string, error) {
return input, nil // No conversion needed for string
})
pattern = newPattern
case 2:
newAgeThreshold := input("Enter the new age threshold (in days): ", strconv.Atoi)
ageThreshold = newAgeThreshold
case 3:
newDestination := input("Enter the new destination folder: ", func(input string) (string, error) {
return input, nil // No conversion needed for string
})
destination = newDestination
case 4:
newDeleteFlag := input("Delete the file? (true/false): ", strconv.ParseBool)
deleteFlag = newDeleteFlag
default:
println("Invalid choice. Exiting...")
}
delete(patterns, oldPattern)
patterns[pattern] = regexInfo{AgeThreshold: ageThreshold, Destination: destination, DeleteFlag: deleteFlag}
writePatternsToFile(patterns)
}
func deletePattern() {
patterns := getSettings(patternsPath)
println("Choose a Pattern to delete:")
keys := make([]string, 0, len(patterns))
i := 1
for key := range patterns {
fmt.Printf("%d. %s\n", i, key)
keys = append(keys, key)
i++
}
choice := input("Enter your choice: ", func(input string) (int, error) {
choice, err := strconv.Atoi(input)
if err != nil || choice < 1 || choice > len(keys) {
return 0, errors.New("invalid choice")
}
return choice, nil
})
delete(patterns, keys[choice-1])
writePatternsToFile(patterns)
}