package main

import (
	"flag"

	"sparrow/pkg/mysql"

	"github.com/jinzhu/gorm"
)

const (
	flagDBHost = "dbhost"
	flagDBPort = "dbport"
	flagDBName = "dbname"
	flagDBUser = "dbuser"
	flagDBPass = "dbpass"

	defaultDBHost = "192.168.175.60"
	defaultDBPort = "3306"
	defaultDBName = "SparrowCloud"
	defaultDBUser = "SparrowCloud"
)

var (
	confDBHost = flag.String(flagDBHost, defaultDBHost, "database host address.")
	confDBPort = flag.String(flagDBPort, defaultDBPort, "database host port.")
	confDBName = flag.String(flagDBName, defaultDBName, "database name.")
	confDBUser = flag.String(flagDBUser, defaultDBUser, "database user.")
	confDBPass = flag.String(flagDBPass, "", "databse password.")
)

func getDB() (*gorm.DB, error) {
	db, err := mysql.GetClient(*confDBHost, *confDBPort, *confDBName, *confDBUser, *confDBPass)
	if err != nil {
		return nil, err
	}
	gormdb, err := gorm.Open("mysql", db)
	if err != nil {
		return nil, err
	}
	gormdb.SingularTable(true)
	gormdb.LogMode(true)
	return gormdb, nil
}
