mirror of
https://github.com/mikefarah/yq.git
synced 2026-03-10 15:54:26 +00:00
- Add ShellVariablesPreferences struct with KeySeparator field (default: '_') - Update shellVariablesEncoder to use configurable separator - Add --shell-key-separator CLI flag - Add comprehensive tests for custom separator functionality - Update documentation with example usage for custom separator This feature allows users to specify a custom separator (e.g. '__') when outputting shell variables, which helps disambiguate nested keys from keys that contain underscores in their names. Example: yq -o=shell --shell-key-separator='__' file.yaml Fixes ambiguity when original YAML keys contain underscores.
138 lines
4.0 KiB
Go
138 lines
4.0 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
)
|
|
|
|
func assertEncodesTo(t *testing.T, yaml string, shellvars string) {
|
|
var output bytes.Buffer
|
|
writer := bufio.NewWriter(&output)
|
|
|
|
var encoder = NewShellVariablesEncoder()
|
|
inputs, err := readDocuments(strings.NewReader(yaml), "test.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
node := inputs.Front().Value.(*CandidateNode)
|
|
err = encoder.Encode(writer, node)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
writer.Flush()
|
|
|
|
test.AssertResult(t, shellvars, strings.TrimSuffix(output.String(), "\n"))
|
|
}
|
|
|
|
func TestShellVariablesEncoderNonquoting(t *testing.T) {
|
|
assertEncodesTo(t, "a: alice", "a=alice")
|
|
}
|
|
|
|
func TestShellVariablesEncoderQuoting(t *testing.T) {
|
|
assertEncodesTo(t, "a: Lewis Carroll", "a='Lewis Carroll'")
|
|
}
|
|
|
|
func TestShellVariablesEncoderQuotesQuoting(t *testing.T) {
|
|
assertEncodesTo(t, "a: Lewis Carroll's Alice", "a='Lewis Carroll'\"'\"'s Alice'")
|
|
}
|
|
|
|
func TestShellVariablesEncoderStripComments(t *testing.T) {
|
|
assertEncodesTo(t, "a: Alice # comment", "a=Alice")
|
|
}
|
|
|
|
func TestShellVariablesEncoderMap(t *testing.T) {
|
|
assertEncodesTo(t, "a:\n b: Lewis\n c: Carroll", "a_b=Lewis\na_c=Carroll")
|
|
}
|
|
|
|
func TestShellVariablesEncoderArray_Unwrapped(t *testing.T) {
|
|
assertEncodesTo(t, "a: [{n: Alice}, {n: Bob}]", "a_0_n=Alice\na_1_n=Bob")
|
|
}
|
|
|
|
func TestShellVariablesEncoderKeyNonPrintable(t *testing.T) {
|
|
assertEncodesTo(t, `"be\all": ring!`, "bell='ring!'")
|
|
}
|
|
|
|
func TestShellVariablesEncoderKeyPrintableNonAlphaNumeric(t *testing.T) {
|
|
assertEncodesTo(t, `"b-e l=l": ring!`, "b_e_l_l='ring!'")
|
|
}
|
|
|
|
func TestShellVariablesEncoderKeyPrintableNonAscii(t *testing.T) {
|
|
assertEncodesTo(t, `"b\u00e9ll": ring!`, "bell='ring!'")
|
|
}
|
|
|
|
func TestShellVariablesEncoderRootKeyStartingWithDigit(t *testing.T) {
|
|
assertEncodesTo(t, "1a: onea", "_1a=onea")
|
|
}
|
|
|
|
func TestShellVariablesEncoderRootKeyStartingWithUnderscore(t *testing.T) {
|
|
assertEncodesTo(t, "_key: value", "_key=value")
|
|
}
|
|
|
|
func TestShellVariablesEncoderChildStartingWithUnderscore(t *testing.T) {
|
|
assertEncodesTo(t, "root:\n _child: value", "root__child=value")
|
|
}
|
|
|
|
func TestShellVariablesEncoderEmptyValue(t *testing.T) {
|
|
assertEncodesTo(t, "empty:", "empty=")
|
|
}
|
|
|
|
func TestShellVariablesEncoderEmptyArray(t *testing.T) {
|
|
assertEncodesTo(t, "empty: []", "")
|
|
}
|
|
|
|
func TestShellVariablesEncoderEmptyMap(t *testing.T) {
|
|
assertEncodesTo(t, "empty: {}", "")
|
|
}
|
|
|
|
func TestShellVariablesEncoderScalarNode(t *testing.T) {
|
|
assertEncodesTo(t, "some string", "value='some string'")
|
|
}
|
|
|
|
func assertEncodesToWithSeparator(t *testing.T, yaml string, shellvars string, separator string) {
|
|
var output bytes.Buffer
|
|
writer := bufio.NewWriter(&output)
|
|
|
|
// Save the original separator
|
|
originalSeparator := ConfiguredShellVariablesPreferences.KeySeparator
|
|
defer func() {
|
|
ConfiguredShellVariablesPreferences.KeySeparator = originalSeparator
|
|
}()
|
|
|
|
// Set the custom separator
|
|
ConfiguredShellVariablesPreferences.KeySeparator = separator
|
|
|
|
var encoder = NewShellVariablesEncoder()
|
|
inputs, err := readDocuments(strings.NewReader(yaml), "test.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
node := inputs.Front().Value.(*CandidateNode)
|
|
err = encoder.Encode(writer, node)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
writer.Flush()
|
|
|
|
test.AssertResult(t, shellvars, strings.TrimSuffix(output.String(), "\n"))
|
|
}
|
|
|
|
func TestShellVariablesEncoderCustomSeparator(t *testing.T) {
|
|
assertEncodesToWithSeparator(t, "a:\n b: Lewis\n c: Carroll", "a__b=Lewis\na__c=Carroll", "__")
|
|
}
|
|
|
|
func TestShellVariablesEncoderCustomSeparatorNested(t *testing.T) {
|
|
assertEncodesToWithSeparator(t, "my_app:\n db_config:\n host: localhost", "my_app__db_config__host=localhost", "__")
|
|
}
|
|
|
|
func TestShellVariablesEncoderCustomSeparatorArray(t *testing.T) {
|
|
assertEncodesToWithSeparator(t, "a: [{n: Alice}, {n: Bob}]", "a__0__n=Alice\na__1__n=Bob", "__")
|
|
}
|
|
|
|
func TestShellVariablesEncoderCustomSeparatorSingleChar(t *testing.T) {
|
|
assertEncodesToWithSeparator(t, "a:\n b: value", "aXb=value", "X")
|
|
}
|