V2bX/conf/conf.go

48 lines
805 B
Go
Raw Normal View History

package conf
2022-09-12 23:08:20 -04:00
import (
"fmt"
2024-01-25 09:06:37 -05:00
"io"
2022-09-12 23:08:20 -04:00
"os"
2023-07-29 06:47:47 -04:00
2024-01-25 09:06:37 -05:00
"github.com/InazumaV/V2bX/common/json5"
2023-08-20 03:13:52 -04:00
"github.com/goccy/go-json"
2022-09-12 23:08:20 -04:00
)
type Conf struct {
2023-08-20 03:13:52 -04:00
LogConfig LogConfig `json:"Log"`
CoresConfig []CoreConfig `json:"Cores"`
NodeConfig []NodeConfig `json:"Nodes"`
}
func New() *Conf {
return &Conf{
2023-08-20 03:13:52 -04:00
LogConfig: LogConfig{
Level: "info",
Output: "",
},
}
}
2022-09-12 23:08:20 -04:00
func (p *Conf) LoadFromPath(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
2022-10-09 23:31:15 -04:00
return fmt.Errorf("open config file error: %s", err)
2022-09-12 23:08:20 -04:00
}
2023-06-01 21:19:37 -04:00
defer f.Close()
2024-01-25 09:06:37 -05:00
reader := json5.NewTrimNodeReader(f)
data, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("read config file error: %s", err)
}
err = json.Unmarshal(data, p)
if err != nil {
return fmt.Errorf("unmarshal config error: %s", err)
}
return nil
2022-10-09 23:31:15 -04:00
}