v0.8.15 + import 命令
This commit is contained in:
parent
d053596a9b
commit
177cc45d5e
5 changed files with 1318 additions and 1 deletions
77
cmd/import.go
Normal file
77
cmd/import.go
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/yaoapp/gou"
|
||||||
|
"github.com/yaoapp/xiang/config"
|
||||||
|
"github.com/yaoapp/xiang/engine"
|
||||||
|
)
|
||||||
|
|
||||||
|
var importRenew bool
|
||||||
|
var importChunk int
|
||||||
|
var importCmd = &cobra.Command{
|
||||||
|
Use: "import",
|
||||||
|
Short: "导入数据(alpha)",
|
||||||
|
Long: `导入CSV/Excel数据`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
Boot()
|
||||||
|
|
||||||
|
if len(args) < 3 {
|
||||||
|
fmt.Fprintln(os.Stderr, "参数错误", args)
|
||||||
|
fmt.Println("xiang import <数据模型> <文件名称> <导入配置>")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := args[0]
|
||||||
|
filename := args[1]
|
||||||
|
configfile := args[2]
|
||||||
|
|
||||||
|
// 检查数据文件
|
||||||
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
|
fmt.Println(color.RedString("数据文件不存在: %s", filename))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(configfile); os.IsNotExist(err) {
|
||||||
|
fmt.Println(color.RedString("配置文件不存在: %s", configfile))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(configfile)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(color.RedString("读取配置文件失败: %s", err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
impt := gou.NewImport(importChunk)
|
||||||
|
err = jsoniter.Unmarshal(content, impt)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(color.RedString("解析配置文件=失败: %s", err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载数据模型
|
||||||
|
engine.Load(config.Conf)
|
||||||
|
if importRenew {
|
||||||
|
gou.Select(name).Migrate(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := impt.InsertCSV(filename, name)
|
||||||
|
for _, err := range *resp.Errors {
|
||||||
|
fmt.Println(color.RedString("%s line: %d", err.Message, err.Line))
|
||||||
|
}
|
||||||
|
fmt.Println(color.WhiteString("成功: %d, 失败: %d, 总共: %d", resp.Success, resp.Failure, resp.Total))
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
importCmd.PersistentFlags().BoolVarP(&importRenew, "renew", "r", false, "清空数据模型原有数据")
|
||||||
|
importCmd.PersistentFlags().IntVarP(&importChunk, "chunk", "c", 1000, "每次处理数量(数值越大,处理越快,内存占用越大)")
|
||||||
|
}
|
||||||
|
|
@ -34,6 +34,7 @@ func init() {
|
||||||
migrateCmd,
|
migrateCmd,
|
||||||
inspectCmd,
|
inspectCmd,
|
||||||
startCmd,
|
startCmd,
|
||||||
|
importCmd,
|
||||||
)
|
)
|
||||||
rootCmd.SetHelpCommand(helpCmd)
|
rootCmd.SetHelpCommand(helpCmd)
|
||||||
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", "指定应用目录")
|
rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", "指定应用目录")
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// VERSION 版本号
|
// VERSION 版本号
|
||||||
const VERSION = "0.8.14"
|
const VERSION = "0.8.15"
|
||||||
|
|
||||||
// DOMAIN 许可域
|
// DOMAIN 许可域
|
||||||
const DOMAIN = "*.iqka.com"
|
const DOMAIN = "*.iqka.com"
|
||||||
|
|
|
||||||
1229
tests/import/manu.csv
Normal file
1229
tests/import/manu.csv
Normal file
File diff suppressed because it is too large
Load diff
10
tests/import/manu.json
Normal file
10
tests/import/manu.json
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"mapping": {
|
||||||
|
"name": "name",
|
||||||
|
"short_name": "smalltitle",
|
||||||
|
"rank": "priority"
|
||||||
|
},
|
||||||
|
"option": {
|
||||||
|
"strict": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue