Indent multi-line messages/errors.

This commit is contained in:
Alec Thomas
2018-06-20 12:44:18 +10:00
parent 9a68d32e72
commit 3a2f3eebdd
4 changed files with 25 additions and 5 deletions
+1
View File
@@ -31,3 +31,4 @@ issues:
# Very commonly not checked. # Very commonly not checked.
- 'Error return value of .(.*\.Help|.*\.MarkFlagRequired|(os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked' - 'Error return value of .(.*\.Help|.*\.MarkFlagRequired|(os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked'
- 'exported method (.*\.MarshalJSON|.*\.UnmarshalJSON) should have comment or be unexported' - 'exported method (.*\.MarshalJSON|.*\.UnmarshalJSON) should have comment or be unexported'
- 'composite literal uses unkeyed fields'
+1 -1
View File
@@ -49,7 +49,7 @@ func flattenedFields(v reflect.Value) (out []flattenedField) {
if !fv.CanSet() { if !fv.CanSet() {
continue continue
} }
out = append(out, flattenedField{ft, fv}) out = append(out, flattenedField{field: ft, value: fv})
} }
return return
} }
+13 -4
View File
@@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
) )
// Error reported by Kong. // Error reported by Kong.
@@ -14,7 +15,7 @@ type Error struct{ msg string }
func (e Error) Error() string { return e.msg } func (e Error) Error() string { return e.msg }
func fail(format string, args ...interface{}) { func fail(format string, args ...interface{}) {
panic(Error{fmt.Sprintf(format, args...)}) panic(Error{msg: fmt.Sprintf(format, args...)})
} }
// Must creates a new Parser or panics if there is an error. // Must creates a new Parser or panics if there is an error.
@@ -170,15 +171,23 @@ func (k *Kong) applyHooks(ctx *Context) error {
return nil return nil
} }
func formatMultilineMessage(w io.Writer, leader string, format string, args ...interface{}) {
lines := strings.Split(fmt.Sprintf(format, args...), "\n")
fmt.Fprintf(w, "%s%s\n", leader, lines[0])
for _, line := range lines[1:] {
fmt.Fprintf(w, "%*s%s\n", len(leader), " ", line)
}
}
// Printf writes a message to Kong.Stdout with the application name prefixed. // Printf writes a message to Kong.Stdout with the application name prefixed.
func (k *Kong) Printf(format string, args ...interface{}) *Kong { func (k *Kong) Printf(format string, args ...interface{}) *Kong {
fmt.Fprintf(k.Stdout, k.Model.Name+": "+format, args...) formatMultilineMessage(k.Stdout, k.Model.Name+": ", format, args...)
return k return k
} }
// Errorf writes a message to Kong.Stderr with the application name prefixed. // Errorf writes a message to Kong.Stderr with the application name prefixed.
func (k *Kong) Errorf(format string, args ...interface{}) *Kong { func (k *Kong) Errorf(format string, args ...interface{}) *Kong {
fmt.Fprintf(k.Stderr, k.Model.Name+": error: "+format, args...) formatMultilineMessage(k.Stderr, k.Model.Name+": error: ", format, args...)
return k return k
} }
@@ -191,7 +200,7 @@ func (k *Kong) FatalIfErrorf(err error, args ...interface{}) {
if len(args) > 0 { if len(args) > 0 {
msg = fmt.Sprintf(args[0].(string), args[1:]...) + ": " + err.Error() msg = fmt.Sprintf(args[0].(string), args[1:]...) + ": " + err.Error()
} }
k.Errorf("%s\n", msg) k.Errorf("%s", msg)
k.Exit(1) k.Exit(1)
} }
+10
View File
@@ -1,6 +1,7 @@
package kong package kong
import ( import (
"bytes"
"strings" "strings"
"testing" "testing"
@@ -10,6 +11,7 @@ import (
func mustNew(t *testing.T, cli interface{}, options ...Option) *Kong { func mustNew(t *testing.T, cli interface{}, options ...Option) *Kong {
t.Helper() t.Helper()
options = append([]Option{ options = append([]Option{
Name("test"),
Exit(func(int) { Exit(func(int) {
t.Helper() t.Helper()
t.Fatalf("unexpected exit()") t.Fatalf("unexpected exit()")
@@ -444,3 +446,11 @@ func TestSliceWithDisabledSeparator(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, []string{"a,b", "b,c"}, cli.Flag) require.Equal(t, []string{"a,b", "b,c"}, cli.Flag)
} }
func TestMultilineMessage(t *testing.T) {
w := &bytes.Buffer{}
var cli struct{}
p := mustNew(t, &cli, Writers(w, w))
p.Printf("hello\nworld")
require.Equal(t, "test: hello\n world\n", w.String())
}