feat(command): add command parsing
This commit is contained in:
41
commandParse.go
Normal file
41
commandParse.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getTextMiddle(text, left, right string) string {
|
||||||
|
start := 0
|
||||||
|
if left != "" {
|
||||||
|
idx := strings.Index(text, left)
|
||||||
|
if idx == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
start = idx + len(left)
|
||||||
|
}
|
||||||
|
|
||||||
|
if right == "" {
|
||||||
|
if start > len(text) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return text[start:]
|
||||||
|
}
|
||||||
|
|
||||||
|
endIdx := strings.Index(text[start:], right)
|
||||||
|
if endIdx == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return text[start : start+endIdx]
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCommand(content string) string {
|
||||||
|
return getTextMiddle(content, "[", "]")
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseCommand(content, cmdType string) string {
|
||||||
|
return getTextMiddle(content, "<" + cmdType + ">", "</" + cmdType + ">")
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeCommand(input string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
7
main.go
7
main.go
@@ -70,7 +70,12 @@ func handleRequest(conn net.Conn) {
|
|||||||
if recvMsg == "watchdogAgentConnectionTest" {
|
if recvMsg == "watchdogAgentConnectionTest" {
|
||||||
responseMsg = "success"
|
responseMsg = "success"
|
||||||
} else {
|
} else {
|
||||||
responseMsg = "error: unknown message"
|
err := executeCommand(recvMsg)
|
||||||
|
if err != nil {
|
||||||
|
responseMsg = fmt.Sprintf("error: %v", err)
|
||||||
|
} else {
|
||||||
|
responseMsg = "success"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if _, err := conn.Write([]byte(responseMsg + "\n")); err != nil {
|
if _, err := conn.Write([]byte(responseMsg + "\n")); err != nil {
|
||||||
postLog.Error(fmt.Sprintf("Failed to write: %v, err: %v", conn, err))
|
postLog.Error(fmt.Sprintf("Failed to write: %v, err: %v", conn, err))
|
||||||
|
|||||||
Reference in New Issue
Block a user