hub/commands/help.go

222 строки
4.6 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"
"github.com/kballard/go-shellquote"
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.
2018-12-29 16:58:42 +03:00
## Lookup mechanism:
2016-01-25 14:32:00 +03:00
On systems that have 'man', help pages are looked up in these directories
2018-12-29 16:58:42 +03:00
relative to the hub install prefix:
2016-01-25 14:32:00 +03:00
2018-12-29 16:58:42 +03:00
* man/<command>.1
* share/man/man1/<command>.1
2016-01-25 14:32:00 +03:00
2018-12-29 16:58:42 +03:00
On systems without 'man', help pages are looked up using the ".txt" extension.
2016-01-25 14:32:00 +03:00
## See also:
hub(1), git-help(1)
`,
2013-04-09 08:53:13 +04:00
}
var cmdListCmds = &Command{
Key: "--list-cmds",
Run: runListCmds,
GitExtension: true,
}
2013-04-09 08:53:13 +04:00
func init() {
CmdRunner.Use(cmdHelp, "--help")
CmdRunner.Use(cmdListCmds)
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
}
2019-01-17 20:40:02 +03:00
p := utils.NewArgsParser()
p.RegisterBool("--all", "-a")
p.RegisterBool("--plain-text")
p.Parse(args.Params)
if p.Bool("--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 {
2019-01-17 20:40:02 +03:00
if !p.Bool("--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 {
var manArgs []string
manProgram, _ := utils.CommandPath("man")
if manProgram != "" {
manArgs = []string{manProgram}
} else {
manPage += ".txt"
manProgram = os.Getenv("PAGER")
if manProgram != "" {
var err error
manArgs, err = shellquote.Split(manProgram)
if err != nil {
return err
}
} else {
manArgs = []string{"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
}
manArgs = append(manArgs, manFile)
man := cmd.NewWithArray(manArgs)
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.Strings(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
api Low-level GitHub API request interface
2013-07-06 00:58:51 +04:00
browse Open a GitHub page in the default browser
ci-status Show the status of GitHub checks for 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
gist Make a gist
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
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
`