mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41:45 +00:00
40 lines
583 B
Go
40 lines
583 B
Go
package util
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func ReadConfigs() ([]*Config, error) {
|
|
file, err := os.Open("configs.json")
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
bytes, _ := io.ReadAll(file)
|
|
|
|
var config []*Config
|
|
err = json.Unmarshal(bytes, &config)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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
|
|
}
|