Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
subchen committed May 25, 2018
2 parents 8f559e2 + a2bcfae commit a8b55c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
22 changes: 13 additions & 9 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"io"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -49,6 +50,9 @@ Run '{{.Name}} COMMAND --help' for more information on a command.{{end}}
`

// helpWriter variable for testing hook
var helpWriter io.Writer = os.Stdout

// HelpContext is a struct for output help
type HelpContext struct {
Name string
Expand Down Expand Up @@ -271,27 +275,27 @@ func showHelp(c *HelpContext) {
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, c)
err = tmpl.Execute(helpWriter, c)
if err != nil {
panic(err)
}
}

func showVersion(app *App) {
fmt.Printf("Name: %s\n", app.Name)
fmt.Printf("Version: %s\n", app.Version)
fmt.Fprintf(helpWriter, "Name: %s\n", app.Name)
fmt.Fprintf(helpWriter, "Version: %s\n", app.Version)
if app.BuildInfo.GitRevCount != "" {
fmt.Printf("Patches: %s\n", app.BuildInfo.GitRevCount)
fmt.Fprintf(helpWriter, "Patches: %s\n", app.BuildInfo.GitRevCount)
}
if app.BuildInfo.GitBranch != "" {
fmt.Printf("Git branch: %s\n", app.BuildInfo.GitBranch)
fmt.Fprintf(helpWriter, "Git branch: %s\n", app.BuildInfo.GitBranch)
}
if app.BuildInfo.GitCommit != "" {
fmt.Printf("Git commit: %s\n", app.BuildInfo.GitCommit)
fmt.Fprintf(helpWriter, "Git commit: %s\n", app.BuildInfo.GitCommit)
}
if app.BuildInfo.Timestamp != "" {
fmt.Printf("Built: %s\n", app.BuildInfo.Timestamp)
fmt.Fprintf(helpWriter, "Built: %s\n", app.BuildInfo.Timestamp)
}
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Printf("OS/Arch: %s/%v\n", runtime.GOOS, runtime.GOARCH)
fmt.Fprintf(helpWriter, "Go version: %s\n", runtime.Version())
fmt.Fprintf(helpWriter, "OS/Arch: %s/%v\n", runtime.GOOS, runtime.GOARCH)
}
7 changes: 7 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"bytes"
"testing"
)

Expand All @@ -16,6 +17,9 @@ func TestHelpShowVersion(t *testing.T) {
},
}

// reset
helpWriter = new(bytes.Buffer)

showVersion(app)
}

Expand Down Expand Up @@ -56,6 +60,9 @@ func TestHelpShowHelp(t *testing.T) {
},
}

// reset
helpWriter = new(bytes.Buffer)

ctx1 := newAppHelpContext("app", app)
showHelp(ctx1)

Expand Down

0 comments on commit a8b55c8

Please sign in to comment.