Files
work_cation/views/showView/execFile.go
T
2024-10-12 15:44:16 +08:00

64 lines
1.7 KiB
Go

package showView
import (
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"os"
"os/exec"
"path/filepath"
"work_cation/cfg"
"work_cation/models"
)
func ExecFileCardView(w fyne.Window, data *models.BaseCard) (fyne.CanvasObject, error) {
cardButton := widget.NewButton("启动", func() { runExecFile(data, w) })
openDirButton := widget.NewButton("打开目录", func() { openDir(data, w) })
card := container.NewVBox(cardButton, openDirButton)
return card, nil
}
func runExecFile(data *models.BaseCard, w fyne.Window) {
infoPath := filepath.Join(cfg.T.CardDir, data.UUID, cfg.T.CardInfo)
bytes, err := os.ReadFile(infoPath)
if err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
return
}
var info models.ExecFiles
err = json.Unmarshal(bytes, &info)
if err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
return
}
ex := exec.Command("cmd", "/c", "start", info.Cmd)
ex.Dir = filepath.Join(cfg.T.CardDir, data.UUID, info.Pwd)
if err := ex.Start(); err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
return
}
go func() {
if err := ex.Wait(); err != nil {
dialog.ShowInformation("测试失败", err.Error(), w)
}
}()
}
func openDir(data *models.BaseCard, w fyne.Window) {
infoPath := filepath.Join(cfg.T.CardDir, data.UUID)
// 打开目录
//utils.File.WinOpenFolder(infoPath)
cmd := exec.Command("explorer", infoPath)
if err := cmd.Start(); err != nil {
dialog.ShowInformation("打开失败", err.Error(), w)
}
go func() {
if err := cmd.Wait(); err != nil && err.Error() != "exit status 1" {
dialog.ShowInformation("测试失败", err.Error(), w)
}
}()
}