-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermaction.go
86 lines (68 loc) · 1.85 KB
/
termaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"log"
"os/exec"
"path/filepath"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type TerminalAction struct {
ctx context.Context
terminals map[string]Terminal
}
var app *App
func NewTerminalAction(a *App) *TerminalAction {
app = a
return &TerminalAction{
terminals: getTerminals(),
}
}
func (ta *TerminalAction) startup(ctx context.Context) {
ta.ctx = ctx
runtime.EventsOn(ta.ctx, "onConfigLoaded", ta.onConfigLoaded)
}
func (ta *TerminalAction) onDomReady(_ context.Context) {
response := Response{"success", "terminals mapped", ta.terminals}
runtime.EventsEmit(ta.ctx, "onTerminalsMapped", response)
}
// onConfigLoaded adds additional terminals loaded from the config file
func (ta *TerminalAction) onConfigLoaded(data ...interface{}) {
if len(data) == 0 {
return
}
// Assert the data to a response
resp, ok := data[0].(Response)
if !ok {
log.Println("Error: Data value cannot be asserted to Response.")
return
}
if resp.Status != "success" {
return
}
// Assert the response's data field to a config
config, ok := resp.Data.(*AppConfig)
if !ok {
log.Println("Error: Response data cannot be asserted to AppConfig")
return
}
// Add the terminals to the map
for _, terminal := range config.ExtTerminals {
ta.terminals[terminal.Name] = terminal
}
response := Response{"success", "terminals mapped", ta.terminals}
runtime.EventsEmit(ta.ctx, "onTerminalsMapped", response)
}
// OpenTerminal opens the terminal specified by the supplied name
func (ta *TerminalAction) OpenTerminal(name string) {
terminal := ta.terminals[name]
// Create the command
cmd := exec.Command(terminal.CmdRoot, terminal.OpenCmd...)
// Set the command's working directory
if len(app.filepath) > 0 {
cmd.Dir = filepath.Dir(app.filepath)
}
// Run the command
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}