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 { cardButton := widget.NewButton("启动", func() { runExecFile(data, w) }) openDirButton := widget.NewButton("打开目录", func() { openDir(data, w) }) card := widget.NewCard(data.Title, data.Text, container.NewVBox(cardButton, openDirButton)) return card } 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(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) } }() }