add cobra

add cobra
master
xiexiaojun 6 years ago
parent 21a054c9c7
commit 03a29cc432

@ -0,0 +1,11 @@
serial_number = "1.0"
service_name = "jewelryserver"
service_displayname = "jewelryserver"
sercice_desc = "jewelryserver"
is_dev = true
[mysql_info]
host = "127.0.0.1"
port = 3306
username = "root"
password = "qwer"
database = "oauth_db"

@ -0,0 +1,66 @@
package cmd
import (
"data/config"
"fmt"
"os"
"public/tools"
"github.com/spf13/cobra"
"gopkg.in/go-playground/validator.v9"
)
var mysqlInfo config.MysqlDbInfo
var rootCmd = &cobra.Command{
Use: "main",
Short: "gorm mysql reflect tools",
Long: `base on gorm tools for mysql database to golang struct`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(tools.GetJsonStr(config.GetMysqlDbInfo()))
//开始做事情
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Host, "host", "H", "", "数据库地址.")
rootCmd.MarkFlagRequired("host")
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Username, "user", "u", "", "用户名.")
rootCmd.MarkFlagRequired("user")
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Password, "password", "p", "", "密码.")
rootCmd.MarkFlagRequired("password")
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Database, "database", "d", "", "数据库名")
rootCmd.MarkFlagRequired("database")
rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
validate := validator.New()
err := validate.Struct(mysqlInfo)
if err != nil {
err1 := validate.Struct(config.GetMysqlDbInfo())
if err1 != nil {
fmt.Println("Can't read cmd: using -h, --help) to get more imfo")
fmt.Println("error info: ", err, err1)
os.Exit(1)
} else {
fmt.Println("using default config info.")
}
} else {
config.SetMysqlDbInfo(&mysqlInfo)
}
}

@ -0,0 +1,39 @@
package config
import "fmt"
//
type Config struct {
CfgBase
MySQLInfo MysqlDbInfo `toml:"mysql_info"`
}
//mysql 数据库信息
type MysqlDbInfo struct {
Host string `validate:"required"` //地址
Port int `validate:"required"` //端口号
Username string `validate:"required"` //用户名
Password string `validate:"required"` //密码
Database string `validate:"required"` //数据库名
}
//更新mysql配置信息
func SetMysqlDbInfo(info *MysqlDbInfo) {
_map.MySQLInfo = *info
}
//获取mysql配置信息
func GetMysqlDbInfo() MysqlDbInfo {
return _map.MySQLInfo
}
//获取mysql 连接字符串
func GetMysqlConStr() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
_map.MySQLInfo.Username,
_map.MySQLInfo.Password,
_map.MySQLInfo.Host,
_map.MySQLInfo.Port,
_map.MySQLInfo.Database,
)
}

@ -0,0 +1,55 @@
package config
import (
"fmt"
"public/tools"
"github.com/BurntSushi/toml"
)
//
type CfgBase struct {
SerialNumber string `json:"serial_number" toml:"serial_number"` //版本号
ServiceName string `json:"service_name" toml:"service_name"` //service名字
ServiceDisplayname string `json:"service_displayname" toml:"service_displayname"` //显示名
SerciceDesc string `json:"sercice_desc" toml:"sercice_desc"` //service描述
IsDev bool `json:"is_dev" toml:"is_dev"` //是否是开发版本
}
var _map = Config{}
func init() {
onInit()
}
func onInit() {
path := tools.GetModelPath()
err := InitFile(path + "/config.toml")
if err != nil {
fmt.Println("InitFile: ", err.Error())
return
}
}
//OnIsDev ... 是否是开发版本
func OnIsDev() bool {
return _map.IsDev
}
//InitFile ...
func InitFile(filename string) error {
if _, err := toml.DecodeFile(filename, &_map); err != nil {
fmt.Println("read toml error: ", err.Error())
return err
}
return nil
}
//获取service配置信息
func GetServiceConfig() (name, displayName, desc string) {
name = _map.ServiceName
displayName = _map.ServiceDisplayname
desc = _map.SerciceDesc
return
}

@ -0,0 +1,19 @@
package config
import (
"os"
"strings"
)
const (
test_file = `
`
)
//判断是否在测试环境下使用
func IsRunTesting() bool {
if len(os.Args) > 1 {
return strings.HasPrefix(os.Args[1], "-test")
}
return false
}

@ -0,0 +1,9 @@
package main
import (
"data/cmd"
)
func main() {
cmd.Execute()
}
Loading…
Cancel
Save