get run command fully hooked up and test full message path

This commit is contained in:
Dan Ballard 2015-01-27 22:20:39 -08:00
parent dc7b1e88c0
commit e8db38d869
4 changed files with 23 additions and 13 deletions

View File

@ -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)

View File

@ -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")
}

View File

@ -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)
}

View File

@ -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
}