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())
|
s, err := net.Dial(warren.CmdSockType(), warren.CmdSockAddr())
|
||||||
|
if s != nil {
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error connecting to warrend:", err)
|
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)
|
resp, err := warren.ReadStringz(s)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/dballard/warren/lib/warren"
|
"github.com/dballard/warren/lib/warren"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
@ -21,10 +20,10 @@ var statusCmd = &SockCommand{
|
||||||
}
|
}
|
||||||
|
|
||||||
func statusRun(cmd *SockCommand, c net.Conn, args ...string) {
|
func statusRun(cmd *SockCommand, c net.Conn, args ...string) {
|
||||||
if len(args) == 0 || args[0] == "help" {
|
if len(args) > 0 && args[0] == "help" {
|
||||||
fmt.Print(cmd.Help)
|
warren.WriteStringz(c, cmd.Help)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Write([]byte("status\000"))
|
warren.WriteStringz(c, "status")
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,31 +34,34 @@ func sockCmdHandle(c net.Conn) {
|
||||||
data, err := warren.ReadStringz(c)
|
data, err := warren.ReadStringz(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
warren.WriteStringz(c, "read error:"+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("cmdHandle got command:", string(data))
|
fmt.Println("cmdHandle got command:", string(data))
|
||||||
|
|
||||||
// BAD: TODO: handle quotes "
|
// BAD: TODO: handle quotes "
|
||||||
var args = strings.Split(" ", data)
|
var args = strings.Split(data, " ")
|
||||||
|
|
||||||
// parse args
|
// parse args
|
||||||
var icmd = warren.GetCommand(args[0])
|
var icmd = warren.GetCommand(args[0])
|
||||||
// get and run cmd
|
// get and run cmd
|
||||||
if icmd == nil {
|
if icmd == nil {
|
||||||
c.Write([]byte("Unknown command " + args[0]))
|
warren.WriteStringz(c, "Unknown command "+args[0])
|
||||||
// usage ?
|
// usage ?
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
icmd.(*SockCommand).Exec(c, args[1:])
|
icmd.(*SockCommand).Exec(c, args[1:])
|
||||||
|
warren.WriteStringz(c, "echo: "+data)
|
||||||
c.Write([]byte("echo: " + data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
warren.RegisterCommand(statusCmd)
|
warren.RegisterCommand(statusCmd)
|
||||||
|
|
||||||
cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr())
|
cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr())
|
||||||
|
if cmdSock != nil {
|
||||||
defer cmdSock.Close()
|
defer cmdSock.Close()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Listen on sock error:", err)
|
log.Fatal("Listen on sock error:", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,11 @@ func AppName() string {
|
||||||
return name
|
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) {
|
func ReadStringz(c net.Conn) (string, error) {
|
||||||
data := ""
|
data := ""
|
||||||
buf := make([]byte, 1024) // Max size of buff based on?
|
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)
|
nr, err := c.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("readStringz read error:", err)
|
fmt.Println("readStringz read error:", err)
|
||||||
return "", fmt.Errorf("readStringz error", err)
|
return "", fmt.Errorf("readStringz error:", err)
|
||||||
}
|
}
|
||||||
data += string(buf[0:nr])
|
data += string(buf[0:nr])
|
||||||
}
|
}
|
||||||
return data, nil
|
//return minux \000
|
||||||
|
return data[0 : len(data)-1], nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue