-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
73 lines (60 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"flag"
"fmt"
"github.com/orchardup/go-orchard/commands"
"github.com/orchardup/go-orchard/constants"
"io"
"os"
"strings"
"text/template"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "--version" {
fmt.Printf("Orchard %s\n", constants.Version)
return
}
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
usage()
}
for _, cmd := range commands.All {
if cmd.Name() == args[0] {
cmd.Flag.Usage = func() { cmd.Usage() }
cmd.Flag.Parse(args[1:])
args = cmd.Flag.Args()
err := cmd.Run(cmd, args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
}
fmt.Fprintf(os.Stderr, "Unknown subcommand: %q\n\n", args[0])
usage()
}
var usageTemplate = `Orchard command-line client.
Usage: orchard COMMAND [ARG...]
Commands:
{{range .}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}
Run 'orchard COMMAND -h' for more information on a command.
`
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": strings.TrimSpace})
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
panic(err)
}
}
func printUsage(w io.Writer) {
tmpl(w, usageTemplate, commands.All)
}
func usage() {
printUsage(os.Stderr)
os.Exit(2)
}