diff --git a/cmd/warren/cmd_run.go b/cmd/warren/cmd_run.go index 74ae0e6..f0b016d 100644 --- a/cmd/warren/cmd_run.go +++ b/cmd/warren/cmd_run.go @@ -30,12 +30,14 @@ func runRun(cmd *warren.Command, args ...string) { } s, err := net.Dial(warren.CmdSockType(), warren.CmdSockAddr()) - defer s.Close() + if s != nil { + defer s.Close() + } if err != nil { log.Fatal("Error connecting to warrend:", err) } - s.Write([]byte(strings.Join(args, " ") + "\000")) + warren.WriteStringz(s, strings.Join(args, " ")) resp, err := warren.ReadStringz(s) diff --git a/cmd/warrend/cmd_status.go b/cmd/warrend/cmd_status.go index 9c63dba..e2d1136 100644 --- a/cmd/warrend/cmd_status.go +++ b/cmd/warrend/cmd_status.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "github.com/dballard/warren/lib/warren" "net" ) @@ -21,10 +20,10 @@ var statusCmd = &SockCommand{ } func statusRun(cmd *SockCommand, c net.Conn, args ...string) { - if len(args) == 0 || args[0] == "help" { - fmt.Print(cmd.Help) + if len(args) > 0 && args[0] == "help" { + warren.WriteStringz(c, cmd.Help) return } - c.Write([]byte("status\000")) + warren.WriteStringz(c, "status") } diff --git a/cmd/warrend/main.go b/cmd/warrend/main.go index 4639a58..4018155 100644 --- a/cmd/warrend/main.go +++ b/cmd/warrend/main.go @@ -34,31 +34,34 @@ func sockCmdHandle(c net.Conn) { data, err := warren.ReadStringz(c) if err != nil { fmt.Println(err) + warren.WriteStringz(c, "read error:"+err.Error()) return } fmt.Println("cmdHandle got command:", string(data)) // BAD: TODO: handle quotes " - var args = strings.Split(" ", data) + var args = strings.Split(data, " ") // parse args var icmd = warren.GetCommand(args[0]) // get and run cmd if icmd == nil { - c.Write([]byte("Unknown command " + args[0])) + warren.WriteStringz(c, "Unknown command "+args[0]) // usage ? + return } icmd.(*SockCommand).Exec(c, args[1:]) - - c.Write([]byte("echo: " + data)) + warren.WriteStringz(c, "echo: "+data) } func main() { warren.RegisterCommand(statusCmd) cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr()) - defer cmdSock.Close() + if cmdSock != nil { + defer cmdSock.Close() + } if err != nil { log.Fatal("Listen on sock error:", err) } diff --git a/lib/warren/warren.go b/lib/warren/warren.go index ea12468..88a7b2b 100644 --- a/lib/warren/warren.go +++ b/lib/warren/warren.go @@ -43,6 +43,11 @@ func AppName() string { return name } +func WriteStringz(c net.Conn, str string) (err error) { + _, err = c.Write([]byte(str + "\000")) + return +} + func ReadStringz(c net.Conn) (string, error) { data := "" buf := make([]byte, 1024) // Max size of buff based on? @@ -51,9 +56,10 @@ func ReadStringz(c net.Conn) (string, error) { nr, err := c.Read(buf) if err != nil { fmt.Println("readStringz read error:", err) - return "", fmt.Errorf("readStringz error", err) + return "", fmt.Errorf("readStringz error:", err) } data += string(buf[0:nr]) } - return data, nil + //return minux \000 + return data[0 : len(data)-1], nil }