2021-12-28 09:18:32 +08:00
|
|
|
|
package migrate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/spf13/cobra"
|
2021-12-28 10:38:02 +08:00
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
"os"
|
2021-12-28 09:18:32 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-12-28 10:38:02 +08:00
|
|
|
|
type Config struct {
|
2021-12-28 10:57:13 +08:00
|
|
|
|
Debug bool `json:"debug" yaml:"debug"`
|
|
|
|
|
Mode string `json:"mode" yaml:"mode"`
|
|
|
|
|
MaxLifetime int `json:"max_lifetime" yaml:"max_lifetime"`
|
|
|
|
|
MaxOpenConns int `json:"max_open_conns" yaml:"max_open_conns"`
|
|
|
|
|
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns"`
|
|
|
|
|
Engines map[string]interface{} `json:"engines" yaml:"engines"`
|
2021-12-28 10:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 10:57:13 +08:00
|
|
|
|
type Mysql struct {
|
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
|
Database string `yaml:"database"`
|
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
|
Parameters string `json:"parameters"`
|
2021-12-28 09:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 10:57:13 +08:00
|
|
|
|
type Sqlite struct {
|
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
|
Name string `yaml:"name"`
|
2021-12-28 09:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
Cmd = &cobra.Command{
|
|
|
|
|
Use: "init",
|
|
|
|
|
Short: "初始化配置",
|
|
|
|
|
Example: "serve init -m mysql -d sciences -u root -p 123456",
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
run()
|
|
|
|
|
},
|
|
|
|
|
}
|
2021-12-28 10:38:02 +08:00
|
|
|
|
file string
|
2021-12-28 09:18:32 +08:00
|
|
|
|
mode string
|
|
|
|
|
|
2021-12-28 10:57:13 +08:00
|
|
|
|
_mysql = new(Mysql)
|
|
|
|
|
_sqlite = new(Sqlite)
|
2021-12-28 09:18:32 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2021-12-28 10:38:02 +08:00
|
|
|
|
Cmd.PersistentFlags().StringVarP(&file, "file", "f", "./default_engine.yaml", "文件存储,现支持yaml文件,默认./default_engine.yaml")
|
2021-12-28 09:18:32 +08:00
|
|
|
|
Cmd.PersistentFlags().StringVarP(&mode, "mode", "m", "mysql", "数据引擎,支持mysql和sqlite,默认mysql")
|
2021-12-28 10:57:13 +08:00
|
|
|
|
Cmd.PersistentFlags().StringVarP(&_mysql.Host, "mysql_host", "H", "127.0.0.1", "主机名,默认127.0.0.1")
|
|
|
|
|
Cmd.PersistentFlags().IntVarP(&_mysql.Port, "mysql_port", "P", 3306, "端口号,默认为3306")
|
|
|
|
|
Cmd.PersistentFlags().StringVarP(&_mysql.Database, "mysql_database", "d", "", "数据库,默认为空")
|
|
|
|
|
Cmd.PersistentFlags().StringVarP(&_mysql.Username, "mysql_username", "u", "root", "用户名,默认为root")
|
|
|
|
|
Cmd.PersistentFlags().StringVarP(&_mysql.Password, "mysql_password", "p", "", "密码,默认为空")
|
|
|
|
|
Cmd.PersistentFlags().StringVar(&_mysql.Parameters, "mysql_parameters", "charset=utf8mb4,utf8&parseTime=True&loc=Local", "附加参数")
|
|
|
|
|
Cmd.PersistentFlags().StringVarP(&_sqlite.Path, "sqlite_path", "a", "data", "Sqlite文件存放地址,默认data")
|
|
|
|
|
Cmd.PersistentFlags().StringVarP(&_sqlite.Name, "sqlite_name", "n", "app.db", "Sqlite文件存放地址,默认app.db")
|
2021-12-28 09:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run() {
|
|
|
|
|
fmt.Println("========================\n=== 初始化项目配置 ===\n========================")
|
|
|
|
|
|
|
|
|
|
// 初始化数据引擎
|
2021-12-28 10:57:13 +08:00
|
|
|
|
//engine := orm.NewInstance(
|
|
|
|
|
// orm.WithDBMode(mode),
|
|
|
|
|
// orm.WithMysqlOption(&logic.Mysql{
|
|
|
|
|
// Host: _mysql.Host, Port: _mysql.Port,
|
|
|
|
|
// Username: _mysql.Username, Password: _mysql.Password,
|
|
|
|
|
// Database: _mysql.Database, Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
|
|
|
|
|
// }),
|
|
|
|
|
// orm.WithSqliteOption(&logic.Sqlite{Path: _sqlite.Path, Name: _sqlite.Name}),
|
|
|
|
|
//).Init()
|
2021-12-28 09:18:32 +08:00
|
|
|
|
|
|
|
|
|
fmt.Println("========================\n=== 数据引擎创建成功 ===\n========================")
|
|
|
|
|
// 迁移数据
|
2021-12-28 10:57:13 +08:00
|
|
|
|
//migrate.NewInstance(migrate.WithGormDBOption(engine.Engine)).Handle()
|
2021-12-28 09:18:32 +08:00
|
|
|
|
|
2021-12-28 10:38:02 +08:00
|
|
|
|
if err := saveFile(file, &Config{
|
|
|
|
|
Debug: true,
|
|
|
|
|
Mode: mode,
|
|
|
|
|
MaxLifetime: 3600,
|
|
|
|
|
MaxOpenConns: 2000,
|
|
|
|
|
MaxIdleConns: 1000,
|
2021-12-28 10:57:13 +08:00
|
|
|
|
Engines: map[string]interface{}{
|
|
|
|
|
"mysql": _mysql,
|
|
|
|
|
"sqlite": _sqlite,
|
2021-12-28 10:38:02 +08:00
|
|
|
|
},
|
|
|
|
|
}); err != nil {
|
|
|
|
|
fmt.Errorf("数据初始化文件错误:%v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-28 09:18:32 +08:00
|
|
|
|
fmt.Println("========================\n=== 数据初始化成功 ===\n========================")
|
|
|
|
|
}
|
2021-12-28 10:38:02 +08:00
|
|
|
|
|
|
|
|
|
func saveFile(_file string, data *Config) error {
|
|
|
|
|
file, err := os.OpenFile(_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
enc := yaml.NewEncoder(file)
|
|
|
|
|
|
|
|
|
|
err = enc.Encode(data)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|