-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
204 lines (171 loc) · 4.2 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package commands
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/github/hub/cmd"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
)
var cmdHelp = &Command{
Run: runHelp,
GitExtension: true,
Usage: `
help hub
help <COMMAND>
help hub-<COMMAND> [--plain-text]
`,
Long: `Show the help page for a command.
## Options:
hub-<COMMAND>
Use this format to view help for hub extensions to an existing git command.
--plain-text
Skip man page lookup mechanism and display plain help text.
## Man lookup mechanism:
On systems that have 'man', help pages are looked up in these directories
relative to 'hub' install prefix:
* 'man/<command>.1'
* 'share/man/man1/<command>.1'
On systems without 'man', same help pages are looked up with a '.txt' suffix.
## See also:
hub(1), git-help(1)
`,
}
var cmdListCmds = &Command{
Key: "--list-cmds",
Run: runListCmds,
GitExtension: true,
}
func init() {
CmdRunner.Use(cmdHelp, "--help")
CmdRunner.Use(cmdListCmds)
}
func runHelp(helpCmd *Command, args *Args) {
if args.IsParamsEmpty() {
args.AfterFn(func() error {
ui.Println(helpText)
return nil
})
return
}
if args.HasFlags("-a", "--all") {
args.AfterFn(func() error {
ui.Printf("\nhub custom commands\n\n %s\n", strings.Join(customCommands(), " "))
return nil
})
return
}
command := args.FirstParam()
if command == "hub" {
err := displayManPage("hub.1", args)
if err != nil {
utils.Check(err)
}
}
if c := lookupCmd(command); c != nil {
if !args.HasFlags("--plain-text") {
manPage := fmt.Sprintf("hub-%s.1", c.Name())
err := displayManPage(manPage, args)
if err == nil {
return
}
}
ui.Println(c.HelpText())
args.NoForward()
}
}
func runListCmds(cmd *Command, args *Args) {
listOthers := false
parts := strings.SplitN(args.Command, "=", 2)
for _, kind := range strings.Split(parts[1], ",") {
if kind == "others" {
listOthers = true
break
}
}
if listOthers {
args.AfterFn(func() error {
ui.Println(strings.Join(customCommands(), "\n"))
return nil
})
}
}
func displayManPage(manPage string, args *Args) error {
manProgram, _ := utils.CommandPath("man")
if manProgram == "" {
manPage += ".txt"
manProgram = os.Getenv("PAGER")
if manProgram == "" {
manProgram = "less -R"
}
}
programPath, err := utils.CommandPath(args.ProgramPath)
if err != nil {
return err
}
installPrefix := filepath.Join(filepath.Dir(programPath), "..")
manFile, err := localManPage(manPage, installPrefix)
if err != nil {
return err
}
man := cmd.New(manProgram)
man.WithArg(manFile)
if err = man.Run(); err == nil {
os.Exit(0)
} else {
os.Exit(1)
}
return nil
}
func localManPage(name, installPrefix string) (string, error) {
manPath := filepath.Join(installPrefix, "man", name)
_, err := os.Stat(manPath)
if err == nil {
return manPath, nil
}
manPath = filepath.Join(installPrefix, "share", "man", "man1", name)
_, err = os.Stat(manPath)
if err == nil {
return manPath, nil
} else {
return "", err
}
}
func lookupCmd(name string) *Command {
if strings.HasPrefix(name, "hub-") {
return CmdRunner.Lookup(strings.TrimPrefix(name, "hub-"))
} else {
cmd := CmdRunner.Lookup(name)
if cmd != nil && !cmd.GitExtension {
return cmd
} else {
return nil
}
}
}
func customCommands() []string {
cmds := []string{}
for n, c := range CmdRunner.All() {
if !c.GitExtension && !strings.HasPrefix(n, "--") {
cmds = append(cmds, n)
}
}
sort.Sort(sort.StringSlice(cmds))
return cmds
}
var helpText = `
These GitHub commands are provided by hub:
browse Open a GitHub page in the default browser
ci-status Show the status of GitHub checks for a commit
compare Open a compare page on GitHub
create Create this repository on GitHub and add GitHub as origin
delete Delete a repository on GitHub
fork Make a fork of a remote repository on GitHub and add as remote
issue List or create GitHub issues
pr List or checkout GitHub pull requests
pull-request Open a pull request on GitHub
release List or create GitHub releases
sync Fetch git objects from upstream and update branches
`