feat: file as json support

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

View File

@ -185,8 +185,16 @@ func StartSession(options *client.ClientOptions) {
var rsp *hsp.Response var rsp *hsp.Response
var rerr error var rerr error
if strings.HasPrefix(line, "/file ") { 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) file, err := os.Open(filename)
if err != nil { if err != nil {
@ -194,21 +202,30 @@ func StartSession(options *client.ClientOptions) {
continue continue
} }
buf, err := io.ReadAll(file) if !isJson {
if err != nil { buf, err := io.ReadAll(file)
fmt.Printf("ERR: Failed to read from file '%s': %v\n", filename, err) if err != nil {
continue 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 ") { } else if strings.HasPrefix(line, "/json ") {
var data any var data any
err = json.Unmarshal([]byte(strings.TrimLeft(line, "/json ")), &data) err = json.Unmarshal([]byte(strings.TrimLeft(line, "/json ")), &data)
if err != nil { if err != nil {
fmt.Println("ERR: Invalid JSON for request:", err) fmt.Println("ERR: Invalid JSON for request:", err)
} else {
rsp, err = c.SendJson(route, data)
} }
rsp, rerr = c.SendJson(route, data)
} else { } else {
rsp, rerr = c.SendText(route, line) rsp, rerr = c.SendText(route, line)
} }