2024-08-30 16:08:24 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-09-12 22:15:56 +00:00
|
|
|
func ReadConfigs(name string) ([]*Config, error) {
|
2024-08-31 16:04:53 +00:00
|
|
|
file, err := os.Open("configs.json")
|
2024-08-30 16:08:24 +00:00
|
|
|
|
2024-08-31 16:04:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-08-30 16:08:24 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
bytes, _ := io.ReadAll(file)
|
|
|
|
|
2024-08-31 16:04:53 +00:00
|
|
|
var config []*Config
|
|
|
|
err = json.Unmarshal(bytes, &config)
|
2024-08-30 16:08:24 +00:00
|
|
|
|
2024-08-31 16:04:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-08-30 16:08:24 +00:00
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
2024-08-31 19:13:16 +00:00
|
|
|
|
2024-09-12 22:15:56 +00:00
|
|
|
func SaveConfig(name string, config Config) error {
|
|
|
|
configs, _ := ReadConfigs(name)
|
2024-08-31 19:13:16 +00:00
|
|
|
configs = append(configs, &config)
|
|
|
|
|
|
|
|
data, err := json.Marshal(configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-12 22:15:56 +00:00
|
|
|
err = os.WriteFile(name, data, 0666)
|
2024-08-31 19:13:16 +00:00
|
|
|
return err
|
|
|
|
}
|