Browse Source

新增命令行参数

developer
zhangshu 8 months ago
parent
commit
efac724608
  1. 4
      main_program/common/Constant.go
  2. 3
      main_program/config.yml
  3. 5
      main_program/config/EnvConfig.go
  4. 23
      main_program/main.go
  5. 12
      main_program/moveData/moveDataService.go

4
main_program/common/Constant.go

@ -6,3 +6,7 @@ import (
var ConfigFile string var ConfigFile string
var MyEnv config.EnvConfig var MyEnv config.EnvConfig
var Command string
var Table string
var Dbpath string
var XlsxPath string

3
main_program/config.yml

@ -3,8 +3,9 @@ log:
logpath: main_program logpath: main_program
command: move_data command: move_data
movedata: movedata:
table: DownLoadInfo table: Channel
dbpath: D:/Work/Code/youtube_dev/youtube_prod.db dbpath: D:/Work/Code/youtube_dev/youtube_prod.db
xlsxpath: D:/Work/Code/youtube_dev/youtube-golang/main_program/moveData/channel_region.xlsx
mysql: mysql:
host: 47.108.20.249:3306 host: 47.108.20.249:3306
user: root user: root

5
main_program/config/EnvConfig.go

@ -13,8 +13,9 @@ type LogEntity struct {
} }
type MoveDataEntity struct { type MoveDataEntity struct {
Table string Table string
DBPath string DBPath string
XlsxPath string
} }
type MysqlEntity struct { type MysqlEntity struct {

23
main_program/main.go

@ -14,6 +14,10 @@ import (
func init() { func init() {
flag.StringVar(&common.ConfigFile, "cf", "config.yml", "配置文件名") flag.StringVar(&common.ConfigFile, "cf", "config.yml", "配置文件名")
flag.StringVar(&common.Command, "cmd", "", "命令")
flag.StringVar(&common.Table, "table", "", "需要迁移的表")
flag.StringVar(&common.Dbpath, "sqlite", "", "sqlite文件地址")
flag.StringVar(&common.XlsxPath, "excel", "", "excel文件地址")
} }
func main() { func main() {
@ -29,17 +33,32 @@ func main() {
config.Logger.Info("初始化Logger成功...") config.Logger.Info("初始化Logger成功...")
// 判断command // 判断command
command := common.MyEnv.Command command := common.MyEnv.Command
config.Logger.Infof("Command:%s", common.MyEnv.Command) if common.Command != "" {
command = common.Command
}
config.Logger.Infof("Command:%s", command)
if command == "move_data" { if command == "move_data" {
// 判断参数 // 判断参数
table := common.MyEnv.MoveData.Table table := common.MyEnv.MoveData.Table
if common.Table != "" {
table = common.Table
}
dbPath := common.MyEnv.MoveData.DBPath dbPath := common.MyEnv.MoveData.DBPath
if common.Dbpath != "" {
dbPath = common.Dbpath
}
// "D:/Work/Code/youtube_dev/youtube-golang/main_program/moveData/channel_region.xlsx"
xlsxPath := common.MyEnv.MoveData.XlsxPath
if common.XlsxPath != "" {
xlsxPath = common.XlsxPath
}
if table == "" || dbPath == "" { if table == "" || dbPath == "" {
config.Logger.Error("move_data配置文件错误...") config.Logger.Error("move_data配置文件错误...")
return return
} }
config.Logger.Infof("开始调用move_data方法") config.Logger.Infof("开始调用move_data方法")
move_data.MoveDataService.Start(table, dbPath, common.MyEnv.Mysql) config.Logger.Infof("table:%s dbPath:%s xlsxPath:%s", table, dbPath, xlsxPath)
move_data.MoveDataService.Start(table, dbPath, common.MyEnv.Mysql, xlsxPath)
} }
} }

12
main_program/moveData/moveDataService.go

@ -18,7 +18,7 @@ type moveDataService struct{}
var MoveDataService moveDataService var MoveDataService moveDataService
func (m *moveDataService) Start(table string, sqlitePath string, mysqlConfig config.MysqlEntity) { func (m *moveDataService) Start(table string, sqlitePath string, mysqlConfig config.MysqlEntity, xlsxPath string) {
config.Logger.Info("开始迁移sqlite3到mysql...") config.Logger.Info("开始迁移sqlite3到mysql...")
sqliteDB, err := sql.Open("sqlite3", sqlitePath) sqliteDB, err := sql.Open("sqlite3", sqlitePath)
if err != nil { if err != nil {
@ -32,7 +32,11 @@ func (m *moveDataService) Start(table string, sqlitePath string, mysqlConfig con
} }
config.Logger.Info("连接成功mysql...") config.Logger.Info("连接成功mysql...")
if table == "Channel" { if table == "Channel" {
m.moveChannel(sqliteDB, mysqlDB) if xlsxPath == "" {
config.Logger.Error("xlsxPath not existx...")
return
}
m.moveChannel(sqliteDB, mysqlDB, "D:/Work/Code/youtube_dev/youtube-golang/main_program/moveData/channel_region.xlsx")
} else if table == "Videos" { } else if table == "Videos" {
m.moveVideos(sqliteDB, mysqlDB) m.moveVideos(sqliteDB, mysqlDB)
} else if table == "DownLoadInfo" { } else if table == "DownLoadInfo" {
@ -40,9 +44,9 @@ func (m *moveDataService) Start(table string, sqlitePath string, mysqlConfig con
} }
} }
func (m *moveDataService) moveChannel(sqliteDB *sql.DB, mysqlDB *gorm.DB) { func (m *moveDataService) moveChannel(sqliteDB *sql.DB, mysqlDB *gorm.DB, xlsxPath string) {
config.Logger.Info("读取xlsx获取region...") config.Logger.Info("读取xlsx获取region...")
file, err := xlsx.OpenFile("D:/Work/Code/youtube_dev/youtube-golang/main_program/moveData/channel_region.xlsx") file, err := xlsx.OpenFile(xlsxPath)
if err != nil { if err != nil {
config.Logger.Fatalf("Error opening file: %s", err) config.Logger.Fatalf("Error opening file: %s", err)
} }

Loading…
Cancel
Save