Skip to content

Commit

Permalink
Support --indent flag on Query() command
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Rutkowski <[email protected]>
  • Loading branch information
mrutkows committed Nov 16, 2023
1 parent 685cf78 commit b4ca844
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
13 changes: 4 additions & 9 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,11 @@ func Query(writer io.Writer, request *common.QueryRequest, response *common.Quer
return
}

// Convert query results to formatted JSON for output
// TODO: we MAY want to use a JSON Encoder to avoid unicode encoding
var formattedResult string
if formattedResult, err = utils.MarshalAnyToFormattedJsonString(resultJson); err != nil {
getLogger().Debugf("unhandled error: %s, QueryRequest: %s", err, request.String())
return
}

// Convert query results to encoded JSON for output
indent := utils.GenerateIndentString(int(utils.GlobalFlags.PersistentFlags.OutputIndent))
buffer, err := utils.EncodeAnyToIndentedJSON(resultJson, indent)
// Use the selected output device (e.g., default stdout or the specified --output-file)
fmt.Fprintf(writer, "%s\n", formattedResult)
writer.Write(buffer.Bytes())

return
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ func innerQuery(t *testing.T, filename string, queryRequest *common.QueryRequest
return
}

// This will print results ONLY if --quiet mode is `false`
printMarshaledResultOnlyIfNotQuiet(result)
// Log results if trace enabled
var buffer bytes.Buffer
buffer, err = utils.EncodeAnyToIndentedJSON(
result, utils.DEFAULT_JSON_INDENT_STRING)
if err != nil {
// Output the JSON data directly to stdout (not subject to log-level)
getLogger().Tracef("%s\n", buffer.String())
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&utils.GlobalFlags.LogOutputIndentCallstack, FLAG_LOG_OUTPUT_INDENT, "", false, MSG_FLAG_LOG_INDENT)

// Output (JSON) indent
rootCmd.Flags().Uint8VarP(&utils.GlobalFlags.PersistentFlags.OutputIndent, FLAG_OUTPUT_INDENT, "", DEFAULT_OUTPUT_INDENT_LENGTH, MSG_FLAG_OUTPUT_INDENT)
rootCmd.PersistentFlags().Uint8VarP(&utils.GlobalFlags.PersistentFlags.OutputIndent, FLAG_OUTPUT_INDENT, "", DEFAULT_OUTPUT_INDENT_LENGTH, MSG_FLAG_OUTPUT_INDENT)

// Add root commands
rootCmd.AddCommand(NewCommandVersion())
Expand Down
10 changes: 0 additions & 10 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,3 @@ func bufferContainsValues(buffer bytes.Buffer, values ...string) bool {
}
return true
}

// TODO: find a better way using some log package feature
func printMarshaledResultOnlyIfNotQuiet(iResult interface{}) {
if !*TestLogQuiet {
// Format results in JSON
fResult, _ := utils.MarshalAnyToFormattedJsonString(iResult)
// Output the JSON data directly to stdout (not subject to log-level)
fmt.Printf("%s\n", fResult)
}
}

0 comments on commit b4ca844

Please sign in to comment.