Skip to content

Commit

Permalink
Look up .txt help pages when man is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jan 25, 2016
1 parent fa2ba49 commit 7e62ef8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
74 changes: 45 additions & 29 deletions commands/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package commands
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"path/filepath"

"github.com/github/hub/cmd"
"github.com/github/hub/git"
Expand Down Expand Up @@ -39,49 +39,65 @@ func runHelp(helpCmd *Command, args *Args) {
command := args.FirstParam()

if command == "hub" {
man := cmd.New("man")
man.WithArg("hub")
err := man.Run()
if err == nil {
os.Exit(0)
} else {
os.Exit(1)
err := displayManPage("hub.1", args)
if err != nil {
utils.Check(err)
}
}

if c := lookupCmd(command); c != nil {
manProgram, err := utils.CommandPath("man")
if err == nil && !args.HasFlags("--plain-text") {
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 !args.HasFlags("--plain-text") {
manPage := fmt.Sprintf("hub-%s.1", c.Name())
err := displayManPage(manPage, args)
if err == nil {
os.Exit(0)
} else {
os.Exit(1)
return
}
} else {
ui.Println(c.HelpText())
os.Exit(0)
}

ui.Println(c.HelpText())
os.Exit(0)
}
}

func localManPage(cmd string, args *Args) (string, error) {
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
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(filepath.Dir(programPath), "..", "man", cmd + ".1")
manPath = filepath.Join(installPrefix, "share", "man", "man1", name)
_, err = os.Stat(manPath)
if err == nil {
return manPath, nil
Expand Down
6 changes: 3 additions & 3 deletions features/help.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ Feature: hub help
When I successfully run `hub help -a`
Then the output should contain "pull-request"

Scenario: Opens man page
When I successfully run `hub help hub`
Then "man hub" should be run
Scenario: Shows help for a subcommand
When I successfully run `hub help hub-help`
Then the output should contain "Usage: hub help"

0 comments on commit 7e62ef8

Please sign in to comment.