hub/main.go

82 строки
1.1 KiB
Go
Исходник Обычный вид История

2013-04-09 08:53:13 +04:00
package main
import (
"flag"
"fmt"
"log"
2013-04-09 08:53:13 +04:00
"os"
"strings"
)
type Command struct {
Run func(cmd *Command, args []string)
Flag flag.FlagSet
Usage string
Short string
Long string
}
func (c *Command) printUsage() {
if c.Runnable() {
fmt.Printf("Usage: gh %s\n\n", c.Usage)
}
fmt.Println(strings.Trim(c.Long, "\n"))
}
func (c *Command) Name() string {
name := c.Usage
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
func (c *Command) Runnable() bool {
return c.Run != nil
}
2013-04-11 19:33:43 +04:00
func (c *Command) List() bool {
return c.Short != ""
}
2013-04-30 06:45:57 +04:00
var (
commands = []*Command{
cmdPullRequest,
cmdHelp,
2013-05-24 10:16:00 +04:00
cmdVersion,
2013-04-30 06:45:57 +04:00
}
2013-05-28 21:52:27 +04:00
git = Git{"git"}
2013-04-30 06:45:57 +04:00
)
2013-04-22 07:36:21 +04:00
2013-04-09 08:53:13 +04:00
func main() {
args := os.Args[1:]
if len(args) < 1 {
usage()
}
for _, cmd := range commands {
if cmd.Name() == args[0] && cmd.Run != nil {
cmd.Flag.Usage = func() {
cmd.printUsage()
}
if err := cmd.Flag.Parse(args[1:]); err != nil {
os.Exit(2)
}
cmd.Run(cmd, cmd.Flag.Args())
return
}
}
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", args[0])
usage()
}
func check(err error) {
if err != nil {
2013-05-29 00:14:40 +04:00
log.Fatalf("fatal: %v", err)
}
}