From 11ad2b36c828c166547b33345d724667da2ae93a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 4 May 2016 19:41:33 -0400 Subject: [PATCH 1/6] Assert output in some README examples --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 06f7b84..2ac96fd 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,9 @@ func main() { This app will run and show help text, but is not very useful. Let's give an action to execute and some help documentation: + ``` go package main @@ -77,6 +80,9 @@ Being a programmer can be a lonely job. Thankfully by the power of automation th Start by creating a directory named `greet`, and within it, add a file, `greet.go` with the following code in it: + ``` go package main @@ -210,6 +216,7 @@ Sometimes it's useful to specify a flag's value within the usage string itself. indicated with back quotes. For example this: + ```go cli.StringFlag{ Name: "config, c", @@ -504,6 +511,9 @@ templates are exposed as variables `AppHelpTemplate`, `CommandHelpTemplate`, and is possible by assigning a compatible func to the `cli.HelpPrinter` variable, e.g.: + ``` go package main From 2a256d4c5397fb0e91ab71cc73787698d13023e0 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Thu, 5 May 2016 10:26:53 -0400 Subject: [PATCH 2/6] Provide a variable for writing output with a default of os.Stderr --- app.go | 6 +++--- errors.go | 6 +++++- flag.go | 2 +- help.go | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index 89c741b..28bf78b 100644 --- a/app.go +++ b/app.go @@ -228,11 +228,11 @@ func (a *App) Run(arguments []string) (err error) { // DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling func (a *App) RunAndExitOnError() { - fmt.Fprintf(os.Stderr, + fmt.Fprintf(OutWriter, "DEPRECATED cli.App.RunAndExitOnError. %s See %s\n", contactSysadmin, runAndExitOnErrorDeprecationURL) if err := a.Run(os.Args); err != nil { - fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(OutWriter, err) OsExiter(1) } } @@ -422,7 +422,7 @@ func HandleAction(action interface{}, context *Context) (err error) { vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)}) if len(vals) == 0 { - fmt.Fprintf(os.Stderr, + fmt.Fprintf(OutWriter, "DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n", contactSysadmin, appActionDeprecationURL) return nil diff --git a/errors.go b/errors.go index 5f1e83b..c03e676 100644 --- a/errors.go +++ b/errors.go @@ -8,6 +8,10 @@ import ( var OsExiter = os.Exit +// OutWriter is used to write output to the user. This can be anything +// implementing the io.Writer interface and defaults to os.Stderr. +var OutWriter = os.Stderr + type MultiError struct { Errors []error } @@ -69,7 +73,7 @@ func HandleExitCoder(err error) { if exitErr, ok := err.(ExitCoder); ok { if err.Error() != "" { - fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(OutWriter, err) } OsExiter(exitErr.ExitCode()) return diff --git a/flag.go b/flag.go index 3b6a2e1..7778a2d 100644 --- a/flag.go +++ b/flag.go @@ -220,7 +220,7 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) { s = strings.TrimSpace(s) err := newVal.Set(s) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(OutWriter, err.Error()) } } f.Value = newVal diff --git a/help.go b/help.go index 45e8603..79f2e85 100644 --- a/help.go +++ b/help.go @@ -188,7 +188,7 @@ func printHelp(out io.Writer, templ string, data interface{}) { err := t.Execute(w, data) if err != nil { // If the writer is closed, t.Execute will fail, and there's nothing - // we can do to recover. We could send this to os.Stderr if we need. + // we can do to recover. We could send this to OutWriter if we need. return } w.Flush() From 6f0b442222239d0c6c6a999742e6d56b58494d7e Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Fri, 6 May 2016 12:14:26 -0400 Subject: [PATCH 3/6] Update to ErrWriter and make available on app --- app.go | 18 +++++++++++++++--- errors.go | 7 ++++--- flag.go | 2 +- help.go | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index 28bf78b..4d18d24 100644 --- a/app.go +++ b/app.go @@ -82,6 +82,8 @@ type App struct { Email string // Writer writer to write output to Writer io.Writer + // ErrWriter writes error output + ErrWriter io.Writer // Other custom info Metadata map[string]interface{} } @@ -228,11 +230,11 @@ func (a *App) Run(arguments []string) (err error) { // DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling func (a *App) RunAndExitOnError() { - fmt.Fprintf(OutWriter, + fmt.Fprintf(a.errWriter(), "DEPRECATED cli.App.RunAndExitOnError. %s See %s\n", contactSysadmin, runAndExitOnErrorDeprecationURL) if err := a.Run(os.Args); err != nil { - fmt.Fprintln(OutWriter, err) + fmt.Fprintln(a.errWriter(), err) OsExiter(1) } } @@ -377,6 +379,16 @@ func (a *App) hasFlag(flag Flag) bool { return false } +func (a *App) errWriter() io.Writer { + + // When the app ErrWriter is nil use the package level one. + if a.ErrWriter == nil { + return ErrWriter + } + + return a.ErrWriter +} + func (a *App) appendFlag(flag Flag) { if !a.hasFlag(flag) { a.Flags = append(a.Flags, flag) @@ -422,7 +434,7 @@ func HandleAction(action interface{}, context *Context) (err error) { vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)}) if len(vals) == 0 { - fmt.Fprintf(OutWriter, + fmt.Fprintf(ErrWriter, "DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n", contactSysadmin, appActionDeprecationURL) return nil diff --git a/errors.go b/errors.go index c03e676..db46a83 100644 --- a/errors.go +++ b/errors.go @@ -2,15 +2,16 @@ package cli import ( "fmt" + "io" "os" "strings" ) var OsExiter = os.Exit -// OutWriter is used to write output to the user. This can be anything +// ErrWriter is used to write errors to the user. This can be anything // implementing the io.Writer interface and defaults to os.Stderr. -var OutWriter = os.Stderr +var ErrWriter io.Writer = os.Stderr type MultiError struct { Errors []error @@ -73,7 +74,7 @@ func HandleExitCoder(err error) { if exitErr, ok := err.(ExitCoder); ok { if err.Error() != "" { - fmt.Fprintln(OutWriter, err) + fmt.Fprintln(ErrWriter, err) } OsExiter(exitErr.ExitCode()) return diff --git a/flag.go b/flag.go index 7778a2d..8354de0 100644 --- a/flag.go +++ b/flag.go @@ -220,7 +220,7 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) { s = strings.TrimSpace(s) err := newVal.Set(s) if err != nil { - fmt.Fprintf(OutWriter, err.Error()) + fmt.Fprintf(ErrWriter, err.Error()) } } f.Value = newVal diff --git a/help.go b/help.go index 79f2e85..f4ea7a3 100644 --- a/help.go +++ b/help.go @@ -188,7 +188,7 @@ func printHelp(out io.Writer, templ string, data interface{}) { err := t.Execute(w, data) if err != nil { // If the writer is closed, t.Execute will fail, and there's nothing - // we can do to recover. We could send this to OutWriter if we need. + // we can do to recover. We could send this to ErrWriter if we need. return } w.Flush() From cd230f3a88267fff46df1f1763978494c08c8b29 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Fri, 6 May 2016 12:19:01 -0400 Subject: [PATCH 4/6] Update travis config for Go versions - Added Go 1.6 testing - Updated 1.5.x and 1.4.x to latest point releases --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 133722f..1707c50 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,9 @@ go: - 1.1.2 - 1.2.2 - 1.3.3 -- 1.4.2 -- 1.5.1 +- 1.4.3 +- 1.5.4 +- 1.6.2 - tip matrix: From e9970b7b13333aba175bb7cb4c3b3ca07e3fba34 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Fri, 6 May 2016 12:24:51 -0400 Subject: [PATCH 5/6] Adding Go Report Card badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2ac96fd..c1709ce 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Build Status](https://travis-ci.org/codegangsta/cli.svg?branch=master)](https://travis-ci.org/codegangsta/cli) [![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli) [![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-codegangsta-cli) +[![Go Report Card](https://goreportcard.com/badge/codegangsta/cli)](https://goreportcard.com/report/codegangsta/cli) # cli From 0a3c5e751657e5ab9ff65bedefb07a29cea6a5b9 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Fri, 6 May 2016 12:30:30 -0400 Subject: [PATCH 6/6] Letting Travis CI select the patch version of Go 1.4 to use The last release of Go 1.4 when installed on Travis CI does not have go vet installed. Letting Travis CI select the patch version instead. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1707c50..76f38a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ go: - 1.1.2 - 1.2.2 - 1.3.3 -- 1.4.3 +- 1.4 - 1.5.4 - 1.6.2 - tip