From b2317046d1cddb95376bcf3268285bd477dc9e7b Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 31 Dec 2014 08:20:53 -0800 Subject: [PATCH] push cmd stuff to lib --- cmd/warren/cmd_build.go | 4 ++-- cmd/warren/cmd_deploy.go | 4 ++-- cmd/warren/cmd_run.go | 4 ++-- cmd/warren/main.go | 21 ++++++--------------- {cmd => lib}/warren/cmd.go | 23 ++++++++++++++++++++++- 5 files changed, 34 insertions(+), 22 deletions(-) rename {cmd => lib}/warren/cmd.go (50%) diff --git a/cmd/warren/cmd_build.go b/cmd/warren/cmd_build.go index fce1357..f071b1e 100644 --- a/cmd/warren/cmd_build.go +++ b/cmd/warren/cmd_build.go @@ -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 diff --git a/cmd/warren/cmd_deploy.go b/cmd/warren/cmd_deploy.go index b8bfdc1..ab5b665 100644 --- a/cmd/warren/cmd_deploy.go +++ b/cmd/warren/cmd_deploy.go @@ -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 diff --git a/cmd/warren/cmd_run.go b/cmd/warren/cmd_run.go index e14423e..74ae0e6 100644 --- a/cmd/warren/cmd_run.go +++ b/cmd/warren/cmd_run.go @@ -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 diff --git a/cmd/warren/main.go b/cmd/warren/main.go index 5ce7032..17ffdb9 100644 --- a/cmd/warren/main.go +++ b/cmd/warren/main.go @@ -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) diff --git a/cmd/warren/cmd.go b/lib/warren/cmd.go similarity index 50% rename from cmd/warren/cmd.go rename to lib/warren/cmd.go index 8a520d5..8dfa11d 100644 --- a/cmd/warren/cmd.go +++ b/lib/warren/cmd.go @@ -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) +}