github.com/dolotech/hongbao@v0.0.0-20191130105438-fd59d7a5dda5/src/utils/cfg/cfg.go (about)

     1  package cfg
     2  
     3  import (
     4  	"github.com/BurntSushi/toml"
     5  	"github.com/golang/glog"
     6  	"github.com/kr/pretty"
     7  	"os"
     8  )
     9  
    10  type Cookies struct {
    11  	Cookie  []string
    12  	Address string
    13  	API     string
    14  	Mode    int
    15  }
    16  
    17  type MysqlDB struct {
    18  	Host     string
    19  	User     string
    20  	Password string
    21  	DBName   string
    22  }
    23  
    24  // Config 配置类型
    25  type Config struct {
    26  	Cookies []Cookies
    27  	MysqlDB MysqlDB
    28  }
    29  
    30  // Opts Config 默认配置
    31  var opts *Config
    32  
    33  // ParseToml 解析配置文件
    34  var file string
    35  
    36  func Reload() error {
    37  	if _, err := os.Stat(file); os.IsNotExist(err) {
    38  		glog.Errorln("没有找到配置文件 ...")
    39  		return nil
    40  	}
    41  	opts = &Config{}
    42  	_, err := toml.DecodeFile(file, opts)
    43  	if err != nil {
    44  		glog.Errorln("配置文件解析错误:", err)
    45  		return err
    46  	}
    47  	glog.Infof("cfg is %v", pretty.Formatter(opts))
    48  	return nil
    49  }
    50  func ParseToml(f string) error {
    51  	file = f
    52  	return Reload()
    53  }
    54  
    55  // Opts 获取配置
    56  func Opts() *Config {
    57  	return opts
    58  }