@@ -46,7 +46,11 @@ func main() {
4646 for {
4747 fullCmd , err := reader .ReadString ('\n' )
4848 if err != nil {
49- outputError (err )
49+ output (& genericMessageJSON {
50+ EventType : "command_error" ,
51+ Error : true ,
52+ Message : err .Error (),
53+ })
5054 os .Exit (1 )
5155 }
5256 split := strings .Split (fullCmd , " " )
@@ -104,18 +108,27 @@ func main() {
104108 os .Exit (0 )
105109 case "START_SYNC" :
106110 if syncStarted {
111+ // sync already started, just acknowledge again...
112+ output (& genericMessageJSON {
113+ EventType : "start_sync" ,
114+ Message : "OK" ,
115+ })
107116 } else if close , err := startSync (); err != nil {
108- outputError (err )
117+ output (& genericMessageJSON {
118+ EventType : "start_sync" ,
119+ Error : true ,
120+ Message : err .Error (),
121+ })
109122 } else {
110123 syncCloseChan = close
111124 syncStarted = true
112125 }
126+ default :
113127 output (& genericMessageJSON {
114- EventType : "start_sync" ,
115- Message : "OK" ,
128+ EventType : "command_error" ,
129+ Error : true ,
130+ Message : fmt .Sprintf ("Command %s not supported" , cmd ),
116131 })
117- default :
118- outputError (fmt .Errorf ("Command %s not supported" , cmd ))
119132 }
120133 }
121134}
@@ -137,23 +150,22 @@ type listOutputJSON struct {
137150func outputList () {
138151 list , err := enumerator .GetDetailedPortsList ()
139152 if err != nil {
140- outputError (err )
153+ output (& genericMessageJSON {
154+ EventType : "list" ,
155+ Error : true ,
156+ Message : err .Error (),
157+ })
141158 return
142159 }
143160 portsJSON := []* boardPortJSON {}
144161 for _ , port := range list {
145162 portJSON := newBoardPortJSON (port )
146163 portsJSON = append (portsJSON , portJSON )
147164 }
148- d , err := json . MarshalIndent (& listOutputJSON {
165+ output (& listOutputJSON {
149166 EventType : "list" ,
150167 Ports : portsJSON ,
151- }, "" , " " )
152- if err != nil {
153- outputError (err )
154- return
155- }
156- syncronizedPrintLn (string (d ))
168+ })
157169}
158170
159171func newBoardPortJSON (port * enumerator.PortDetails ) * boardPortJSON {
@@ -193,20 +205,16 @@ type genericMessageJSON struct {
193205func output (msg interface {}) {
194206 d , err := json .MarshalIndent (msg , "" , " " )
195207 if err != nil {
196- outputError (err )
208+ output (& genericMessageJSON {
209+ EventType : "command_error" ,
210+ Error : true ,
211+ Message : err .Error (),
212+ })
197213 } else {
198214 syncronizedPrintLn (string (d ))
199215 }
200216}
201217
202- func outputError (err error ) {
203- output (& genericMessageJSON {
204- EventType : "command_error" ,
205- Error : true ,
206- Message : err .Error (),
207- })
208- }
209-
210218var stdoutMutext sync.Mutex
211219
212220func syncronizedPrintLn (a ... interface {}) {
0 commit comments