-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_handler.go
59 lines (51 loc) · 1.52 KB
/
settings_handler.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
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
)
var patternsPath = filepath.Join(getUserFolder(), "AppData", "Local", "CleanDL", "patterns.json")
func createSettings(path string) {
// Ensure the directory exists
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
panic(err)
}
// if the file doesn't exist, create it
if _, err := os.Stat(path); os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
panic(err)
}
defer file.Close()
// Serialize the map to JSON
jsonData, err := json.Marshal(regexPatternsJSON{Patterns: regexPatterns{}})
if err != nil {
panic(err) // Consider more graceful error handling
}
// Write the JSON data to the file
if _, err := file.Write(jsonData); err != nil {
panic(err) // Consider more graceful error handling
}
}
}
func getSettings(path string) regexPatterns {
settingsFile, err := os.Open(path)
// if os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Printf("Successfully Opened %s\n", path)
// defer the closing of our jsonFile so that we can parse it later on
defer settingsFile.Close()
byteValue, _ := io.ReadAll(settingsFile)
// we initialize our custom regex array
var regexPatternsJSON regexPatternsJSON
// we unmarshal our byteArray which contains our
// jsonFile's content into 'regexPatternsJSON' which we defined above
json.Unmarshal(byteValue, ®exPatternsJSON)
var regexPatterns regexPatterns = regexPatternsJSON.Patterns
return regexPatterns
}