| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package comp
- import (
- pb "git.wanbits.io/joe/franklin/protos"
- "git.wanbits.io/joe/kettle/utl"
- "github.com/spf13/viper"
- "path/filepath"
- )
- func LoadConf(fullfile string, defPaths ...string) (*pb.AppConf, error) {
- c := pb.AppConf{}
- dir, f := filepath.Split(fullfile)
- if len(dir) <= 0 {
- dir = "./"
- }
- v := viper.New()
- v.SetConfigType("toml")
- v.AddConfigPath(dir)
- for _, path := range defPaths {
- v.AddConfigPath(path)
- }
- v.SetConfigFile(f)
- err := v.ReadInConfig()
- if err != nil {
- return nil, err
- }
- err = v.Unmarshal(&c)
- if err != nil {
- return nil, err
- }
- err = checkAppConf(&c)
- if err != nil {
- return nil, err
- }
- return &c, nil
- }
- func checkAppConf(c *pb.AppConf) error {
- if c == nil {
- return utl.ErrForCode(int(pb.ErrCode_INVALID_PARAMS))
- }
- if len(c.Namespace) <= 0 {
- return utl.ErrForCode(int(pb.ErrCode_LENGTH))
- }
- if _, err := Parse(c.Id); err != nil {
- return utl.ErrForCode(int(pb.ErrCode_BAD_FORMAT))
- }
- if len(c.Zoo.Addrs) <= 0 {
- return utl.ErrForCode(int(pb.ErrCode_LENGTH))
- }
- return nil
- }
|