-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.go
178 lines (147 loc) · 3.56 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
package commands
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/github/hub/cmd"
"github.com/github/hub/git"
"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)
`,
}
func init() {
CmdRunner.Use(cmdHelp, "--help")
}
func runHelp(helpCmd *Command, args *Args) {
if args.IsParamsEmpty() {
printUsage()
os.Exit(0)
}
if args.HasFlags("-a", "--all") {
args.After("echo", "\nhub custom commands\n")
args.After("echo", " ", strings.Join(customCommands(), " "))
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())
os.Exit(0)
}
}
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:
pull-request Open a pull request on GitHub
fork Make a fork of a remote repository on GitHub and add as remote
create Create this repository on GitHub and add GitHub as origin
browse Open a GitHub page in the default browser
compare Open a compare page on GitHub
release List or create releases
issue List or create issues
ci-status Show the CI status of a commit
`
func printUsage() {
err := git.ForwardGitHelp()
utils.Check(err)
fmt.Print(helpText)
}