Browse Source

新增命令行参数

developer
zhangshu 8 months ago
parent
commit
efac724608
  1. 4
      main_program/common/Constant.go
  2. 3
      main_program/config.yml
  3. 1
      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 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
command: move_data
movedata:
table: DownLoadInfo
table: Channel
dbpath: D:/Work/Code/youtube_dev/youtube_prod.db
xlsxpath: D:/Work/Code/youtube_dev/youtube-golang/main_program/moveData/channel_region.xlsx
mysql:
host: 47.108.20.249:3306
user: root

1
main_program/config/EnvConfig.go

@ -15,6 +15,7 @@ type LogEntity struct {
type MoveDataEntity struct {
Table string
DBPath string
XlsxPath string
}
type MysqlEntity struct {

23
main_program/main.go

@ -14,6 +14,10 @@ import (
func init() {
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() {
@ -29,17 +33,32 @@ func main() {
config.Logger.Info("初始化Logger成功...")
// 判断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" {
// 判断参数
table := common.MyEnv.MoveData.Table
if common.Table != "" {
table = common.Table
}
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 == "" {
config.Logger.Error("move_data配置文件错误...")
return
}
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
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...")
sqliteDB, err := sql.Open("sqlite3", sqlitePath)
if err != nil {
@ -32,7 +32,11 @@ func (m *moveDataService) Start(table string, sqlitePath string, mysqlConfig con
}
config.Logger.Info("连接成功mysql...")
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" {
m.moveVideos(sqliteDB, mysqlDB)
} 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...")
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 {
config.Logger.Fatalf("Error opening file: %s", err)
}

Loading…
Cancel
Save