Skip to content

Commit

Permalink
Change hub help to display man pages when available
Browse files Browse the repository at this point in the history
Instead of displaying plain text help, `hub help <command>` will now
search for man pages to display.

* If `man` is available, hub first tries to locate the local
  `../man/hub-<command>.1` file relative to the `hub` executable. If
  found, open it with `man`. If not, execute `man 1 hub-<command>`.

* Otherwise, resort to displaying plain text help.
  • Loading branch information
mislav committed Jan 24, 2016
1 parent d5aa903 commit 21089ec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/bin
.bundle
vendor/bundle/
man/hub-*
tmp/
*.test
target
Expand Down
1 change: 1 addition & 0 deletions commands/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Args struct {
Executable string
GlobalFlags []string
Command string
ProgramPath string
Params []string
beforeChain []*cmd.Cmd
afterChain []*cmd.Cmd
Expand Down
40 changes: 38 additions & 2 deletions commands/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"sort"
"strings"
"path/filepath"

"github.com/github/hub/cmd"
"github.com/github/hub/git"
Expand Down Expand Up @@ -49,8 +50,43 @@ func runHelp(helpCmd *Command, args *Args) {
}

if c := lookupCmd(command); c != nil {
ui.Println(c.HelpText())
os.Exit(0)
manProgram, err := utils.CommandPath("man")
if err == nil {
man := cmd.New(manProgram)
manPage := "hub-" + c.Name()
manFile, err := localManPage(manPage, args)
if err == nil {
man.WithArg(manFile)
} else {
man.WithArgs("1", manPage)
}

err = man.Run()
if err == nil {
os.Exit(0)
} else {
os.Exit(1)
}
} else {
ui.Println(c.HelpText())
os.Exit(0)
}

}
}

func localManPage(cmd string, args *Args) (string, error) {
programPath, err := utils.CommandPath(args.ProgramPath)
if err != nil {
return "", err
}

manPath := filepath.Join(filepath.Dir(programPath), "..", "man", cmd + ".1")
_, err = os.Stat(manPath)
if err == nil {
return manPath, nil
} else {
return "", err
}
}

Expand Down
1 change: 1 addition & 0 deletions commands/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (r *Runner) Lookup(name string) *Command {

func (r *Runner) Execute() ExecError {
args := NewArgs(os.Args[1:])
args.ProgramPath = os.Args[0]

if args.Command == "" {
printUsage()
Expand Down
18 changes: 18 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ func searchBrowserLauncher(goos string) (browser string) {
return browser
}

func CommandPath(cmd string) (string, error) {
if runtime.GOOS == "windows" {
cmd = cmd + ".exe"
}

path, err := exec.LookPath(cmd)
if err != nil {
return "", err
}

path, err = filepath.Abs(path)
if err != nil {
return "", err
}

return filepath.EvalSymlinks(path)
}

func DirName() (string, error) {
dir, err := os.Getwd()
if err != nil {
Expand Down

0 comments on commit 21089ec

Please sign in to comment.