mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-05 12:10:37 +00:00
Compare commits
7 Commits
d1aad0293a
...
30451c67d7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30451c67d7 | ||
|
|
82f3e1497e | ||
|
|
6251e95af8 | ||
|
|
b2155ebb00 | ||
|
|
911e3942e3 | ||
|
|
35d338cc24 | ||
|
|
6d7aa384c4 |
24
README.md
24
README.md
@ -423,6 +423,30 @@ Flags:
|
||||
|
||||
Use "yq [command] --help" for more information about a command.
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
### Color Customization
|
||||
|
||||
You can customize the colors used in YAML output by setting these environment variables:
|
||||
|
||||
- `YQ_COLOR_BOOL` - Color for boolean values (default: hi-magenta)
|
||||
- `YQ_COLOR_NUMBER` - Color for numeric values (default: hi-magenta)
|
||||
- `YQ_COLOR_MAP_KEY` - Color for map keys (default: cyan)
|
||||
- `YQ_COLOR_ANCHOR` - Color for YAML anchors (default: hi-yellow)
|
||||
- `YQ_COLOR_ALIAS` - Color for YAML aliases (default: hi-yellow)
|
||||
- `YQ_COLOR_STRING` - Color for string values (default: green)
|
||||
- `YQ_COLOR_COMMENT` - Color for comments (default: hi-black)
|
||||
|
||||
Supported color values: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, and their `hi-` prefixed variants (e.g., `hi-red`).
|
||||
|
||||
Example:
|
||||
```bash
|
||||
export YQ_COLOR_STRING=red
|
||||
export YQ_COLOR_MAP_KEY=hi-blue
|
||||
yq -C '.data' file.yaml
|
||||
```
|
||||
|
||||
## Known Issues / Missing Features
|
||||
- `yq` attempts to preserve comment positions and whitespace as much as possible, but it does not handle all scenarios (see https://github.com/go-yaml/yaml/tree/v3 for details)
|
||||
- Powershell has its own...[opinions on quoting yq](https://mikefarah.gitbook.io/yq/usage/tips-and-tricks#quotes-in-windows-powershell)
|
||||
|
||||
@ -11,7 +11,7 @@ var (
|
||||
GitDescribe string
|
||||
|
||||
// Version is main version number that is being run at the moment.
|
||||
Version = "v4.47.1"
|
||||
Version = "v4.47.2"
|
||||
|
||||
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
|
||||
// then it means that it is a final release. Otherwise, this is a pre-release
|
||||
|
||||
4
go.mod
4
go.mod
@ -14,8 +14,8 @@ require (
|
||||
github.com/magiconair/properties v1.8.10
|
||||
github.com/pelletier/go-toml/v2 v2.2.4
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
|
||||
github.com/spf13/cobra v1.9.1
|
||||
github.com/spf13/pflag v1.0.7
|
||||
github.com/spf13/cobra v1.10.1
|
||||
github.com/spf13/pflag v1.0.10
|
||||
github.com/yuin/gopher-lua v1.1.1
|
||||
go.yaml.in/yaml/v3 v3.0.4
|
||||
golang.org/x/net v0.43.0
|
||||
|
||||
10
go.sum
10
go.sum
@ -40,11 +40,11 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
|
||||
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
|
||||
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
|
||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
|
||||
93
pkg/yqlib/color_config.go
Normal file
93
pkg/yqlib/color_config.go
Normal file
@ -0,0 +1,93 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ColorConfig struct {
|
||||
Bool color.Attribute
|
||||
Number color.Attribute
|
||||
MapKey color.Attribute
|
||||
Anchor color.Attribute
|
||||
Alias color.Attribute
|
||||
String color.Attribute
|
||||
Comment color.Attribute
|
||||
}
|
||||
|
||||
func NewColorConfig() *ColorConfig {
|
||||
config := &ColorConfig{
|
||||
Bool: color.FgHiMagenta,
|
||||
Number: color.FgHiMagenta,
|
||||
MapKey: color.FgCyan,
|
||||
Anchor: color.FgHiYellow,
|
||||
Alias: color.FgHiYellow,
|
||||
String: color.FgGreen,
|
||||
Comment: color.FgHiBlack,
|
||||
}
|
||||
|
||||
colorMappings := map[string]*color.Attribute{
|
||||
"YQ_COLOR_BOOL": &config.Bool,
|
||||
"YQ_COLOR_NUMBER": &config.Number,
|
||||
"YQ_COLOR_MAP_KEY": &config.MapKey,
|
||||
"YQ_COLOR_ANCHOR": &config.Anchor,
|
||||
"YQ_COLOR_ALIAS": &config.Alias,
|
||||
"YQ_COLOR_STRING": &config.String,
|
||||
"YQ_COLOR_COMMENT": &config.Comment,
|
||||
}
|
||||
|
||||
for envVar, configField := range colorMappings {
|
||||
if colorStr := os.Getenv(envVar); colorStr != "" {
|
||||
if attr, err := parseColorAttribute(colorStr); err == nil {
|
||||
*configField = attr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// parseColorAttribute converts a color string to a color.Attribute.
|
||||
//
|
||||
// Supports three types of color specifications:
|
||||
// 1. Standard color names: "red", "green", "blue", "yellow", "magenta", "cyan", "white", "black"
|
||||
// 2. High-intensity variants: "hi-red", "hi-green", "hi-blue", etc.
|
||||
func parseColorAttribute(colorStr string) (color.Attribute, error) {
|
||||
switch colorStr {
|
||||
case "black":
|
||||
return color.FgBlack, nil
|
||||
case "red":
|
||||
return color.FgRed, nil
|
||||
case "green":
|
||||
return color.FgGreen, nil
|
||||
case "yellow":
|
||||
return color.FgYellow, nil
|
||||
case "blue":
|
||||
return color.FgBlue, nil
|
||||
case "magenta":
|
||||
return color.FgMagenta, nil
|
||||
case "cyan":
|
||||
return color.FgCyan, nil
|
||||
case "white":
|
||||
return color.FgWhite, nil
|
||||
case "hi-black":
|
||||
return color.FgHiBlack, nil
|
||||
case "hi-red":
|
||||
return color.FgHiRed, nil
|
||||
case "hi-green":
|
||||
return color.FgHiGreen, nil
|
||||
case "hi-yellow":
|
||||
return color.FgHiYellow, nil
|
||||
case "hi-blue":
|
||||
return color.FgHiBlue, nil
|
||||
case "hi-magenta":
|
||||
return color.FgHiMagenta, nil
|
||||
case "hi-cyan":
|
||||
return color.FgHiCyan, nil
|
||||
case "hi-white":
|
||||
return color.FgHiWhite, nil
|
||||
default:
|
||||
return color.Reset, fmt.Errorf("unknown color: %s", colorStr)
|
||||
}
|
||||
}
|
||||
@ -19,46 +19,47 @@ func format(attr color.Attribute) string {
|
||||
|
||||
func colorizeAndPrint(yamlBytes []byte, writer io.Writer) error {
|
||||
tokens := lexer.Tokenize(string(yamlBytes))
|
||||
config := NewColorConfig()
|
||||
var p printer.Printer
|
||||
p.Bool = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgHiMagenta),
|
||||
Prefix: format(config.Bool),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
p.Number = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgHiMagenta),
|
||||
Prefix: format(config.Number),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
p.MapKey = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgCyan),
|
||||
Prefix: format(config.MapKey),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
p.Anchor = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgHiYellow),
|
||||
Prefix: format(config.Anchor),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
p.Alias = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgHiYellow),
|
||||
Prefix: format(config.Alias),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
p.String = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgGreen),
|
||||
Prefix: format(config.String),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
p.Comment = func() *printer.Property {
|
||||
return &printer.Property{
|
||||
Prefix: format(color.FgHiBlack),
|
||||
Prefix: format(config.Comment),
|
||||
Suffix: format(color.Reset),
|
||||
}
|
||||
}
|
||||
|
||||
@ -282,13 +282,13 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
|
||||
tableValue = dec.parser.Expression()
|
||||
// next expression is not table data, so we are done
|
||||
if tableValue.Kind != toml.KeyValue {
|
||||
log.Debug("got an empty table, returning")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return false, err
|
||||
log.Debug("got an empty table")
|
||||
runAgainstCurrentExp = true
|
||||
} else {
|
||||
runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ street = "first street"
|
||||
[owner]
|
||||
name = "Tom Preston-Werner"
|
||||
`
|
||||
|
||||
var expectedTableArrayBeforeOwners = `owner:
|
||||
addresses:
|
||||
- street: first street
|
||||
@ -67,6 +68,29 @@ var emptyTable = `
|
||||
|
||||
var emptyTableExpected = "dependencies: {}\n"
|
||||
|
||||
var multipleEmptyTables = `
|
||||
[firstEmptyTable]
|
||||
[firstTableWithContent]
|
||||
key = "value"
|
||||
[secondEmptyTable]
|
||||
[thirdEmptyTable]
|
||||
[secondTableWithContent]
|
||||
key = "value"
|
||||
[fourthEmptyTable]
|
||||
[fifthEmptyTable]
|
||||
`
|
||||
|
||||
var expectedMultipleEmptyTables = `firstEmptyTable: {}
|
||||
firstTableWithContent:
|
||||
key: value
|
||||
secondEmptyTable: {}
|
||||
thirdEmptyTable: {}
|
||||
secondTableWithContent:
|
||||
key: value
|
||||
fourthEmptyTable: {}
|
||||
fifthEmptyTable: {}
|
||||
`
|
||||
|
||||
var sampleWithHeader = `
|
||||
[servers]
|
||||
|
||||
@ -238,6 +262,13 @@ var tomlScenarios = []formatScenario{
|
||||
expected: expectedSampleWithHeader,
|
||||
scenarioType: "decode",
|
||||
},
|
||||
{
|
||||
description: "Parse: multiple empty tables",
|
||||
skipDoc: true,
|
||||
input: multipleEmptyTables,
|
||||
expected: expectedMultipleEmptyTables,
|
||||
scenarioType: "decode",
|
||||
},
|
||||
}
|
||||
|
||||
func testTomlScenario(t *testing.T, s formatScenario) {
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
4.47.2:
|
||||
- Conversion from TOML to JSON no longer omits empty tables #2459 (thanks @louislouislouislouis)
|
||||
- Bumped dependencies
|
||||
|
||||
4.47.1:
|
||||
- Fixed merge anchor behaviour (`<<`); #2404, #2110, #2386, #2178 Huge thanks to @stevenwdv!
|
||||
Note that you will need to set --yaml-fix-merge-anchor-to-spec to see the fixes
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
name: yq
|
||||
version: 'v4.47.1'
|
||||
version: 'v4.47.2'
|
||||
summary: A lightweight and portable command-line data file processor
|
||||
description: |
|
||||
`yq` uses [jq](https://github.com/stedolan/jq) like syntax but works with yaml, json, xml, csv, properties and TOML files.
|
||||
@ -27,6 +27,6 @@ parts:
|
||||
build-environment:
|
||||
- CGO_ENABLED: 0
|
||||
source: https://github.com/mikefarah/yq.git
|
||||
source-tag: v4.47.1
|
||||
source-tag: v4.47.2
|
||||
build-snaps:
|
||||
- go/latest/stable
|
||||
|
||||
Loading…
Reference in New Issue
Block a user