@@ -50,27 +50,39 @@ func main() {
50
50
cmd = strings .ToUpper (strings .TrimSpace (cmd ))
51
51
switch cmd {
52
52
case "START" :
53
- outputMessage ("start" , "OK" )
53
+ output (& genericMessageJSON {
54
+ EventType : "start" ,
55
+ Message : "OK" ,
56
+ })
54
57
case "STOP" :
55
58
if syncStarted {
56
59
syncCloseChan <- true
57
60
syncStarted = false
58
61
}
59
- outputMessage ("stop" , "OK" )
62
+ output (& genericMessageJSON {
63
+ EventType : "stop" ,
64
+ Message : "OK" ,
65
+ })
60
66
case "LIST" :
61
67
outputList ()
62
68
case "QUIT" :
63
- outputMessage ("quit" , "OK" )
69
+ output (& genericMessageJSON {
70
+ EventType : "quit" ,
71
+ Message : "OK" ,
72
+ })
64
73
os .Exit (0 )
65
74
case "START_SYNC" :
66
75
if syncStarted {
67
- outputMessage ("startSync" , "OK" )
68
76
} else if close , err := startSync (); err != nil {
69
77
outputError (err )
70
78
} else {
71
79
syncCloseChan = close
72
80
syncStarted = true
73
81
}
82
+ output (& genericMessageJSON {
83
+ EventType : "start_sync" ,
84
+ Message : "OK" ,
85
+ })
74
86
default :
75
87
outputError (fmt .Errorf ("Command %s not supported" , cmd ))
76
88
}
@@ -135,16 +147,14 @@ func newBoardPortJSON(port *enumerator.PortDetails) *boardPortJSON {
135
147
return portJSON
136
148
}
137
149
138
- type messageOutputJSON struct {
150
+ type genericMessageJSON struct {
139
151
EventType string `json:"eventType"`
152
+ Error bool `json:"error,omitempty"`
140
153
Message string `json:"message"`
141
154
}
142
155
143
- func outputMessage (eventType , message string ) {
144
- d , err := json .MarshalIndent (& messageOutputJSON {
145
- EventType : eventType ,
146
- Message : message ,
147
- }, "" , " " )
156
+ func output (msg interface {}) {
157
+ d , err := json .MarshalIndent (msg , "" , " " )
148
158
if err != nil {
149
159
outputError (err )
150
160
} else {
@@ -153,7 +163,11 @@ func outputMessage(eventType, message string) {
153
163
}
154
164
155
165
func outputError (err error ) {
156
- outputMessage ("error" , err .Error ())
166
+ output (& genericMessageJSON {
167
+ EventType : "command_error" ,
168
+ Error : true ,
169
+ Message : err .Error (),
170
+ })
157
171
}
158
172
159
173
var stdoutMutext sync.Mutex
0 commit comments