2013-04-09 08:53:13 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-05-24 02:21:18 +04:00
|
|
|
"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-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()
|
|
|
|
}
|
2013-05-24 02:21:18 +04:00
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|