Skip to content

Commit f98028c

Browse files
authored
Unwrap scalars in shell output mode. (#2548)
* feat: Add UnwrapScalar to ShellVariablesPreferences - Add UnwrapScalar boolean field to ShellVariablesPreferences struct. - Initialize UnwrapScalar to false in NewDefaultShellVariablesPreferences. - This preference will control whether shell output should be quoted or raw. * feat: Propagate unwrapScalar to ShellVariablesPreferences - In configureEncoder function, set UnwrapScalar in ConfiguredShellVariablesPreferences. - This ensures the -r flag's state is passed to the shell encoder for raw output control. * feat: Implement conditional quoting in shellVariablesEncoder - Modify doEncode method to check pe.prefs.UnwrapScalar. - If UnwrapScalar is true, output raw node.Value. - Otherwise, use quoteValue for shell-safe quoting. - This enables quote-free output for Kubernetes workflows when -r is used. * test: Add tests for UnwrapScalar in shell encoder - Introduce assertEncodesToUnwrapped helper function. - Add TestShellVariablesEncoderUnwrapScalar to verify quote-free output with -r. - Add TestShellVariablesEncoderDefaultQuoting to confirm default quoting behavior without -r. - Ensure comprehensive testing of conditional quoting logic for shell output. * remove redundant test
1 parent c602937 commit f98028c

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

cmd/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func configureEncoder() (yqlib.Encoder, error) {
207207
yqlib.ConfiguredKYamlPreferences.UnwrapScalar = unwrapScalar
208208
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
209209
yqlib.ConfiguredJSONPreferences.UnwrapScalar = unwrapScalar
210+
yqlib.ConfiguredShellVariablesPreferences.UnwrapScalar = unwrapScalar
210211

211212
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
212213
yqlib.ConfiguredKYamlPreferences.ColorsEnabled = colorsEnabled

pkg/yqlib/encoder_shellvariables.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ func (pe *shellVariablesEncoder) doEncode(w *io.Writer, node *CandidateNode, pat
5757
// let's just pick a fallback key to use if we are encoding a single scalar
5858
nonemptyPath = "value"
5959
}
60-
_, err := io.WriteString(*w, nonemptyPath+"="+quoteValue(node.Value)+"\n")
60+
var valueString string
61+
if pe.prefs.UnwrapScalar {
62+
valueString = node.Value
63+
} else {
64+
valueString = quoteValue(node.Value)
65+
}
66+
_, err := io.WriteString(*w, nonemptyPath+"="+valueString+"\n")
6167
return err
6268
case SequenceNode:
6369
for index, child := range node.Content {

pkg/yqlib/encoder_shellvariables_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,36 @@ func TestShellVariablesEncoderCustomSeparatorArray(t *testing.T) {
135135
func TestShellVariablesEncoderCustomSeparatorSingleChar(t *testing.T) {
136136
assertEncodesToWithSeparator(t, "a:\n b: value", "aXb=value", "X")
137137
}
138+
139+
func assertEncodesToUnwrapped(t *testing.T, yaml string, shellvars string) {
140+
var output bytes.Buffer
141+
writer := bufio.NewWriter(&output)
142+
143+
originalUnwrapScalar := ConfiguredShellVariablesPreferences.UnwrapScalar
144+
defer func() {
145+
ConfiguredShellVariablesPreferences.UnwrapScalar = originalUnwrapScalar
146+
}()
147+
148+
ConfiguredShellVariablesPreferences.UnwrapScalar = true
149+
150+
var encoder = NewShellVariablesEncoder()
151+
inputs, err := readDocuments(strings.NewReader(yaml), "test.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
152+
if err != nil {
153+
panic(err)
154+
}
155+
node := inputs.Front().Value.(*CandidateNode)
156+
err = encoder.Encode(writer, node)
157+
if err != nil {
158+
panic(err)
159+
}
160+
writer.Flush()
161+
162+
test.AssertResult(t, shellvars, strings.TrimSuffix(output.String(), "\n"))
163+
}
164+
165+
func TestShellVariablesEncoderUnwrapScalar(t *testing.T) {
166+
assertEncodesToUnwrapped(t, "a: Lewis Carroll", "a=Lewis Carroll")
167+
assertEncodesToUnwrapped(t, "b: 123", "b=123")
168+
assertEncodesToUnwrapped(t, "c: true", "c=true")
169+
assertEncodesToUnwrapped(t, "d: value with spaces", "d=value with spaces")
170+
}

pkg/yqlib/shellvariables.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package yqlib
22

33
type ShellVariablesPreferences struct {
44
KeySeparator string
5+
UnwrapScalar bool
56
}
67

78
func NewDefaultShellVariablesPreferences() ShellVariablesPreferences {
89
return ShellVariablesPreferences{
910
KeySeparator: "_",
11+
UnwrapScalar: false,
1012
}
1113
}
1214

0 commit comments

Comments
 (0)