addressed PR comments
This commit is contained in:
parent
6a3c881d8e
commit
f2808e4c71
|
|
@ -95,8 +95,9 @@ var (
|
||||||
connectTimeout = flags.Float64("connect-timeout", 0, prettify(`
|
connectTimeout = flags.Float64("connect-timeout", 0, prettify(`
|
||||||
The maximum time, in seconds, to wait for connection to be established.
|
The maximum time, in seconds, to wait for connection to be established.
|
||||||
Defaults to 10 seconds.`))
|
Defaults to 10 seconds.`))
|
||||||
errorFormat = flags.String("error-format", "text", prettify(`
|
formatError = flags.Bool("format-error", false, prettify(`
|
||||||
The format of a non-zero status response. The allowed values are 'json' or 'text'.`))
|
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(`
|
keepaliveTime = flags.Float64("keepalive-time", 0, prettify(`
|
||||||
If present, the maximum idle time in seconds, after which a keepalive
|
If present, the maximum idle time in seconds, after which a keepalive
|
||||||
probe is sent. If the connection remains idle and no keepalive response
|
probe is sent. If the connection remains idle and no keepalive response
|
||||||
|
|
@ -274,9 +275,6 @@ func main() {
|
||||||
if *format != "json" && *format != "text" {
|
if *format != "json" && *format != "text" {
|
||||||
fail(nil, "The -format option must be 'json' or '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" {
|
if *emitDefaults && *format != "json" {
|
||||||
warn("The -emit-defaults is only used when using json format.")
|
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)
|
fmt.Printf("Sent %d request%s and received %d response%s\n", reqCount, reqSuffix, h.NumResponses, respSuffix)
|
||||||
}
|
}
|
||||||
if h.Status.Code() != codes.OK {
|
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)
|
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)
|
// HandleFormatAndPrintStatus passes the writer, status object, and formatter (if applicable)
|
||||||
// to the appropriate status printing function
|
// 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 {
|
switch format {
|
||||||
case FormatJSON:
|
case FormatJSON:
|
||||||
marshaler := jsonpb.Marshaler{Indent: " "}
|
formatter = NewJSONFormatter(
|
||||||
PrintFormattedStatus(w, stat, marshaler.MarshalToString)
|
emitJSONDefaultFields,
|
||||||
|
anyResolverWithFallback{AnyResolver: jsonpb.Marshaler{Indent: " "}.AnyResolver})
|
||||||
case FormatText:
|
case FormatText:
|
||||||
PrintStatus(w, stat, formatter)
|
formatter = NewTextFormatter(includeTextSeparator)
|
||||||
default:
|
default:
|
||||||
fmt.Errorf("unknown format: %s", format)
|
fmt.Errorf("unknown format: %s", format)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
PrintFormattedStatus(w, stat, formatter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintStatus prints details about the given status to the given writer. The given
|
// 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) {
|
func TestPrintFormattedStatus(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
input *status.Status
|
input *status.Status
|
||||||
|
format Format
|
||||||
expectedOutput string
|
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 {
|
for _, tc := range testCases {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
HandleFormatAndPrintStatus(FormatJSON, &b, tc.input, nil)
|
HandleFormatAndPrintStatus(tc.format, &b, tc.input, false, true)
|
||||||
got := b.String()
|
got := b.String()
|
||||||
if !compare(tc.expectedOutput, got) {
|
if !compare(tc.expectedOutput, got) {
|
||||||
t.Errorf("Incorrect output. Expected:\n%s\nGot:\n%s", tc.expectedOutput, got)
|
t.Errorf("Incorrect output. Expected:\n%s\nGot:\n%s", tc.expectedOutput, got)
|
||||||
|
|
@ -269,6 +278,8 @@ Response contents:
|
||||||
"code": 3,
|
"code": 3,
|
||||||
"message": "Missing Argument"
|
"message": "Missing Argument"
|
||||||
}`
|
}`
|
||||||
|
statusAsText = `code: 3
|
||||||
|
message: "Missing Argument"`
|
||||||
messageAsText = `struct_value: <
|
messageAsText = `struct_value: <
|
||||||
fields: <
|
fields: <
|
||||||
key: "bar"
|
key: "bar"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue