Skip to content

Commit adaf607

Browse files
committed
Factored json output and added Error field
1 parent d21bd88 commit adaf607

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

main.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,39 @@ func main() {
5050
cmd = strings.ToUpper(strings.TrimSpace(cmd))
5151
switch cmd {
5252
case "START":
53-
outputMessage("start", "OK")
53+
output(&genericMessageJSON{
54+
EventType: "start",
55+
Message: "OK",
56+
})
5457
case "STOP":
5558
if syncStarted {
5659
syncCloseChan <- true
5760
syncStarted = false
5861
}
59-
outputMessage("stop", "OK")
62+
output(&genericMessageJSON{
63+
EventType: "stop",
64+
Message: "OK",
65+
})
6066
case "LIST":
6167
outputList()
6268
case "QUIT":
63-
outputMessage("quit", "OK")
69+
output(&genericMessageJSON{
70+
EventType: "quit",
71+
Message: "OK",
72+
})
6473
os.Exit(0)
6574
case "START_SYNC":
6675
if syncStarted {
67-
outputMessage("startSync", "OK")
6876
} else if close, err := startSync(); err != nil {
6977
outputError(err)
7078
} else {
7179
syncCloseChan = close
7280
syncStarted = true
7381
}
82+
output(&genericMessageJSON{
83+
EventType: "start_sync",
84+
Message: "OK",
85+
})
7486
default:
7587
outputError(fmt.Errorf("Command %s not supported", cmd))
7688
}
@@ -135,16 +147,14 @@ func newBoardPortJSON(port *enumerator.PortDetails) *boardPortJSON {
135147
return portJSON
136148
}
137149

138-
type messageOutputJSON struct {
150+
type genericMessageJSON struct {
139151
EventType string `json:"eventType"`
152+
Error bool `json:"error,omitempty"`
140153
Message string `json:"message"`
141154
}
142155

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, "", " ")
148158
if err != nil {
149159
outputError(err)
150160
} else {
@@ -153,7 +163,11 @@ func outputMessage(eventType, message string) {
153163
}
154164

155165
func outputError(err error) {
156-
outputMessage("error", err.Error())
166+
output(&genericMessageJSON{
167+
EventType: "command_error",
168+
Error: true,
169+
Message: err.Error(),
170+
})
157171
}
158172

159173
var stdoutMutext sync.Mutex

0 commit comments

Comments
 (0)