more go lang idiomatic oo around commands

This commit is contained in:
Dan Ballard 2015-01-02 09:07:30 -08:00
parent 5857870a0b
commit 92e16f322e
5 changed files with 73 additions and 17 deletions

View File

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

View File

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

30
cmd/warrend/cmd_status.go Normal file
View File

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

View File

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

View File

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