-
Notifications
You must be signed in to change notification settings - Fork 0
/
dxprocess.go
133 lines (118 loc) · 2.77 KB
/
dxprocess.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/chinenual/synergize/logger"
)
func getExeDirectory() (path string) {
var err error
if path, err = os.Executable(); err != nil {
logger.Errorf("Could not determine the location of the executable: %v\n", err)
path = ""
}
path = filepath.Dir(path)
logger.Infof("ExeDirectory: %s\n", path)
return
}
var dxcmd *exec.Cmd
func dx2synProcessStart(path string) (err error) {
exeName := "dx2syn"
if runtime.GOOS == "windows" {
exeName += ".exe"
}
exePath := filepath.Join(getExeDirectory(), exeName)
dxcmd = exec.Command(exePath, "-makecrt", path)
// suppress the console window on windows
SetCmdNoConsoleWindow(dxcmd)
var stderr io.ReadCloser
if stderr, err = dxcmd.StderrPipe(); err != nil {
return
}
if err = dxcmd.Start(); err != nil {
return
}
go _slurpLog(stderr)
return
}
func dx2SynProcessCancel() (err error) {
if dxcmd != nil && dxcmd.Process != nil {
if err = dxcmd.Process.Kill(); err != nil {
return
}
_finishToUI("Cancelled!")
}
return
}
func _slurpLog(pipe io.ReadCloser) {
for {
r := bufio.NewReader(pipe)
var line []byte
var err error
line, err = r.ReadBytes('\n')
_logToUI(string(line))
if err != nil {
if err == io.EOF {
err = nil
} else {
logger.Errorf("error reading subprocess pipe: %v\n", err)
}
break
}
}
dxcmd.Wait()
msg := "Success"
if !dxcmd.ProcessState.Success() {
msg = fmt.Sprintf("dx2syn returned error status: %d", dxcmd.ProcessState.ExitCode())
}
_finishToUI(msg)
}
var astilectronWindow *astilectron.Window
func dx2synRegisterBridge(w *astilectron.Window) (err error) {
astilectronWindow = w
return
}
func _finishToUI(msg string) (err error) {
logger.Infof("dx2syn finished: %s\n", strings.TrimSpace(msg))
if astilectronWindow == nil {
return
}
var strval string
if err = bootstrap.SendMessage(astilectronWindow, "dx2synFinish", msg,
func(m *bootstrap.MessageIn) {
// Unmarshal payload
if err = json.Unmarshal(m.Payload, &strval); err != nil {
logger.Errorf(" _finishToUI failed to decode json response : %v\n", err)
return
}
}); err != nil {
return
}
return
}
func _logToUI(line string) (err error) {
logger.Infof("dx2syn: %s\n", strings.TrimSpace(line))
if astilectronWindow == nil {
return
}
var strval string
if err = bootstrap.SendMessage(astilectronWindow, "dx2synAddProcessLog", line,
func(m *bootstrap.MessageIn) {
// Unmarshal payload
if err = json.Unmarshal(m.Payload, &strval); err != nil {
logger.Errorf(" _logToUI failed to decode json response : %v\n", err)
return
}
}); err != nil {
return
}
return
}