This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
149 lines (139 loc) · 3.35 KB
/
executor.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package execonmcode
import (
"fmt"
"io"
"log"
"os/exec"
"strings"
"github.com/Duet3D/DSF-APIs/godsfapi/v3/commands"
"github.com/Duet3D/DSF-APIs/godsfapi/v3/connection"
"github.com/Duet3D/DSF-APIs/godsfapi/v3/connection/initmessages"
"github.com/Duet3D/DSF-APIs/godsfapi/v3/machine/messages"
)
const (
variablePrefix = "%"
)
type Executor struct {
socketPath string
mode initmessages.InterceptionMode
mCodes map[int64]int
filters []string
commands Commands
execAsync bool
returnOutput bool
flush bool
debug bool
trace bool
}
func NewExecutor(s Settings) *Executor {
mc := make(map[int64]int)
filters := make([]string, 0)
for i, m := range s.MCodes {
mc[m] = i
filters = append(filters, fmt.Sprintf("M%d", m))
if s.Debug {
cmd, args, err := s.Commands.Get(i)
if err != nil {
log.Println(m, err)
}
log.Printf("%d: %s %s", m, cmd, strings.Join(args, " "))
}
}
return &Executor{
socketPath: s.SocketPath,
mode: initmessages.InterceptionMode(s.InterceptionMode),
mCodes: mc,
filters: filters,
commands: s.Commands,
execAsync: s.ExecAsync,
returnOutput: s.ReturnOutput,
flush: !s.NoFlush,
debug: s.Debug,
trace: s.Trace,
}
}
func (e *Executor) Run() error {
ic := connection.InterceptConnection{}
ic.Debug = e.trace
err := ic.Connect(e.mode, nil, e.filters, false, e.socketPath)
if err != nil {
return err
}
defer ic.Close()
for {
c, err := ic.ReceiveCode()
if err != nil {
if err == io.EOF {
log.Println("Connection to DCS closed")
return err
}
if _, ok := err.(*connection.DecodeError); ok {
// If it is "just" a problem with decoding ignore the received code
// as it otherwise will block DCS
ic.IgnoreCode()
}
log.Printf("Error receiving code: %s", err)
continue
}
if c.Type == commands.MCode && c.MajorNumber != nil {
i, ok := e.mCodes[*c.MajorNumber]
if !ok {
ic.IgnoreCode()
continue
}
if e.flush {
success, err := ic.Flush(c.Channel)
if !success || err != nil {
log.Println("Could not Flush. Cancelling code")
ic.CancelCode()
continue
}
}
comd, a, err := e.commands.Get(i)
if err != nil {
ic.ResolveCode(messages.Error, err.Error())
} else {
cmd := exec.Command(comd, e.getArgs(c, a)...)
if e.debug {
log.Println("Executing:", cmd)
}
// If we should exec async run it as goroutine and return success
if e.execAsync {
go cmd.Run()
err = ic.ResolveCode(messages.Success, "")
} else {
output, err := cmd.CombinedOutput()
if err != nil {
err = ic.ResolveCode(messages.Error, fmt.Sprintf("%s: %s", err.Error(), string(output)))
} else {
msg := ""
if e.returnOutput {
msg = string(output)
}
err = ic.ResolveCode(messages.Success, msg)
}
}
if err != nil {
log.Println("Error executing command:", err)
}
}
} else {
ic.IgnoreCode()
}
}
}
func (e *Executor) getArgs(c *commands.Code, args []string) []string {
a := make([]string, 0)
for _, v := range args {
if strings.HasPrefix(v, variablePrefix) {
vl := strings.TrimSpace(strings.ToUpper(strings.TrimLeft(v, variablePrefix)))
if len(vl) == 1 {
if pv := c.Parameter(vl); pv != nil {
v = pv.AsString()
}
}
}
a = append(a, v)
}
return a
}