-
Notifications
You must be signed in to change notification settings - Fork 689
yqutil: fix to preserve line breaks #2625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jandubois
merged 5 commits into
lima-vm:master
from
norio-nomura:yqutil-fix-preserve-line-breaks
Sep 18, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd3923d
yqutil: fix to preserve line breaks.
norio-nomura cc577be
examples/*.yaml: format templates by using `limactl edit --set 'del(.…
norio-nomura beeeccf
test.yaml: verify templates match `limactl edit` format
norio-nomura bf44aa5
default.yaml: apply review
norio-nomura f2d6a55
yqutil: stop adapting internal codes from `yamlfmt`
norio-nomura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package yqutil | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
|
@@ -15,13 +16,17 @@ import ( | |
// EvaluateExpression evaluates the yq expression, and returns the modified yaml. | ||
func EvaluateExpression(expression string, content []byte) ([]byte, error) { | ||
logrus.Debugf("Evaluating yq expression: %q", expression) | ||
contentModified, err := replaceLineBreaksWithMagicString(content) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tmpYAMLFile, err := os.CreateTemp("", "lima-yq-*.yaml") | ||
if err != nil { | ||
return nil, err | ||
} | ||
tmpYAMLPath := tmpYAMLFile.Name() | ||
defer os.RemoveAll(tmpYAMLPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is not part of this PR, but why is this calling @afbjorklund Do you remember why you choose |
||
_, err = tmpYAMLFile.Write(content) | ||
_, err = tmpYAMLFile.Write(contentModified) | ||
if err != nil { | ||
tmpYAMLFile.Close() | ||
return nil, err | ||
|
@@ -94,3 +99,41 @@ func yamlfmt(content []byte) ([]byte, error) { | |
} | ||
return formatter.Format(content) | ||
} | ||
|
||
const yamlfmtLineBreakPlaceholder = "#magic___^_^___line" | ||
norio-nomura marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type paddinger struct { | ||
strings.Builder | ||
} | ||
|
||
func (p *paddinger) adjust(txt string) { | ||
var indentSize int | ||
for i := 0; i < len(txt) && txt[i] == ' '; i++ { // yaml only allows space to indent. | ||
indentSize++ | ||
} | ||
// Grows if the given size is larger than us and always return the max padding. | ||
for diff := indentSize - p.Len(); diff > 0; diff-- { | ||
p.WriteByte(' ') | ||
} | ||
} | ||
|
||
func replaceLineBreaksWithMagicString(content []byte) ([]byte, error) { | ||
// hotfix: yq does not support line breaks in the middle of a string. | ||
var buf bytes.Buffer | ||
reader := bytes.NewReader(content) | ||
scanner := bufio.NewScanner(reader) | ||
var padding paddinger | ||
for scanner.Scan() { | ||
txt := scanner.Text() | ||
padding.adjust(txt) | ||
if strings.TrimSpace(txt) == "" { // line break or empty space line. | ||
buf.WriteString(padding.String()) // prepend some padding incase literal multiline strings. | ||
buf.WriteString(yamlfmtLineBreakPlaceholder) | ||
buf.WriteString("\n") | ||
} else { | ||
buf.WriteString(txt) | ||
buf.WriteString("\n") | ||
} | ||
} | ||
return buf.Bytes(), scanner.Err() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ memory: null | |
expected := ` | ||
# CPUs | ||
cpus: 2 | ||
|
||
# Memory size | ||
memory: 2GiB | ||
` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.