addressed PR comments
This commit is contained in:
parent
6a3c881d8e
commit
f2808e4c71
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
format.go
13
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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue