get run command fully hooked up and test full message path
This commit is contained in:
parent
dc7b1e88c0
commit
e8db38d869
|
@ -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)
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue