diff --git a/cmd/warren/cmd_build.go b/cmd/warren/cmd_build.go index f071b1e..faa36cb 100644 --- a/cmd/warren/cmd_build.go +++ b/cmd/warren/cmd_build.go @@ -26,6 +26,7 @@ var buildCmd = &warren.Command{ } func buildRun(cmd *warren.Command, args ...string) { + if len(args) > 0 && args[0] == "help" { fmt.Print(cmd.Help) return diff --git a/cmd/warren/main.go b/cmd/warren/main.go index 17ffdb9..a9166a9 100644 --- a/cmd/warren/main.go +++ b/cmd/warren/main.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "github.com/dballard/warren/lib/warren" "os" @@ -34,25 +33,21 @@ func main() { warren.RegisterCommand(buildCmd) warren.RegisterCommand(deployCmd) warren.RegisterCommand(runCmd) - - flag.Usage = usage - flag.Parse() - - args := flag.Args() + args := os.Args[1:] if len(args) == 0 || args[0] == "-h" { - flag.Usage() + usage() return } name := args[0] - var cmd *warren.Command = warren.GetCommand(name) + var icmd = warren.GetCommand(name) - if cmd == nil { + if icmd == nil { fmt.Printf("error: unknown command %q\n", name) - flag.Usage() + usage() os.Exit(1) } - cmd.Exec(args[1:]) + icmd.(*warren.Command).Exec(args[1:]) } diff --git a/cmd/warrend/cmd_status.go b/cmd/warrend/cmd_status.go new file mode 100644 index 0000000..9c63dba --- /dev/null +++ b/cmd/warrend/cmd_status.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "github.com/dballard/warren/lib/warren" + "net" +) + +var statusUsage = ` +status - +` + +var statusCmd = &SockCommand{ + Command: warren.Command{ + Name: "status", + Usage: "", + Summary: "Print status of warrend", + Help: statusUsage, + }, + Run: statusRun, +} + +func statusRun(cmd *SockCommand, c net.Conn, args ...string) { + if len(args) == 0 || args[0] == "help" { + fmt.Print(cmd.Help) + return + } + + c.Write([]byte("status\000")) +} diff --git a/cmd/warrend/main.go b/cmd/warrend/main.go index 150a058..ba112ac 100644 --- a/cmd/warrend/main.go +++ b/cmd/warrend/main.go @@ -10,7 +10,24 @@ import ( "syscall" ) -func cmdHandle(c net.Conn) { +type SockCommand struct { + warren.Command + Run func(cmd *SockCommand, c net.Conn, args ...string) +} + +func (cmd *SockCommand) GetName() string { + return cmd.Name +} + +func (cmd *SockCommand) Exec(c net.Conn, args []string) { + cmd.Flag.Usage = func() { + // helpFunc(c, c.Name) + } + cmd.Flag.Parse(args) + cmd.Run(cmd, c, cmd.Flag.Args()...) +} + +func sockCmdHandle(c net.Conn) { defer c.Close() fmt.Println("cmdHandle") data, err := warren.ReadStringz(c) @@ -19,10 +36,15 @@ func cmdHandle(c net.Conn) { return } fmt.Println("cmdHandle got command:", string(data)) + + // parse args + // get and run cmd + c.Write([]byte("echo: " + data)) } func main() { + warren.RegisterCommand(statusCmd) cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr()) defer cmdSock.Close() @@ -49,7 +71,7 @@ func main() { if err != nil { log.Fatal("accpet error:", err) } - go cmdHandle(fd) + go sockCmdHandle(fd) } } diff --git a/lib/warren/cmd.go b/lib/warren/cmd.go index 8dfa11d..75f5420 100644 --- a/lib/warren/cmd.go +++ b/lib/warren/cmd.go @@ -6,6 +6,12 @@ import ( "text/template" ) +type ICommand interface { + GetName() string +} + +var commands map[string]ICommand = make(map[string]ICommand) + // shamelessly snagged from the go tool // each command gets its own set of args, // defines its own entry point, and provides its own help @@ -20,7 +26,9 @@ type Command struct { Help string } -var commands map[string]*Command = make(map[string]*Command) +func (c *Command) GetName() string { + return c.Name +} func (c *Command) Exec(args []string) { c.Flag.Usage = func() { @@ -30,11 +38,11 @@ func (c *Command) Exec(args []string) { c.Run(c, c.Flag.Args()...) } -func RegisterCommand(c *Command) { - commands[c.Name] = c +func RegisterCommand(c ICommand) { + commands[c.GetName()] = c } -func GetCommand(name string) *Command { +func GetCommand(name string) ICommand { return commands[name] }