Skip to content

Commit

Permalink
Multiline value fix - multi line strings no longer printed as a yaml …
Browse files Browse the repository at this point in the history
…block

Although printing the string as a yaml block can be argued to be technically correct, in practical terms it's more useful to just print out the multiline string as is.
  • Loading branch information
Mike Farah committed Mar 27, 2018
1 parent 8f15dba commit ee8ffd4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/multiline-text.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test: |
abcdefg
hijklmno
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yq
version: 1.14.0
version: 1.14.1
summary: A lightweight and portable command-line YAML processor
description: |
The aim of the project is to be the jq or sed of yaml files.
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
GitDescribe string

// Version is main version number that is being run at the moment.
Version = "1.14.0"
Version = "1.14.1"

// 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
Expand Down
11 changes: 11 additions & 0 deletions yq.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,21 @@ func toString(context interface{}) (string, error) {
}

func yamlToString(context interface{}) (string, error) {
switch context.(type) {
case string:
return context.(string), nil
default:
return marshalContext(context)
}
}

func marshalContext(context interface{}) (string, error) {
out, err := yaml.Marshal(context)

if err != nil {
return "", fmt.Errorf("error printing yaml: %v", err)
}

outStr := string(out)
// trim the trailing new line as it's easier for a script to add
// it in if required than to remove it
Expand Down
8 changes: 8 additions & 0 deletions yq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ application: MyApp`,
formattedResult)
}

func TestMultilineString(t *testing.T) {
testString := `
abcd
efg`
formattedResult, _ := yamlToString(testString)
assertResult(t, testString, formattedResult)
}

func TestNewYaml(t *testing.T) {
result, _ := newYaml([]string{"b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
Expand Down

0 comments on commit ee8ffd4

Please sign in to comment.