push cmd stuff to lib
This commit is contained in:
parent
13cad25620
commit
b2317046d1
|
@ -17,7 +17,7 @@ warren build - Runs go build and populates the following variables:
|
||||||
BuildDate from system time
|
BuildDate from system time
|
||||||
`
|
`
|
||||||
|
|
||||||
var buildCmd = &Command{
|
var buildCmd = &warren.Command{
|
||||||
Name: "build",
|
Name: "build",
|
||||||
Usage: "",
|
Usage: "",
|
||||||
Summary: "build the go server in the current directory",
|
Summary: "build the go server in the current directory",
|
||||||
|
@ -25,7 +25,7 @@ var buildCmd = &Command{
|
||||||
Run: buildRun,
|
Run: buildRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildRun(cmd *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
|
||||||
|
|
|
@ -11,7 +11,7 @@ var deployUsage = `
|
||||||
warren deploy - Deploys the go server and support files
|
warren deploy - Deploys the go server and support files
|
||||||
`
|
`
|
||||||
|
|
||||||
var deployCmd = &Command{
|
var deployCmd = &warren.Command{
|
||||||
Name: "deploy",
|
Name: "deploy",
|
||||||
Usage: "",
|
Usage: "",
|
||||||
Summary: "deploy the go server and support files",
|
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()
|
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" {
|
if len(args) > 0 && args[0] == "help" {
|
||||||
fmt.Print(cmd.Help)
|
fmt.Print(cmd.Help)
|
||||||
return
|
return
|
||||||
|
|
|
@ -15,7 +15,7 @@ var runUsage = `
|
||||||
warren run - Interfaces with warrend to manage running instances
|
warren run - Interfaces with warrend to manage running instances
|
||||||
`
|
`
|
||||||
|
|
||||||
var runCmd = &Command{
|
var runCmd = &warren.Command{
|
||||||
Name: "run",
|
Name: "run",
|
||||||
Usage: "",
|
Usage: "",
|
||||||
Summary: "Interface to warrend to manage running instances",
|
Summary: "Interface to warrend to manage running instances",
|
||||||
|
@ -23,7 +23,7 @@ var runCmd = &Command{
|
||||||
Run: runRun,
|
Run: runRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRun(cmd *Command, args ...string) {
|
func runRun(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
|
||||||
|
|
|
@ -3,8 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/dballard/warren/lib/warren"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,19 +24,16 @@ Commands:{{range .}}
|
||||||
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
|
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
|
||||||
`))
|
`))
|
||||||
|
|
||||||
var commands = []*Command{
|
|
||||||
buildCmd,
|
|
||||||
deployCmd,
|
|
||||||
runCmd,
|
|
||||||
}
|
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Println(BuildGitBranch, " ", BuildGitHash, " ", BuildDate)
|
fmt.Println(BuildGitBranch, " ", BuildGitHash, " ", BuildDate)
|
||||||
fmt.Print(usagePrefix)
|
fmt.Print(usagePrefix)
|
||||||
usageTmpl.Execute(os.Stdout, commands)
|
warren.PrintCommandUsage()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
warren.RegisterCommand(buildCmd)
|
||||||
|
warren.RegisterCommand(deployCmd)
|
||||||
|
warren.RegisterCommand(runCmd)
|
||||||
|
|
||||||
flag.Usage = usage
|
flag.Usage = usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -47,14 +44,8 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmd *Command
|
|
||||||
name := args[0]
|
name := args[0]
|
||||||
for _, c := range commands {
|
var cmd *warren.Command = warren.GetCommand(name)
|
||||||
if strings.HasPrefix(c.Name, name) {
|
|
||||||
cmd = c
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd == nil {
|
if cmd == nil {
|
||||||
fmt.Printf("error: unknown command %q\n", name)
|
fmt.Printf("error: unknown command %q\n", name)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package warren
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"os"
|
||||||
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
// shamelessly snagged from the go tool
|
// shamelessly snagged from the go tool
|
||||||
|
@ -18,6 +20,8 @@ type Command struct {
|
||||||
Help string
|
Help string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var commands map[string]*Command = make(map[string]*Command)
|
||||||
|
|
||||||
func (c *Command) Exec(args []string) {
|
func (c *Command) Exec(args []string) {
|
||||||
c.Flag.Usage = func() {
|
c.Flag.Usage = func() {
|
||||||
// helpFunc(c, c.Name)
|
// helpFunc(c, c.Name)
|
||||||
|
@ -25,3 +29,20 @@ func (c *Command) Exec(args []string) {
|
||||||
c.Flag.Parse(args)
|
c.Flag.Parse(args)
|
||||||
c.Run(c, c.Flag.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)
|
||||||
|
}
|
Loading…
Reference in New Issue