3 Star 3 Fork 1

Gitee 极速下载 / mymon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/open-falcon/mymon
克隆/下载
config.go 2.82 KB
一键复制 编辑 原始数据 按行查看 历史
Leon Zhang 提交于 2018-10-18 10:19 . update vendor and fix #38
/*
* Open-Falcon
*
* Copyright (c) 2014-2018 Xiaomi, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product may include a number of subcomponents with separate copyright notices
* and license terms. Your use of these subcomponents is subject to the terms and
* conditions of the subcomponent's license, as noted in the LICENSE file.
*/
package common
import (
"fmt"
"os"
"github.com/go-ini/ini"
)
// BaseConf config about dir, log, etc.
type BaseConf struct {
BaseDir string
SnapshotDir string
SnapshotDay int
LogDir string
Endpoint string
LogFile string
LogLevel int
FalconClient string
IgnoreFile string
}
// DatabaseConf config about database
type DatabaseConf struct {
User string
Password string
Host string
Port int
}
// Config for initializing. This can be loaded from TOML file with -c
type Config struct {
Base BaseConf
DataBase DatabaseConf
}
// NewConfig the constructor of config
func NewConfig(file string) (*Config, error) {
conf, err := readConf(file)
return &conf, err
}
func readConf(file string) (conf Config, err error) {
_, err = os.Stat(file)
if err != nil {
file = fmt.Sprint("etc/", file)
_, err = os.Stat(file)
if err != nil {
panic(err)
}
}
cfg, err := ini.Load(file)
if err != nil {
panic(err)
}
snapshotDay, err := cfg.Section("default").Key("snapshot_day").Int()
if err != nil {
fmt.Println("No Snapshot!")
snapshotDay = -1
}
logLevel, err := cfg.Section("default").Key("log_level").Int()
if err != nil {
fmt.Println("Log level default: 7!")
logLevel = 7
}
host := cfg.Section("mysql").Key("host").String()
if host == "" {
fmt.Println("Host default: 127.0.0.1!")
host = "127.0.0.1"
}
snapshotDir := cfg.Section("default").Key("snapshot_dir").String()
if snapshotDir == "" {
fmt.Println("SnapshotDir default current dir ")
snapshotDir = "."
}
port, err := cfg.Section("mysql").Key("port").Int()
if err != nil {
fmt.Println("Port: default 3306!")
port = 3306
err = nil
}
conf = Config{
BaseConf{
BaseDir: cfg.Section("default").Key("basedir").String(),
SnapshotDir: snapshotDir,
SnapshotDay: snapshotDay,
LogDir: cfg.Section("default").Key("log_dir").String(),
LogFile: cfg.Section("default").Key("log_file").String(),
Endpoint: cfg.Section("default").Key("endpoint").String(),
LogLevel: logLevel,
FalconClient: cfg.Section("default").Key("falcon_client").String(),
IgnoreFile: cfg.Section("default").Key("ignore_file").String(),
},
DatabaseConf{
User: cfg.Section("mysql").Key("user").String(),
Password: cfg.Section("mysql").Key("password").String(),
Host: host,
Port: port,
},
}
return
}
Shell
1
https://gitee.com/mirrors/mymon.git
git@gitee.com:mirrors/mymon.git
mirrors
mymon
mymon
fda4d97c10d2

搜索帮助

53164aa7 5694891 3bd8fe86 5694891