hub/commands/help.go

180 строки
3.7 KiB
Go
Исходник Обычный вид История

2013-05-29 22:58:46 +04:00
package commands
2013-04-09 08:53:13 +04:00
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
2016-01-22 14:40:14 +03:00
"github.com/github/hub/cmd"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
2013-04-09 08:53:13 +04:00
)
var cmdHelp = &Command{
Run: runHelp,
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() {
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() {
args.AfterFn(func() error {
ui.Println(helpText)
return nil
})
return
2013-04-09 08:53:13 +04:00
}
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()
2016-01-22 14:40:14 +03:00
if command == "hub" {
err := displayManPage("hub.1", args)
if err != nil {
utils.Check(err)
2016-01-22 14:40:14 +03:00
}
}
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 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
2013-04-09 08:53:13 +04:00
}
}
}
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
2013-04-09 08:53:13 +04: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
ci-status Show the CI status of a commit
2013-07-06 00:58:51 +04:00
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 issues
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
`