diff --git a/cmd/utils.go b/cmd/utils.go index 69aeec18..ab3b1a12 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -91,7 +91,7 @@ func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yq func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder { switch format { case yqlib.JSONOutputFormat: - return yqlib.NewJONEncoder(indent) + return yqlib.NewJONEncoder(indent, colorsEnabled) case yqlib.PropsOutputFormat: return yqlib.NewPropertiesEncoder() case yqlib.CSVOutputFormat: diff --git a/pkg/yqlib/encoder_json.go b/pkg/yqlib/encoder_json.go index e76a0180..8d2660a3 100644 --- a/pkg/yqlib/encoder_json.go +++ b/pkg/yqlib/encoder_json.go @@ -1,6 +1,7 @@ package yqlib import ( + "bytes" "encoding/json" "io" @@ -9,6 +10,7 @@ import ( type jsonEncoder struct { indentString string + colorise bool } func mapKeysToStrings(node *yaml.Node) { @@ -26,14 +28,14 @@ func mapKeysToStrings(node *yaml.Node) { } } -func NewJONEncoder(indent int) Encoder { +func NewJONEncoder(indent int, colorise bool) Encoder { var indentString = "" for index := 0; index < indent; index++ { indentString = indentString + " " } - return &jsonEncoder{indentString} + return &jsonEncoder{indentString, colorise} } func (je *jsonEncoder) CanHandleAliases() bool { @@ -49,7 +51,14 @@ func (je *jsonEncoder) PrintLeadingContent(writer io.Writer, content string) err } func (je *jsonEncoder) Encode(writer io.Writer, node *yaml.Node) error { - var encoder = json.NewEncoder(writer) + + destination := writer + tempBuffer := bytes.NewBuffer(nil) + if je.colorise { + destination = tempBuffer + } + + var encoder = json.NewEncoder(destination) encoder.SetEscapeHTML(false) // do not escape html chars e.g. &, <, > encoder.SetIndent("", je.indentString) @@ -60,5 +69,12 @@ func (je *jsonEncoder) Encode(writer io.Writer, node *yaml.Node) error { if errorDecoding != nil { return errorDecoding } - return encoder.Encode(dataBucket) + err := encoder.Encode(dataBucket) + if err != nil { + return err + } + if je.colorise { + return colorizeAndPrint(tempBuffer.Bytes(), writer) + } + return nil } diff --git a/pkg/yqlib/encoder_test.go b/pkg/yqlib/encoder_test.go index 5e616a65..99687975 100644 --- a/pkg/yqlib/encoder_test.go +++ b/pkg/yqlib/encoder_test.go @@ -13,7 +13,7 @@ func yamlToJSON(sampleYaml string, indent int) string { var output bytes.Buffer writer := bufio.NewWriter(&output) - var jsonEncoder = NewJONEncoder(indent) + var jsonEncoder = NewJONEncoder(indent, false) inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder()) if err != nil { panic(err) diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 9e1ac66e..0a400434 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -97,7 +97,7 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode { func testJSONScenario(t *testing.T, s formatScenario) { if s.scenarioType == "encode" || s.scenarioType == "roundtrip" { - test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent)), s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent, false)), s.description) } else { var actual = resultToString(t, decodeJSON(t, s.input)) test.AssertResultWithContext(t, s.expected, actual, s.description) @@ -171,7 +171,7 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) { } writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent)))) + writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent, false)))) } func TestJSONScenarios(t *testing.T) { diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 174a059b..f662d15c 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -331,25 +331,23 @@ func parseInt64(numberString string) (string, int64, error) { return "%v", num, err } -func parseInt(numberString string) (string, int, error) { +func parseInt(numberString string) (int, error) { var err error var parsed int64 - format := "%v" if strings.HasPrefix(numberString, "0x") || strings.HasPrefix(numberString, "0X") { - format = "0x%X" parsed, err = strconv.ParseInt(numberString[2:], 16, 64) } else { parsed, err = strconv.ParseInt(numberString, 10, 64) } if err != nil { - return "", 0, err + return 0, err } else if parsed > math.MaxInt { - return "", 0, fmt.Errorf("%v is too big (larger than %v)", parsed, math.MaxInt) + return 0, fmt.Errorf("%v is too big (larger than %v)", parsed, math.MaxInt) } - return format, int(parsed), err + return int(parsed), err } func createScalarNode(value interface{}, stringValue string) *yaml.Node { diff --git a/pkg/yqlib/operator_encoder_decoder.go b/pkg/yqlib/operator_encoder_decoder.go index 0a74276d..3ccac2f1 100644 --- a/pkg/yqlib/operator_encoder_decoder.go +++ b/pkg/yqlib/operator_encoder_decoder.go @@ -13,7 +13,7 @@ import ( func configureEncoder(format PrinterOutputFormat, indent int) Encoder { switch format { case JSONOutputFormat: - return NewJONEncoder(indent) + return NewJONEncoder(indent, false) case PropsOutputFormat: return NewPropertiesEncoder() case CSVOutputFormat: diff --git a/pkg/yqlib/operator_pick.go b/pkg/yqlib/operator_pick.go index 2444c9b2..89063dc3 100644 --- a/pkg/yqlib/operator_pick.go +++ b/pkg/yqlib/operator_pick.go @@ -31,7 +31,7 @@ func pickSequence(original *yaml.Node, indices *yaml.Node) (*yaml.Node, error) { filteredContent := make([]*yaml.Node, 0) for index := 0; index < len(indices.Content); index = index + 1 { - _, indexInArray, err := parseInt(indices.Content[index].Value) + indexInArray, err := parseInt(indices.Content[index].Value) if err != nil { return nil, fmt.Errorf("cannot index array with %v", indices.Content[index].Value) } diff --git a/pkg/yqlib/operator_traverse_path.go b/pkg/yqlib/operator_traverse_path.go index 2aa9ef94..2b40282f 100644 --- a/pkg/yqlib/operator_traverse_path.go +++ b/pkg/yqlib/operator_traverse_path.go @@ -188,7 +188,7 @@ func traverseArrayWithIndices(candidate *CandidateNode, indices []*yaml.Node, pr for _, indexNode := range indices { log.Debug("traverseArrayWithIndices: '%v'", indexNode.Value) - _, index, err := parseInt(indexNode.Value) + index, err := parseInt(indexNode.Value) if err != nil && prefs.OptionalTraverse { continue } diff --git a/pkg/yqlib/printer_test.go b/pkg/yqlib/printer_test.go index a2d797e2..544009f3 100644 --- a/pkg/yqlib/printer_test.go +++ b/pkg/yqlib/printer_test.go @@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) { var writer = bufio.NewWriter(&output) // note printDocSeparators is true, it should still not print document separators // when outputing JSON. - printer := NewPrinter(NewJONEncoder(0), NewSinglePrinterWriter(writer)) + printer := NewPrinter(NewJONEncoder(0, false), NewSinglePrinterWriter(writer)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder()) if err != nil {