Skip to content

Implement Pluggable Discovery protocol v1 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Factored json output and added Error field
  • Loading branch information
cmaglie committed Jun 3, 2021
commit adaf60730c354012434cdbb0be40630736db772d
36 changes: 25 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,39 @@ func main() {
cmd = strings.ToUpper(strings.TrimSpace(cmd))
switch cmd {
case "START":
outputMessage("start", "OK")
output(&genericMessageJSON{
EventType: "start",
Message: "OK",
})
case "STOP":
if syncStarted {
syncCloseChan <- true
syncStarted = false
}
outputMessage("stop", "OK")
output(&genericMessageJSON{
EventType: "stop",
Message: "OK",
})
case "LIST":
outputList()
case "QUIT":
outputMessage("quit", "OK")
output(&genericMessageJSON{
EventType: "quit",
Message: "OK",
})
os.Exit(0)
case "START_SYNC":
if syncStarted {
outputMessage("startSync", "OK")
} else if close, err := startSync(); err != nil {
outputError(err)
} else {
syncCloseChan = close
syncStarted = true
}
output(&genericMessageJSON{
EventType: "start_sync",
Message: "OK",
})
default:
outputError(fmt.Errorf("Command %s not supported", cmd))
}
Expand Down Expand Up @@ -135,16 +147,14 @@ func newBoardPortJSON(port *enumerator.PortDetails) *boardPortJSON {
return portJSON
}

type messageOutputJSON struct {
type genericMessageJSON struct {
EventType string `json:"eventType"`
Error bool `json:"error,omitempty"`
Message string `json:"message"`
}

func outputMessage(eventType, message string) {
d, err := json.MarshalIndent(&messageOutputJSON{
EventType: eventType,
Message: message,
}, "", " ")
func output(msg interface{}) {
d, err := json.MarshalIndent(msg, "", " ")
if err != nil {
outputError(err)
} else {
Expand All @@ -153,7 +163,11 @@ func outputMessage(eventType, message string) {
}

func outputError(err error) {
outputMessage("error", err.Error())
output(&genericMessageJSON{
EventType: "command_error",
Error: true,
Message: err.Error(),
})
}

var stdoutMutext sync.Mutex
Expand Down