mirror of
https://github.com/number571/hes.git
synced 2026-09-14 11:05:43 +05:00
45 lines
792 B
Go
45 lines
792 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
st "github.com/number571/hes/settings"
|
|
)
|
|
|
|
type CFG struct {
|
|
Pasw string `json:"pasw"`
|
|
Conns [][2]string `json:"conns"`
|
|
}
|
|
|
|
func NewCFG(filename string) *CFG {
|
|
var config = new(CFG)
|
|
config.Conns = append(config.Conns, [2]string{
|
|
"addr",
|
|
"pasw",
|
|
})
|
|
if !fileIsExist(filename) {
|
|
err := ioutil.WriteFile(filename, st.Serialize(config), 0644)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
}
|
|
content, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
err = json.Unmarshal(content, config)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return config
|
|
}
|
|
|
|
func fileIsExist(filename string) bool {
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|