ProxySwitcher/util/config.go

40 lines
583 B
Go
Raw Normal View History

2024-08-30 16:08:24 +00:00
package util
import (
"encoding/json"
"io"
"os"
)
2024-08-31 16:04:53 +00:00
func ReadConfigs() ([]*Config, error) {
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
}
func SaveConfig(config Config) error {
configs, _ := ReadConfigs()
configs = append(configs, &config)
data, err := json.Marshal(configs)
if err != nil {
return err
}
err = os.WriteFile("configs.json", data, 0666)
return err
}