-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
86 lines (74 loc) · 1.63 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"github.com/urfave/cli/v2"
)
func getDownloadsFolder() string {
return filepath.Join(getUserFolder(), "Downloads")
}
func getUserFolder() string {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
return homeDir
}
func getFlag[T any](cCtx *cli.Context, flagName string) *T {
if !cCtx.IsSet(flagName) {
return nil
}
var result T
resultType := reflect.TypeOf(result)
var value interface{}
switch resultType.Kind() {
case reflect.String:
value = cCtx.String(flagName)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = cCtx.Int(flagName)
case reflect.Bool:
value = cCtx.Bool(flagName)
default:
// Handle unsupported types
return nil
}
// I questioned the safety of this type assertion, but it's safe because we're checking the type above
// Convert the value to type T and return a pointer to it
result = value.(T)
return &result
}
func input[T any](prompt string, parseFunc func(string) (T, error)) T {
var result T
for {
fmt.Print(prompt)
var input string
fmt.Scanln(&input)
value, err := parseFunc(input)
if err == nil {
result = value
break
}
fmt.Println("Invalid input, please try again.")
}
return result
}
func clearScreen() {
fmt.Print("\033[H\033[2J")
}
func writePatternsToFile(patterns regexPatterns) {
jsonData, err := json.Marshal(regexPatternsJSON{Patterns: patterns})
if err != nil {
panic(err)
}
file, err := os.Create(patternsPath)
if err != nil {
panic(err)
}
defer file.Close()
if _, err := file.Write(jsonData); err != nil {
panic(err)
}
}