go_dreamfactory/cmd/v2/ui/tool_term.go
2022-09-29 14:57:49 +08:00

473 lines
13 KiB
Go

package ui
import (
"errors"
"fmt"
"go_dreamfactory/cmd/v2/lib/common"
"go_dreamfactory/cmd/v2/model"
"go_dreamfactory/cmd/v2/service"
"go_dreamfactory/cmd/v2/service/observer"
"image/color"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
type appTerm struct {
appAdapter
obs observer.Observer
sshService *service.SSHService
jsonList *fileList //json列表
cProgress *widget.ProgressBarInfinite //连接进度条进度条
upProgress *widget.ProgressBar //上传进度条
endProgress sync.WaitGroup
}
func (this *appTerm) LazyInit(obs observer.Observer) error {
this.obs = obs
this.sshService = &service.SSHService{}
this.jsonList = NewFileList()
//progress
this.cProgress = widget.NewProgressBarInfinite()
this.cProgress.Hide()
this.upProgress = widget.NewProgressBar()
this.upProgress.Hide()
this.tabItem = container.NewTabItemWithIcon(common.TOOLBAR_TERM, theme.MailSendIcon(), nil)
content := container.NewMax()
content.Objects = []fyne.CanvasObject{}
//load term conf
sshConf := service.GetDbService().GetSSHConf(common.BUCKET_SSHCONF)
logrus.Debugf("%v", sshConf)
//left-top
localDir := widget.NewEntry()
localDir.PlaceHolder = `本地Json目录`
remoteDir := widget.NewEntry()
remoteDir.PlaceHolder = `远程目录`
remoteDir.Text = "/home/liwei/go_dreamfactory/bin/json"
//config
ip := widget.NewEntry()
ip.PlaceHolder = "10.0.0.9"
port := widget.NewEntry()
port.Text = "22" //default port
port.PlaceHolder = "端口"
userName := widget.NewEntry()
userName.PlaceHolder = "账号"
password := widget.NewPasswordEntry()
password.PlaceHolder = "密码"
passwordItem := &widget.FormItem{Text: "密码:", Widget: password}
configForm := widget.NewForm(
&widget.FormItem{Text: "IP:", Widget: container.NewBorder(nil, nil, nil, port, ip)},
&widget.FormItem{Text: "用户名:", Widget: userName},
passwordItem,
widget.NewFormItem("Json目录", container.NewBorder(nil, nil, nil, widget.NewButtonWithIcon("", theme.FolderIcon(), func() {
openFolder(localDir)
}), localDir)),
widget.NewFormItem("远程目录", remoteDir),
)
configForm.Items[4].HintText = "谨慎修改远程目录"
//svn form
lubanAddr := widget.NewEntry()
lubanAddr.PlaceHolder = "Luban服务IP"
workDir := widget.NewEntry()
workDir.PlaceHolder = "工作目录"
lubanCli := widget.NewEntry()
lubanCli.PlaceHolder = "luban客户端可执行文件"
//define
define := widget.NewEntry()
define.PlaceHolder = "定义文件"
define.Text = `Defines\\__root__.xml`
dataDir := widget.NewEntry()
dataDir.PlaceHolder = "Data目录"
jsonDir := widget.NewEntry()
jsonDir.PlaceHolder = "Json目录"
svnForm := widget.NewForm(
&widget.FormItem{Text: "服务地址", Widget: lubanAddr},
&widget.FormItem{Text: "工作目录", Widget: container.NewBorder(nil, nil, nil, widget.NewButtonWithIcon("", theme.FolderIcon(), func() {
openFolder(workDir)
}), workDir)},
&widget.FormItem{Text: "LubanCli", Widget: lubanCli},
&widget.FormItem{Text: "Data目录", Widget: dataDir},
&widget.FormItem{Text: "JSON目录", Widget: jsonDir},
)
//set input entry
if sshConf != nil {
ip.Text = sshConf.Ip
port.Text = sshConf.Port
userName.Text = sshConf.UserName
password.Text = sshConf.Password
localDir.Text = sshConf.LocalDir
remoteDir.Text = sshConf.RemoteDir
// //
lubanAddr.Text = sshConf.ServerIp
workDir.Text = sshConf.WorkDir
lubanCli.Text = sshConf.LubanCli
dataDir.Text = sshConf.DataDir
jsonDir.Text = sshConf.JsonDir
}
// 解决文本没显示的问题
passwordItem.Widget.Refresh()
ip.Refresh()
localDir.Refresh()
workDir.Refresh()
// save func
saveFunc := func() {
if err := service.GetDbService().SaveSSHConf(&model.SSHModel{
Ip: ip.Text,
UserName: userName.Text,
Password: password.Text,
Port: port.Text,
LocalDir: localDir.Text,
RemoteDir: remoteDir.Text,
////
ServerIp: lubanAddr.Text,
WorkDir: workDir.Text,
LubanCli: lubanCli.Text,
DataDir: dataDir.Text,
JsonDir: jsonDir.Text,
}); err != nil {
logrus.WithField("err", err).Debug("保存配置")
}
}
saveBtn1 := widget.NewButtonWithIcon("保存配置", theme.DocumentSaveIcon(), saveFunc)
saveBtn2 := widget.NewButtonWithIcon("保存配置", theme.DocumentSaveIcon(), saveFunc)
// conn func
connBtn := &widget.Button{Text: "连接", Icon: theme.MailForwardIcon()}
disConnBtn := &widget.Button{Text: "断开", Icon: theme.CancelIcon()}
syncBtn := &widget.Button{Text: "同步JSON", Icon: theme.ConfirmIcon()}
refreshBtn := &widget.Button{Text: "刷新", Icon: theme.ViewRefreshIcon()}
svnBtn := &widget.Button{Text: "SVN更新", Icon: theme.ConfirmIcon()}
explorBtn := &widget.Button{Text: "资源管理器", Icon: theme.FolderIcon()}
searchEntry := widget.NewEntry()
// 全选/全取消
allSelBtn := &widget.Button{Icon: theme.CheckButtonCheckedIcon()}
allCancelBtn := &widget.Button{Icon: theme.CheckButtonIcon()}
allSelBtn.Hide()
// 连接
connBtn.OnTapped = func() {
this.cProgress.Show()
this.cProgress.Start()
ciphers := []string{}
if ip.Text == "" {
dialog.ShowError(errors.New("IP未填写"), toolWin.w)
return
}
if userName.Text == "" {
dialog.ShowError(errors.New("账号未填写"), toolWin.w)
return
}
if password.Text == "" {
dialog.ShowError(errors.New("密码未填写"), toolWin.w)
return
}
if port.Text == "" {
dialog.ShowError(errors.New("端口未填写"), toolWin.w)
return
}
if localDir.Text == "" {
dialog.ShowError(errors.New("JSON目录未填写"), toolWin.w)
return
}
if remoteDir.Text == "" {
dialog.ShowError(errors.New("远程目录未填写"), toolWin.w)
return
}
connBtn.Disable()
err := this.sshService.Connect(userName.Text, password.Text, ip.Text, "", cast.ToInt(port.Text), ciphers)
if err != nil {
dialog.ShowError(err, toolWin.w)
this.cProgress.Stop()
this.cProgress.Hide()
connBtn.Enable()
return
} else {
this.jsonList.loadItem(localDir.Text)
this.cProgress.Stop()
this.cProgress.Hide()
disConnBtn.Enable()
syncBtn.Enable()
this.upProgress.Hide()
allCancelBtn.Show()
allSelBtn.Hide()
refreshBtn.Enable()
}
}
// 刷新JSON列表
refreshBtn.Disable()
reloadItem := func() {
defer func() {
syncBtn.Enable()
allSelBtn.Hide()
allCancelBtn.Show()
}()
this.jsonList.loadItem(localDir.Text)
}
refreshBtn.OnTapped = reloadItem
// 断开
disConnBtn.Disable()
disConnBtn.OnTapped = func() {
defer func() {
this.jsonList.reset()
connBtn.Enable()
disConnBtn.Disable()
syncBtn.Disable()
allSelBtn.Hide()
allCancelBtn.Show()
refreshBtn.Disable()
}()
this.sshService.Close()
}
//资源管理器
explorBtn.OnTapped = func() {
logrus.Debug(localDir.Text)
if runtime.GOOS == "windows" {
if err := exec.Command("explorer", filepath.Join(localDir.Text)).Start(); err != nil {
dialog.ShowError(err, toolWin.w)
return
}
}
}
//使用说明
helpBtn1 := widget.NewButtonWithIcon("", theme.QuestionIcon(), func() {
quesWin := fyne.CurrentApp().NewWindow("同步JSON使用说明")
quesWin.SetContent(widget.NewRichTextFromMarkdown(
`
* 同步Json的操作仅限于数值热更,非结构热更
* 将json文件热更到内网测试环境
* 提示:
* IP:10.0.0.9 22 (保持一致)
* 用户名:root(保持一致)
* 密码:(服务端同学提供)
* Json目录:(json文件目录全路径)
* 远程目录:/home/liwei/go_dreamfactory/bin/json/(保持一致)
* 全部填写完点击【保存配置】
* 点击【连接】,右侧面板选择要同步的文件
`))
quesWin.Resize(fyne.NewSize(350, 200))
quesWin.SetFixedSize(true)
quesWin.CenterOnScreen()
quesWin.Show()
})
helpBtn2 := widget.NewButtonWithIcon("", theme.QuestionIcon(), func() {
quesWin := fyne.CurrentApp().NewWindow("SVN更新使用说明")
c := widget.NewRichTextFromMarkdown(
`
* 基于Luban工具生成json
* 建议每次同步JSON之前先点击SVN更新
* 提示:
* 服务地址:10.0.1.11 (保持一致)
* 工作目录:svn下ExcelFile目录全路径
* LubanCli:Luban.Client\Luban.Client.exe (保持一致)
* Data目录:Datas(保持一致)
* JSON目录:output\json(保持一致)
* 全部填写完点击【保存配置】
* 点击【SVN更新】生成json文件
`,
)
quesWin.SetContent(c)
quesWin.Resize(fyne.NewSize(350, 200))
quesWin.SetFixedSize(true)
quesWin.CenterOnScreen()
quesWin.Show()
})
//同步JSON
syncBtn.Disable()
syncBtn.OnTapped = func() {
syncBtn.Disable()
defer func() {
syncBtn.Enable()
this.upProgress.Hide()
}()
if this.sshService.Client == nil {
dialog.ShowError(errors.New("请先连接终端"), toolWin.w)
return
}
this.upProgress.Show()
this.upProgress.SetValue(0)
len := len(this.jsonList.selItemIds)
num := 0.0
increment := func(wg *sync.WaitGroup) {
num += float64(1) / float64(len)
this.upProgress.SetValue(num)
wg.Done()
}
for _, fileName := range this.jsonList.selItemIds {
this.endProgress.Add(1)
go func(fn string) {
// time.Sleep(time.Second * time.Duration(rand.Intn(3)))
if err := this.sshService.ScpCopy(filepath.Join(localDir.Text, fn), remoteDir.Text); err != nil {
logrus.WithField("err", err).Error("同步json")
showTip(err.Error())
}
increment(&this.endProgress)
// 移除已上传的
this.jsonList.deleteItem(fn)
showTip(fmt.Sprintf("%s 成功上传", fn))
}(fileName)
}
this.endProgress.Wait()
this.upProgress.SetValue(1)
}
// SVN更新
svnBtn.OnTapped = func() {
this.cProgress.Show()
this.cProgress.Start()
svnBtn.Disable()
defer func() {
this.cProgress.Hide()
this.cProgress.Stop()
svnBtn.Enable()
}()
commandStr := `%s -h %s -j cfg -- -d %s --input_data_dir %s --output_data_dir %s --gen_types data_json -s server`
arg := fmt.Sprintf(commandStr,
filepath.Join(workDir.Text, lubanCli.Text),
lubanAddr.Text,
filepath.Join(workDir.Text, define.Text),
filepath.Join(workDir.Text, dataDir.Text),
filepath.Join(workDir.Text, jsonDir.Text),
)
logrus.Debug(arg)
c := exec.Command("cmd.exe", "/c", arg)
if err := c.Run(); err != nil {
dialog.ShowError(err, toolWin.w)
return
}
}
// 全部取消
allCancelBtn.OnTapped = func() {
defer func() {
allCancelBtn.Hide()
allSelBtn.Show()
}()
for i, v := range this.jsonList.cachedList.Items {
this.jsonList.cachedList.Items[i].Checked = true
this.jsonList.selItemIds = append(this.jsonList.selItemIds, v.Text)
this.jsonList.itemList.UpdateItem(i, widget.NewCheck(v.Text, nil))
}
this.jsonList.itemList.Refresh()
}
// 全选
allSelBtn.OnTapped = func() {
defer func() {
allCancelBtn.Show()
allSelBtn.Hide()
}()
this.jsonList.selItemIds = []string{}
for i, v := range this.jsonList.cachedList.Items {
this.jsonList.cachedList.Items[i].Checked = false
this.jsonList.itemList.UpdateItem(i, widget.NewCheck(v.Text, nil))
}
this.jsonList.itemList.Refresh()
}
// 搜索
searchEntry.PlaceHolder = "搜索"
searchEntry.OnChanged = func(s string) {
if s == "" {
reloadItem()
} else {
// go func() {
newList := []Item{}
for _, v := range this.jsonList.searchItem {
if strings.Contains(v.Text, s) {
newList = append(newList, v)
}
}
this.jsonList.cachedList.Items = newList
this.jsonList.itemList.Refresh()
// }()
}
}
// 创建json列表
this.jsonList.itemList = this.jsonList.createList()
btns1 := container.NewHBox(helpBtn1, &layout.Spacer{}, saveBtn1, connBtn, disConnBtn)
btns2 := container.NewHBox(helpBtn2, &layout.Spacer{}, saveBtn2, svnBtn)
split := container.NewHSplit(container.NewVBox(canvas.NewText("---只能在非上产环境!!!同步Json的操作仅限于数值热更,非结构热更---", color.RGBA{255, 0, 0, 255}), configForm,
btns1, svnForm, btns2, this.cProgress), container.NewBorder(
container.NewBorder(nil, nil, container.NewHBox(allCancelBtn, allSelBtn, syncBtn, refreshBtn), container.NewHBox(explorBtn), searchEntry),
nil, nil, nil, this.jsonList.itemList))
split.Offset = 0.45
content.Objects = append(content.Objects, container.NewBorder(nil, this.upProgress, nil, nil, split))
this.tabItem.Content = content
return nil
}
func (a *appTerm) GetAppName() string {
return common.TOOLBAR_TERM
}
func (a *appTerm) OnClose() bool {
dialog.ShowConfirm("关闭终端", "你希望断开连接吗?", func(b bool) {
if !b {
return
}
if a.sshService.Client != nil {
a.sshService.Client.Close()
}
}, toolWin.w)
return true
}