push cmd stuff to lib

This commit is contained in:
Dan Ballard 2014-12-31 08:20:53 -08:00
parent 13cad25620
commit b2317046d1
5 changed files with 34 additions and 22 deletions

View File

@ -17,7 +17,7 @@ warren build - Runs go build and populates the following variables:
BuildDate from system time
`
var buildCmd = &Command{
var buildCmd = &warren.Command{
Name: "build",
Usage: "",
Summary: "build the go server in the current directory",
@ -25,7 +25,7 @@ var buildCmd = &Command{
Run: buildRun,
}
func buildRun(cmd *Command, args ...string) {
func buildRun(cmd *warren.Command, args ...string) {
if len(args) > 0 && args[0] == "help" {
fmt.Print(cmd.Help)
return

View File

@ -11,7 +11,7 @@ var deployUsage = `
warren deploy - Deploys the go server and support files
`
var deployCmd = &Command{
var deployCmd = &warren.Command{
Name: "deploy",
Usage: "",
Summary: "deploy the go server and support files",
@ -43,7 +43,7 @@ func cp(dst, src string) error {
return exec.Command("cp", src, dst, "-r").Run()
}
func deployRun(cmd *Command, args ...string) {
func deployRun(cmd *warren.Command, args ...string) {
if len(args) > 0 && args[0] == "help" {
fmt.Print(cmd.Help)
return

View File

@ -15,7 +15,7 @@ var runUsage = `
warren run - Interfaces with warrend to manage running instances
`
var runCmd = &Command{
var runCmd = &warren.Command{
Name: "run",
Usage: "",
Summary: "Interface to warrend to manage running instances",
@ -23,7 +23,7 @@ var runCmd = &Command{
Run: runRun,
}
func runRun(cmd *Command, args ...string) {
func runRun(cmd *warren.Command, args ...string) {
if len(args) == 0 || args[0] == "help" {
fmt.Print(cmd.Help)
return

View File

@ -3,8 +3,8 @@ package main
import (
"flag"
"fmt"
"github.com/dballard/warren/lib/warren"
"os"
"strings"
"text/template"
)
@ -24,19 +24,16 @@ Commands:{{range .}}
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
`))
var commands = []*Command{
buildCmd,
deployCmd,
runCmd,
}
func usage() {
fmt.Println(BuildGitBranch, " ", BuildGitHash, " ", BuildDate)
fmt.Print(usagePrefix)
usageTmpl.Execute(os.Stdout, commands)
warren.PrintCommandUsage()
}
func main() {
warren.RegisterCommand(buildCmd)
warren.RegisterCommand(deployCmd)
warren.RegisterCommand(runCmd)
flag.Usage = usage
flag.Parse()
@ -47,14 +44,8 @@ func main() {
return
}
var cmd *Command
name := args[0]
for _, c := range commands {
if strings.HasPrefix(c.Name, name) {
cmd = c
break
}
}
var cmd *warren.Command = warren.GetCommand(name)
if cmd == nil {
fmt.Printf("error: unknown command %q\n", name)

View File

@ -1,7 +1,9 @@
package main
package warren
import (
"flag"
"os"
"text/template"
)
// shamelessly snagged from the go tool
@ -18,6 +20,8 @@ type Command struct {
Help string
}
var commands map[string]*Command = make(map[string]*Command)
func (c *Command) Exec(args []string) {
c.Flag.Usage = func() {
// helpFunc(c, c.Name)
@ -25,3 +29,20 @@ func (c *Command) Exec(args []string) {
c.Flag.Parse(args)
c.Run(c, c.Flag.Args()...)
}
func RegisterCommand(c *Command) {
commands[c.Name] = c
}
func GetCommand(name string) *Command {
return commands[name]
}
var usageTmpl = template.Must(template.New("usage").Parse(`
Commands:{{range .}}
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
`))
func PrintCommandUsage() {
usageTmpl.Execute(os.Stdout, commands)
}