more go lang idiomatic oo around commands
This commit is contained in:
parent
5857870a0b
commit
92e16f322e
|
@ -26,6 +26,7 @@ var buildCmd = &warren.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildRun(cmd *warren.Command, args ...string) {
|
func buildRun(cmd *warren.Command, args ...string) {
|
||||||
|
|
||||||
if len(args) > 0 && args[0] == "help" {
|
if len(args) > 0 && args[0] == "help" {
|
||||||
fmt.Print(cmd.Help)
|
fmt.Print(cmd.Help)
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dballard/warren/lib/warren"
|
"github.com/dballard/warren/lib/warren"
|
||||||
"os"
|
"os"
|
||||||
|
@ -34,25 +33,21 @@ func main() {
|
||||||
warren.RegisterCommand(buildCmd)
|
warren.RegisterCommand(buildCmd)
|
||||||
warren.RegisterCommand(deployCmd)
|
warren.RegisterCommand(deployCmd)
|
||||||
warren.RegisterCommand(runCmd)
|
warren.RegisterCommand(runCmd)
|
||||||
|
args := os.Args[1:]
|
||||||
flag.Usage = usage
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
args := flag.Args()
|
|
||||||
if len(args) == 0 || args[0] == "-h" {
|
if len(args) == 0 || args[0] == "-h" {
|
||||||
flag.Usage()
|
usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name := args[0]
|
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)
|
fmt.Printf("error: unknown command %q\n", name)
|
||||||
flag.Usage()
|
usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Exec(args[1:])
|
icmd.(*warren.Command).Exec(args[1:])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"))
|
||||||
|
}
|
|
@ -10,7 +10,24 @@ import (
|
||||||
"syscall"
|
"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()
|
defer c.Close()
|
||||||
fmt.Println("cmdHandle")
|
fmt.Println("cmdHandle")
|
||||||
data, err := warren.ReadStringz(c)
|
data, err := warren.ReadStringz(c)
|
||||||
|
@ -19,10 +36,15 @@ func cmdHandle(c net.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("cmdHandle got command:", string(data))
|
fmt.Println("cmdHandle got command:", string(data))
|
||||||
|
|
||||||
|
// parse args
|
||||||
|
// get and run cmd
|
||||||
|
|
||||||
c.Write([]byte("echo: " + data))
|
c.Write([]byte("echo: " + data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
warren.RegisterCommand(statusCmd)
|
||||||
|
|
||||||
cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr())
|
cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr())
|
||||||
defer cmdSock.Close()
|
defer cmdSock.Close()
|
||||||
|
@ -49,7 +71,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("accpet error:", err)
|
log.Fatal("accpet error:", err)
|
||||||
}
|
}
|
||||||
go cmdHandle(fd)
|
go sockCmdHandle(fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,12 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ICommand interface {
|
||||||
|
GetName() string
|
||||||
|
}
|
||||||
|
|
||||||
|
var commands map[string]ICommand = make(map[string]ICommand)
|
||||||
|
|
||||||
// shamelessly snagged from the go tool
|
// shamelessly snagged from the go tool
|
||||||
// each command gets its own set of args,
|
// each command gets its own set of args,
|
||||||
// defines its own entry point, and provides its own help
|
// defines its own entry point, and provides its own help
|
||||||
|
@ -20,7 +26,9 @@ type Command struct {
|
||||||
Help string
|
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) {
|
func (c *Command) Exec(args []string) {
|
||||||
c.Flag.Usage = func() {
|
c.Flag.Usage = func() {
|
||||||
|
@ -30,11 +38,11 @@ func (c *Command) Exec(args []string) {
|
||||||
c.Run(c, c.Flag.Args()...)
|
c.Run(c, c.Flag.Args()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterCommand(c *Command) {
|
func RegisterCommand(c ICommand) {
|
||||||
commands[c.Name] = c
|
commands[c.GetName()] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCommand(name string) *Command {
|
func GetCommand(name string) ICommand {
|
||||||
return commands[name]
|
return commands[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue