package ui import ( "errors" "go_dreamfactory/cmd/v2/lib/common" "go_dreamfactory/cmd/v2/model" "go_dreamfactory/cmd/v2/service" "go_dreamfactory/cmd/v2/service/observer" "fyne.io/fyne/v2" "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 } func (this *appTerm) LazyInit(obs observer.Observer) error { this.obs = obs this.sshService = &service.SSHService{} this.tabItem = container.NewTabItemWithIcon(common.TOOLBAR_TERM, theme.ContentCopyIcon(), nil) content := container.NewMax() content.Objects = []fyne.CanvasObject{} //load term conf sshConf := service.GetDbService().GetSSHConf(common.BUCKET_SSHCONF) logrus.Debugf("%v", sshConf) //cmd input cmdLast := widget.NewMultiLineEntry() cmdLast.PlaceHolder = "命令输入,使用;分隔" // output panel output := widget.NewMultiLineEntry() logrus.Debug(output.CursorRow) //config ip := widget.NewEntry() 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} //set input entry if sshConf != nil { ip.Text = sshConf.Ip port.Text = sshConf.Port userName.Text = sshConf.UserName password.Text = sshConf.Password cmdLast.Text = sshConf.CmdLast } configForm := widget.NewForm( &widget.FormItem{Text: "IP:", Widget: container.NewBorder(nil, nil, nil, port, ip)}, &widget.FormItem{Text: "用户名:", Widget: userName}, passwordItem, ) // 非明文显示 passwordItem.Widget.Refresh() // save func saveBtn := widget.NewButtonWithIcon("保存配置", theme.DocumentSaveIcon(), func() { if err := service.GetDbService().SaveSSHConf(&model.SSHModel{ Ip: ip.Text, UserName: userName.Text, Password: password.Text, Port: port.Text, CmdLast: cmdLast.Text, }); err != nil { logrus.WithField("err", err).Debug("保存配置") } logrus.Debug("save term conf") }) // conn func connBtn := &widget.Button{Text: "连接", Icon: theme.ConfirmIcon()} connBtn.OnTapped = func() { ciphers := []string{} 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 } 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) connBtn.Enable() return } } //excute excuteBtn := &widget.Button{Text: "执行", Icon: theme.ConfirmIcon()} excuteBtn.TypedKey(&fyne.KeyEvent{Name: fyne.KeyEnter}) excuteBtn.OnTapped = func() { output.Text = "" excuteBtn.Disable() defer func() { excuteBtn.Enable() }() if this.sshService.Client == nil { dialog.ShowError(errors.New("请先连接终端"), toolWin.w) return } this.sshService.RunShell(cmdLast.Text) output.SetText(this.sshService.LastResult) } btns := container.NewHBox(excuteBtn, &layout.Spacer{}, saveBtn, connBtn) // layout split := container.NewVSplit(container.NewGridWithColumns(2, cmdLast, container.NewBorder(configForm, btns, widget.NewSeparator(), nil)), output) split.Offset = 0.25 content.Objects = append(content.Objects, 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 }