示例#1
0
func FileToConfig(configFile string) (*conf.ConfigFile, error) {
	cnf, err := conf.ReadConfigFile(configFile)
	if err != nil {
		return nil, err
	}
	return cnf, nil
}
示例#2
0
// New create connection to mysql database here
// if an error is encountered, still return database so that the logger may be used
func New(user, password, host, config string) (MysqlDB, error) {

	dsn := map[string]string{"dbname": "information_schema"}
	creds := map[string]string{"root": "/root/.my.cnf", "nrpe": "/etc/my_nrpe.cnf"}

	database := &mysqlDB{}

	if user == "" {
		user = DefaultMySQLUser
		dsn["user"] = DefaultMySQLUser
	} else {
		dsn["user"] = user
	}
	if password != "" {
		dsn["password"] = password
	}

	// ex: "unix(/var/lib/mysql/mysql.sock)"
	// ex: "tcp(your.db.host.com:3306)"
	dsn["host"] = host

	//Parse ini file to get password
	iniFile := creds[user]
	if config != "" {
		iniFile = config
	}
	_, err := os.Stat(iniFile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return database, errors.New("'" + iniFile + "' does not exist")
	}
	// read ini file to get password
	c, err := conf.ReadConfigFile(iniFile)
	if err != nil {
		return database, err
	}
	pw, err := c.GetString("client", "password")
	dsn["password"] = strings.Trim(pw, " \"")
	database.dsnString = makeDsn(dsn)

	//make connection to db
	db, err := sql.Open("mysql", database.dsnString)
	if err != nil {
		return database, err
	}
	database.db = db

	//ping db to verify connection
	err = database.db.Ping()
	if err != nil {
		return database, err
	}
	return database, nil
}