feat: file as json support

This commit is contained in:
2025-05-16 15:57:14 +02:00
parent a1cc664f41
commit 33b412dd85

View File

@ -186,7 +186,15 @@ func StartSession(options *client.ClientOptions) {
var rerr error
if strings.HasPrefix(line, "/file") {
filename := strings.TrimLeft(line, "/file ")
what := strings.TrimLeft(line, "/file")
isJson := false
var filename string
if strings.HasPrefix(what, ":json ") {
isJson = true
filename = strings.TrimLeft(what, ":json ")
} else {
filename = strings.TrimLeft(what, " ")
}
file, err := os.Open(filename)
if err != nil {
@ -194,21 +202,30 @@ func StartSession(options *client.ClientOptions) {
continue
}
if !isJson {
buf, err := io.ReadAll(file)
if err != nil {
fmt.Printf("ERR: Failed to read from file '%s': %v\n", filename, err)
continue
}
rsp, err = c.SendBytes(route, buf)
} else {
var data any
rsp, rerr = c.SendBytes(route, buf)
decoder := json.NewDecoder(file)
err = decoder.Decode(&data)
if err == nil {
rsp, err = c.SendJson(route, data)
}
}
} else if strings.HasPrefix(line, "/json ") {
var data any
err = json.Unmarshal([]byte(strings.TrimLeft(line, "/json ")), &data)
if err != nil {
fmt.Println("ERR: Invalid JSON for request:", err)
} else {
rsp, err = c.SendJson(route, data)
}
rsp, rerr = c.SendJson(route, data)
} else {
rsp, rerr = c.SendText(route, line)
}