From f2808e4c7109a571a893be712d9378cba34ce626 Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Thu, 14 May 2020 16:26:39 -0500 Subject: [PATCH] addressed PR comments --- cmd/grpcurl/grpcurl.go | 14 ++++++++------ format.go | 13 ++++++++----- format_test.go | 21 ++++++++++++++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/cmd/grpcurl/grpcurl.go b/cmd/grpcurl/grpcurl.go index 0ee5b92..adf734b 100644 --- a/cmd/grpcurl/grpcurl.go +++ b/cmd/grpcurl/grpcurl.go @@ -95,8 +95,9 @@ var ( connectTimeout = flags.Float64("connect-timeout", 0, prettify(` The maximum time, in seconds, to wait for connection to be established. Defaults to 10 seconds.`)) - errorFormat = flags.String("error-format", "text", prettify(` - The format of a non-zero status response. The allowed values are 'json' or 'text'.`)) + formatError = flags.Bool("format-error", false, prettify(` + When a non-zero status is returned, format the response using the + value set by the -format flag .`)) keepaliveTime = flags.Float64("keepalive-time", 0, prettify(` If present, the maximum idle time in seconds, after which a keepalive probe is sent. If the connection remains idle and no keepalive response @@ -274,9 +275,6 @@ func main() { if *format != "json" && *format != "text" { fail(nil, "The -format option must be 'json' or 'text'.") } - if *errorFormat != "json" && *errorFormat != "text" { - fail(nil, "The -error-format option must be 'json' or 'text'.") - } if *emitDefaults && *format != "json" { warn("The -emit-defaults is only used when using json format.") } @@ -657,7 +655,11 @@ func main() { fmt.Printf("Sent %d request%s and received %d response%s\n", reqCount, reqSuffix, h.NumResponses, respSuffix) } if h.Status.Code() != codes.OK { - grpcurl.HandleFormatAndPrintStatus(grpcurl.Format(*errorFormat), os.Stderr, h.Status, formatter) + if *formatError { + grpcurl.HandleFormatAndPrintStatus(grpcurl.Format(*format), os.Stderr, h.Status, *emitDefaults, includeSeparators) + } else { + grpcurl.PrintStatus(os.Stderr, h.Status, formatter) + } exit(1) } } diff --git a/format.go b/format.go index 2868e80..65aecb1 100644 --- a/format.go +++ b/format.go @@ -431,17 +431,20 @@ func (h *DefaultEventHandler) OnReceiveTrailers(stat *status.Status, md metadata // HandleFormatAndPrintStatus passes the writer, status object, and formatter (if applicable) // to the appropriate status printing function -func HandleFormatAndPrintStatus(format Format, w io.Writer, stat *status.Status, formatter Formatter) { - +func HandleFormatAndPrintStatus(format Format, w io.Writer, stat *status.Status, emitJSONDefaultFields, includeTextSeparator bool) { + var formatter Formatter switch format { case FormatJSON: - marshaler := jsonpb.Marshaler{Indent: " "} - PrintFormattedStatus(w, stat, marshaler.MarshalToString) + formatter = NewJSONFormatter( + emitJSONDefaultFields, + anyResolverWithFallback{AnyResolver: jsonpb.Marshaler{Indent: " "}.AnyResolver}) case FormatText: - PrintStatus(w, stat, formatter) + formatter = NewTextFormatter(includeTextSeparator) default: fmt.Errorf("unknown format: %s", format) + return } + PrintFormattedStatus(w, stat, formatter) } // PrintStatus prints details about the given status to the given writer. The given diff --git a/format_test.go b/format_test.go index 459392c..a59d305 100644 --- a/format_test.go +++ b/format_test.go @@ -178,15 +178,24 @@ func TestHandler(t *testing.T) { func TestPrintFormattedStatus(t *testing.T) { testCases := []struct { input *status.Status + format Format expectedOutput string - }{{ - input: status.New(codes.InvalidArgument, "Missing Argument"), - expectedOutput: statusAsJSON, - }} + }{ + { + input: status.New(codes.InvalidArgument, "Missing Argument"), + format: FormatJSON, + expectedOutput: statusAsJSON, + }, + { + input: status.New(codes.InvalidArgument, "Missing Argument"), + format: FormatText, + expectedOutput: statusAsText, + }, + } for _, tc := range testCases { var b bytes.Buffer - HandleFormatAndPrintStatus(FormatJSON, &b, tc.input, nil) + HandleFormatAndPrintStatus(tc.format, &b, tc.input, false, true) got := b.String() if !compare(tc.expectedOutput, got) { t.Errorf("Incorrect output. Expected:\n%s\nGot:\n%s", tc.expectedOutput, got) @@ -269,6 +278,8 @@ Response contents: "code": 3, "message": "Missing Argument" }` + statusAsText = `code: 3 +message: "Missing Argument"` messageAsText = `struct_value: < fields: < key: "bar"