diff --git a/cmd/warren/cmd_run.go b/cmd/warren/cmd_run.go index 2b6695d..fb8a91a 100644 --- a/cmd/warren/cmd_run.go +++ b/cmd/warren/cmd_run.go @@ -34,6 +34,13 @@ func runRun(cmd *Command, args ...string) { log.Fatal("Error connecting to warrend:", err) } - s.Write([]byte(strings.Join(args, " "))) + s.Write([]byte(strings.Join(args, " ") + "\000")) + resp, err := warren.ReadStringz(s) + + if err != nil { + log.Fatal(err) + } + + fmt.Println(resp) } diff --git a/cmd/warrend/main.go b/cmd/warrend/main.go index 3748443..150a058 100644 --- a/cmd/warrend/main.go +++ b/cmd/warrend/main.go @@ -11,19 +11,15 @@ import ( ) func cmdHandle(c net.Conn) { - fmt.Println("cmdHandle") - buf := make([]byte, 1024) // Max size of buff based on? - nr, err := c.Read(buf) defer c.Close() + fmt.Println("cmdHandle") + data, err := warren.ReadStringz(c) if err != nil { - fmt.Println("cmdHandle read error:", err) + fmt.Println(err) return } - - data := buf[0:nr] - fmt.Println("cmdHandle got command:", string(data)) - + c.Write([]byte("echo: " + data)) } func main() { diff --git a/lib/warren/warren.go b/lib/warren/warren.go index e42a4f6..ad8cff8 100644 --- a/lib/warren/warren.go +++ b/lib/warren/warren.go @@ -1,7 +1,9 @@ package warren import ( + "fmt" "log" + "net" "os" "os/exec" "path/filepath" @@ -40,3 +42,18 @@ func AppName() string { paths, name := filepath.Split(paths) return name } + +func ReadStringz(c net.Conn) (string, error) { + data := "" + buf := make([]byte, 1024) // Max size of buff based on? + + for len(data) > 0 && data[len(data)-1] == 0 { + nr, err := c.Read(buf) + if err != nil { + fmt.Println("readStringz read error:", err) + return "", fmt.Errorf("readStringz error", err) + } + data += string(buf[0:nr]) + } + return data, nil +}