-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_service.go
84 lines (68 loc) · 2.08 KB
/
files_service.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
package main
import (
"encoding/json"
"fmt"
"os"
"io/ioutil"
)
// Load and parse the config.json file into a slice of Config structs
func loadConfig(filename string) ([]Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening config file: %v", err)
}
defer file.Close()
configData, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("error reading config file: %v", err)
}
var configs []Config
err = json.Unmarshal(configData, &configs)
if err != nil {
return nil, fmt.Errorf("error parsing config file: %v", err)
}
return configs, nil
}
// Write results to a file in the specified format
func writeResultsToFile(results []Result, filename string) error {
fmt.Printf("[writeResultsToFile] Writing results to %s\n", filename)
data, err := json.MarshalIndent(results, "", " ")
if err != nil {
return fmt.Errorf("error marshalling results: %v", err)
}
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
return fmt.Errorf("error writing results to file: %v", err)
}
return nil
}
// Load existing results from store.json
func getResultsMap() (map[string]Result, error) {
existingResults, err := loadExistingResults()
if err != nil {
return nil, fmt.Errorf("error loading existing results: %v", err)
}
existingMap := make(map[string]Result)
for _, res := range existingResults {
existingMap[res.Name] = res
}
return existingMap, nil
}
// Load existing results from store.json
func loadExistingResults() ([]Result, error) {
file, err := os.Open("store.json")
if err != nil {
return nil, fmt.Errorf("error opening store file: %v", err)
}
defer file.Close()
var existingResults []Result
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("error reading store file: %v", err)
}
err = json.Unmarshal(data, &existingResults)
if err != nil {
return nil, fmt.Errorf("error parsing store file: %v", err)
}
return existingResults, nil
}