ProxySwitcher/util/config.go

40 lines
601 B
Go
Raw Normal View History

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