V2bX/conf/conf.go

43 lines
760 B
Go
Raw Normal View History

package conf
2022-09-12 23:08:20 -04:00
import (
"fmt"
2023-06-01 21:19:37 -04:00
"io"
2022-09-12 23:08:20 -04:00
"os"
2023-07-29 06:47:47 -04:00
"gopkg.in/yaml.v3"
2022-09-12 23:08:20 -04:00
)
type Conf struct {
CoreConfig CoreConfig `yaml:"CoreConfig"`
NodesConfig []*NodeConfig `yaml:"Nodes"`
}
func New() *Conf {
return &Conf{
CoreConfig: CoreConfig{
2023-07-29 06:47:47 -04:00
Type: "xray",
XrayConfig: NewXrayConfig(),
SingConfig: NewSingConfig(),
},
NodesConfig: []*NodeConfig{},
}
}
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()
content, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("read file error: %s", err)
}
err = yaml.Unmarshal(content, p)
2022-09-12 23:08:20 -04:00
if err != nil {
2022-10-09 23:31:15 -04:00
return fmt.Errorf("decode config error: %s", err)
}
return nil
}