forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
159 lines (124 loc) · 3.41 KB
/
help.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"sort"
"strings"
"text/template"
"unicode"
"unicode/utf8"
"github.com/constabulary/gb/cmd"
)
func init() {
registerCommand(helpProject)
}
var helpProject = &cmd.Command{
Name: "project",
Short: "gb project layout",
Long: `A gb project is defined as any directory that contains a src/ subdirectory.
gb automatically detects the root of the project by looking at the current working directory and walking backwards until it finds a directory that contains a src/ subdirectory.
In the event you wish to override this auto detection mechanism, the -R flag can be used to supply a project root.
See http://getgb.io/docs/project for details`,
}
var helpTemplate = `{{if .Runnable}}usage: gb {{.UsageLine}}
{{end}}{{.Long | trim}}
`
// help implements the 'help' command.
func help(args []string) {
if len(args) == 0 {
printUsage(os.Stdout)
// not exit 2: succeeded at 'gb help'.
return
}
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "usage: gb help command\n\nToo many arguments given.\n")
os.Exit(2) // failed at 'gb help'
}
arg := args[0]
// 'gb help documentation' generates alldocs.go.
if arg == "documentation" {
var buf bytes.Buffer
printUsage(&buf)
usage := &cmd.Command{Long: buf.String()}
f, _ := os.Create("alldocs.go")
tmpl(f, documentationTemplate, append([]*cmd.Command{usage}, sortedCommands()...))
f.Close()
return
}
for _, cmd := range commands {
if cmd.Name == arg {
tmpl(os.Stdout, helpTemplate, cmd)
// not exit 2: succeeded at 'gb help cmd'.
return
}
}
fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'gb help'.\n", arg)
os.Exit(2) // failed at 'gb help cmd'
}
var usageTemplate = `gb, a project based build tool for the Go programming language.
Usage:
gb command [arguments]
The commands are:
{{range .}}{{if .Runnable}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
Use "gb help [command]" for more information about a command.
Additional help topics:
{{range .}}{{if not .Runnable}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
Use "gb help [topic]" for more information about that topic.
`
var documentationTemplate = `// DO NOT EDIT THIS FILE.
//go:generate gb help documentation
/*
{{range .}}{{if .Short}}{{.Short | capitalize}}
{{end}}{{if .Runnable}}Usage:
gb {{.UsageLine}}
{{end}}{{.Long | trim}}
{{end}}*/
package main
`
// tmpl executes the given template text on data, writing the result to w.
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
panic(err)
}
}
func capitalize(s string) string {
if s == "" {
return s
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToTitle(r)) + s[n:]
}
func printUsage(w io.Writer) {
bw := bufio.NewWriter(w)
tmpl(bw, usageTemplate, sortedCommands())
bw.Flush()
}
func sortedCommands() []*cmd.Command {
// TODO(dfc) drop this and make main.commands a []*cmd.Command
var sortedKeys []string
for k := range commands {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
var cmds []*cmd.Command
for _, c := range sortedKeys {
// skip hidden commands
if commands[c].Hidden() {
continue
}
cmds = append(cmds, commands[c])
}
return cmds
}
func usage() {
printUsage(os.Stderr)
os.Exit(2)
}