2013-05-29 22:58:46 +04:00
|
|
|
package commands
|
2013-04-09 08:53:13 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-01-25 14:17:21 +03:00
|
|
|
"path/filepath"
|
2014-10-19 05:19:18 +04:00
|
|
|
"sort"
|
2014-01-10 19:43:15 +04:00
|
|
|
"strings"
|
2016-01-22 14:28:04 +03:00
|
|
|
|
2016-01-22 14:40:14 +03:00
|
|
|
"github.com/github/hub/cmd"
|
2016-01-24 11:47:17 +03:00
|
|
|
"github.com/github/hub/ui"
|
2016-01-22 14:28:04 +03:00
|
|
|
"github.com/github/hub/utils"
|
2013-04-09 08:53:13 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdHelp = &Command{
|
2016-01-24 11:56:18 +03:00
|
|
|
Run: runHelp,
|
2014-01-10 19:43:15 +04:00
|
|
|
GitExtension: true,
|
2016-01-25 14:32:00 +03:00
|
|
|
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)
|
|
|
|
`,
|
2013-04-09 08:53:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-07-08 21:33:58 +03:00
|
|
|
CmdRunner.Use(cmdHelp, "--help")
|
2013-04-09 08:53:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-22 14:40:14 +03:00
|
|
|
func runHelp(helpCmd *Command, args *Args) {
|
2013-07-02 23:12:20 +04:00
|
|
|
if args.IsParamsEmpty() {
|
2016-09-11 00:49:58 +03:00
|
|
|
args.AfterFn(func() error {
|
|
|
|
ui.Println(helpText)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
2013-04-09 08:53:13 +04:00
|
|
|
}
|
2013-06-29 01:21:48 +04:00
|
|
|
|
2016-01-24 12:35:16 +03:00
|
|
|
if args.HasFlags("-a", "--all") {
|
2016-09-11 00:49:58 +03:00
|
|
|
args.AfterFn(func() error {
|
|
|
|
ui.Printf("\nhub custom commands\n\n %s\n", strings.Join(customCommands(), " "))
|
|
|
|
return nil
|
|
|
|
})
|
2016-01-24 12:35:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-19 05:19:18 +04:00
|
|
|
command := args.FirstParam()
|
2016-01-22 14:40:14 +03:00
|
|
|
|
|
|
|
if command == "hub" {
|
2016-01-25 14:17:21 +03:00
|
|
|
err := displayManPage("hub.1", args)
|
|
|
|
if err != nil {
|
|
|
|
utils.Check(err)
|
2016-01-22 14:40:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 12:35:16 +03:00
|
|
|
if c := lookupCmd(command); c != nil {
|
2016-01-25 14:17:21 +03:00
|
|
|
if !args.HasFlags("--plain-text") {
|
|
|
|
manPage := fmt.Sprintf("hub-%s.1", c.Name())
|
|
|
|
err := displayManPage(manPage, args)
|
2016-01-24 16:20:26 +03:00
|
|
|
if err == nil {
|
2016-01-25 14:17:21 +03:00
|
|
|
return
|
2016-01-24 16:20:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:17:21 +03:00
|
|
|
ui.Println(c.HelpText())
|
2016-09-11 00:49:58 +03:00
|
|
|
args.NoForward()
|
2016-01-24 16:20:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:17:21 +03:00
|
|
|
func displayManPage(manPage string, args *Args) error {
|
|
|
|
manProgram, _ := utils.CommandPath("man")
|
|
|
|
if manProgram == "" {
|
|
|
|
manPage += ".txt"
|
|
|
|
manProgram = os.Getenv("PAGER")
|
|
|
|
if manProgram == "" {
|
|
|
|
manProgram = "less -R"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 16:20:26 +03:00
|
|
|
programPath, err := utils.CommandPath(args.ProgramPath)
|
|
|
|
if err != nil {
|
2016-01-25 14:17:21 +03:00
|
|
|
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
|
2016-01-24 16:20:26 +03:00
|
|
|
}
|
|
|
|
|
2016-01-25 14:17:21 +03:00
|
|
|
manPath = filepath.Join(installPrefix, "share", "man", "man1", name)
|
2016-01-24 16:20:26 +03:00
|
|
|
_, err = os.Stat(manPath)
|
|
|
|
if err == nil {
|
|
|
|
return manPath, nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
2016-01-24 12:35:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-04-09 08:53:13 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-10 19:43:15 +04:00
|
|
|
}
|
|
|
|
|
2014-10-19 05:19:18 +04:00
|
|
|
func customCommands() []string {
|
|
|
|
cmds := []string{}
|
|
|
|
for n, c := range CmdRunner.All() {
|
2016-01-22 14:12:55 +03:00
|
|
|
if !c.GitExtension && !strings.HasPrefix(n, "--") {
|
2014-10-19 05:19:18 +04:00
|
|
|
cmds = append(cmds, n)
|
|
|
|
}
|
2014-01-10 19:43:15 +04:00
|
|
|
}
|
|
|
|
|
2014-10-19 05:19:18 +04:00
|
|
|
sort.Sort(sort.StringSlice(cmds))
|
2014-01-10 19:43:15 +04:00
|
|
|
|
2014-10-19 05:19:18 +04:00
|
|
|
return cmds
|
2013-04-09 08:53:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-22 14:28:04 +03:00
|
|
|
var helpText = `
|
|
|
|
These GitHub commands are provided by hub:
|
2013-06-18 01:09:47 +04:00
|
|
|
|
2013-07-06 00:58:51 +04:00
|
|
|
browse Open a GitHub page in the default browser
|
2017-06-23 19:42:25 +03:00
|
|
|
ci-status Show the CI status of a commit
|
2013-07-06 00:58:51 +04:00
|
|
|
compare Open a compare page on GitHub
|
2017-06-23 19:42:25 +03:00
|
|
|
create Create this repository on GitHub and add GitHub as origin
|
2018-03-18 22:47:34 +03:00
|
|
|
delete Delete a repository on GitHub
|
2017-06-23 19:42:25 +03:00
|
|
|
fork Make a fork of a remote repository on GitHub and add as remote
|
2016-03-14 01:49:54 +03:00
|
|
|
issue List or create issues
|
2017-06-23 19:42:25 +03:00
|
|
|
pr Work with pull requests
|
|
|
|
pull-request Open a pull request on GitHub
|
|
|
|
release List or create releases
|
2018-02-15 16:39:57 +03:00
|
|
|
sync Fetch git objects from upstream and update branches
|
2013-07-06 00:58:51 +04:00
|
|
|
`
|