本文整理匯總了Golang中github.com/c4pt0r/cfg.NewCfg函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewCfg函數的具體用法?Golang NewCfg怎麽用?Golang NewCfg使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewCfg函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: InitConfig
func InitConfig(path string) {
globalCfg = cfg.NewCfg(path)
err := globalCfg.Load()
if err != nil {
log.Fatal(err)
}
}
示例2: InitConfigFromFile
func InitConfigFromFile(filename string) (*cfg.Cfg, error) {
ret := cfg.NewCfg(filename)
if err := ret.Load(); err != nil {
return nil, errors.Trace(err)
}
return ret, nil
}
示例3: LoadConf
func LoadConf(configFile string) (*Config, error) {
c := cfg.NewCfg(configFile)
if err := c.Load(); err != nil {
log.PanicErrorf(err, "load config '%s' failed", configFile)
}
conf := &Config{}
// 讀取product
conf.ProductName, _ = c.ReadString("product", "test")
if len(conf.ProductName) == 0 {
log.Panicf("invalid config: product entry is missing in %s", configFile)
}
// 讀取zk
conf.ZkAddr, _ = c.ReadString("zk", "")
if len(conf.ZkAddr) == 0 {
log.Panicf("invalid config: need zk entry is missing in %s", configFile)
}
conf.ZkAddr = strings.TrimSpace(conf.ZkAddr)
loadConfInt := func(entry string, defInt int) int {
v, _ := c.ReadInt(entry, defInt)
if v < 0 {
log.Panicf("invalid config: read %s = %d", entry, v)
}
return v
}
conf.ZkSessionTimeout = loadConfInt("zk_session_timeout", 30)
conf.Verbose = loadConfInt("verbose", 0) == 1
conf.Service, _ = c.ReadString("service", "")
conf.Service = strings.TrimSpace(conf.Service)
conf.FrontHost, _ = c.ReadString("front_host", "")
conf.FrontHost = strings.TrimSpace(conf.FrontHost)
conf.FrontPort, _ = c.ReadString("front_port", "")
conf.FrontPort = strings.TrimSpace(conf.FrontPort)
conf.FrontendAddr = conf.getFrontendAddr()
conf.IpPrefix, _ = c.ReadString("ip_prefix", "")
conf.IpPrefix = strings.TrimSpace(conf.IpPrefix)
conf.BackAddr, _ = c.ReadString("back_address", "")
conf.BackAddr = strings.TrimSpace(conf.BackAddr)
conf.ProxyAddr, _ = c.ReadString("proxy_address", "")
conf.ProxyAddr = strings.TrimSpace(conf.ProxyAddr)
profile, _ := c.ReadInt("profile", 0)
conf.Profile = profile == 1
return conf, nil
}
示例4: InitConfig
func InitConfig() (*cfg.Cfg, error) {
configFile := os.Getenv("CODIS_CONF")
if len(configFile) == 0 {
configFile = "config.ini"
}
ret := cfg.NewCfg(configFile)
if err := ret.Load(); err != nil {
return nil, err
}
return ret, nil
}
示例5: LoadConf
func LoadConf(configFile string) (*Config, error) {
c := cfg.NewCfg(configFile)
if err := c.Load(); err != nil {
log.PanicErrorf(err, "load config '%s' failed", configFile)
}
conf := &Config{}
conf.productName, _ = c.ReadString("product", "test")
if len(conf.productName) == 0 {
log.Panicf("invalid config: product entry is missing in %s", configFile)
}
conf.dashboardAddr, _ = c.ReadString("dashboard_addr", "")
if conf.dashboardAddr == "" {
log.Panicf("invalid config: dashboard_addr is missing in %s", configFile)
}
conf.zkAddr, _ = c.ReadString("zk", "")
if len(conf.zkAddr) == 0 {
log.Panicf("invalid config: need zk entry is missing in %s", configFile)
}
conf.zkAddr = strings.TrimSpace(conf.zkAddr)
conf.passwd, _ = c.ReadString("password", "")
conf.proxyId, _ = c.ReadString("proxy_id", "")
if len(conf.proxyId) == 0 {
log.Panicf("invalid config: need proxy_id entry is missing in %s", configFile)
}
conf.proto, _ = c.ReadString("proto", "tcp")
conf.provider, _ = c.ReadString("coordinator", "zookeeper")
loadConfInt := func(entry string, defval int) int {
v, _ := c.ReadInt(entry, defval)
if v < 0 {
log.Panicf("invalid config: read %s = %d", entry, v)
}
return v
}
conf.pingPeriod = loadConfInt("backend_ping_period", 5)
conf.maxTimeout = loadConfInt("session_max_timeout", 1800)
conf.maxBufSize = loadConfInt("session_max_bufsize", 131072)
conf.maxPipeline = loadConfInt("session_max_pipeline", 1024)
conf.zkSessionTimeout = loadConfInt("zk_session_timeout", 30000)
if conf.zkSessionTimeout <= 100 {
conf.zkSessionTimeout *= 1000
log.Warn("zkSessionTimeout is to small, it is ms not second")
}
return conf, nil
}
示例6: tryRunAsLeader
func tryRunAsLeader() {
config := cfg.NewCfg(*conf)
err := config.Load()
if err != nil {
log.Fatal(err)
}
zkaddr, err := config.ReadString("zk", "localhost:2181/tyrant")
log.Debug(zkaddr)
zkConn, _, err := zk.Connect(strings.Split(zkaddr, ","), 3*time.Second)
if err != nil {
log.Fatal(err)
}
leader := zkhelper.CreateElection(*zkConn, tyrant_zk_path)
task := &LeaderTask{}
leader.RunTask(task)
}
示例7: main
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
if createdDashboardNode {
releaseDashboardNode()
}
log.Panicf("ctrl-c or SIGTERM found, exit")
}()
args, err := docopt.Parse(usage, nil, true, utils.Version, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// set output log file
if s, ok := args["-L"].(string); ok && s != "" {
f, err := os.OpenFile(s, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
log.PanicErrorf(err, "open log file failed: %s", s)
} else {
defer f.Close()
log.StdLog = log.New(f, "")
}
}
log.SetLevel(log.LEVEL_INFO)
log.SetFlags(log.Flags() | log.Lshortfile)
// set log level
if s, ok := args["--log-level"].(string); ok && s != "" {
setLogLevel(s)
}
// set config file
var configFile string
if args["-c"] != nil {
configFile = args["-c"].(string)
} else {
configFile = "config.ini"
}
config := cfg.NewCfg(configFile)
if err := config.Load(); err != nil {
log.PanicErrorf(err, "load config file error")
}
// load global vars
globalEnv = LoadCodisEnv(config)
cmd := args["<command>"].(string)
cmdArgs := args["<args>"].([]string)
go http.ListenAndServe(":10086", nil)
err = runCommand(cmd, cmdArgs)
if err != nil {
log.PanicErrorf(err, "run sub-command failed")
}
}
示例8: LoadConf
func LoadConf(configFile string) (*Config, error) {
c := cfg.NewCfg(configFile)
if err := c.Load(); err != nil {
log.PanicErrorf(err, "load config '%s' failed", configFile)
}
conf := &Config{}
// 讀取product
conf.ProductName, _ = c.ReadString("product", "test")
if len(conf.ProductName) == 0 {
log.Panicf("invalid config: product entry is missing in %s", configFile)
}
// 讀取zk
conf.ZkAddr, _ = c.ReadString("zk", "")
if len(conf.ZkAddr) == 0 {
log.Panicf("invalid config: need zk entry is missing in %s", configFile)
}
conf.ZkAddr = strings.TrimSpace(conf.ZkAddr)
loadConfInt := func(entry string, defInt int) int {
v, _ := c.ReadInt(entry, defInt)
if v < 0 {
log.Panicf("invalid config: read %s = %d", entry, v)
}
return v
}
conf.ZkSessionTimeout = loadConfInt("zk_session_timeout", 30)
conf.Verbose = loadConfInt("verbose", 0) == 1
conf.Service, _ = c.ReadString("service", "")
conf.Service = strings.TrimSpace(conf.Service)
conf.FrontHost, _ = c.ReadString("front_host", "")
conf.FrontHost = strings.TrimSpace(conf.FrontHost)
conf.FrontPort, _ = c.ReadString("front_port", "")
conf.FrontPort = strings.TrimSpace(conf.FrontPort)
conf.FrontSock, _ = c.ReadString("front_sock", "")
conf.FrontSock = strings.TrimSpace(conf.FrontSock)
// 配置文件中使用的是相對路徑,在注冊到zk時,需要還原成為絕對路徑
if len(conf.FrontSock) > 0 {
dir, _ := os.Getwd()
conf.FrontSock = path.Clean(path.Join(dir, conf.FrontSock))
}
conf.IpPrefix, _ = c.ReadString("ip_prefix", "")
conf.IpPrefix = strings.TrimSpace(conf.IpPrefix)
// 注意先後順序:
// FrontHost, FrontPort, IpPrefix之後才能計算FrontendAddr
conf.FrontendAddr = conf.getFrontendAddr(conf.FrontHost, conf.IpPrefix, conf.FrontPort)
conf.BackAddr, _ = c.ReadString("back_address", "")
conf.BackAddr = strings.TrimSpace(conf.BackAddr)
conf.ProxyAddr, _ = c.ReadString("proxy_address", "")
conf.ProxyAddr = strings.TrimSpace(conf.ProxyAddr)
conf.FalconClient, _ = c.ReadString("falcon_client", "")
profile, _ := c.ReadInt("profile", 0)
conf.Profile = profile == 1
return conf, nil
}