Skip to content

Commit 95d1863

Browse files
authored
Merge pull request #2625 from norio-nomura/yqutil-fix-preserve-line-breaks
yqutil: fix to preserve line breaks
2 parents 4c687f7 + f2d6a55 commit 95d1863

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ jobs:
9090
run: make
9191
- name: Install
9292
run: sudo make install
93+
- name: Verify templates match `limactl edit` format
94+
run: |
95+
find examples -name '*.yaml' -exec limactl edit --set 'del(.nothing)' {} \;
96+
git diff-index --exit-code HEAD
9397
- name: Uninstall
9498
run: sudo make uninstall
9599

examples/buildkit.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# $ export BUILDKIT_HOST=$(limactl list buildkit --format 'unix://{{.Dir}}/sock/buildkitd.sock')
66
# $ buildctl debug workers
77
message: |
8-
To run `buildkit` on the host (assumes buildctl is installed), run the following commands:
9-
-------
10-
export BUILDKIT_HOST="unix://{{.Dir}}/sock/buildkitd.sock"
11-
buildctl debug workers
12-
-------
8+
To run `buildkit` on the host (assumes buildctl is installed), run the following commands:
9+
-------
10+
export BUILDKIT_HOST="unix://{{.Dir}}/sock/buildkitd.sock"
11+
buildctl debug workers
12+
-------
1313
images:
1414
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
1515
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240821/ubuntu-24.04-server-cloudimg-amd64.img"

examples/default.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ containerd:
264264
# Setting of instructions is supported like this: "qemu64,+ssse3".
265265
# 🟢 Builtin default: hard-coded arch map with type (see the output of `limactl info | jq .defaultTemplate.cpuType`)
266266
cpuType:
267-
# aarch64: "cortex-a72" # (or "host" when running on aarch64 host)
268-
# armv7l: "cortex-a7" # (or "host" when running on armv7l host)
269-
# riscv64: "rv64" # (or "host" when running on riscv64 host)
270-
# x86_64: "qemu64" # (or "host,-pdpe1gb" when running on x86_64 host)
267+
# aarch64: "cortex-a72" # (or "host" when running on aarch64 host)
268+
# armv7l: "cortex-a7" # (or "host" when running on armv7l host)
269+
# riscv64: "rv64" # (or "host" when running on riscv64 host)
270+
# x86_64: "qemu64" # (or "host,-pdpe1gb" when running on x86_64 host)
271271

272272
rosetta:
273273
# Enable Rosetta for Linux (EXPERIMENTAL; will graduate from experimental in Lima v1.0).
@@ -456,8 +456,8 @@ hostResolver:
456456
# predefined to specify the gateway address to the host.
457457
# 🟢 Builtin default: null
458458
hosts:
459-
# guest.name: 127.1.1.1
460-
# host.name: host.lima.internal
459+
# guest.name: 127.1.1.1
460+
# host.name: host.lima.internal
461461

462462
# If hostResolver.enabled is false, then the following rules apply for configuring dns:
463463
# Explicitly set DNS addresses for qemu user-mode networking. By default qemu picks *one*

pkg/yqutil/yqutil.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/google/yamlfmt"
910
"github.com/google/yamlfmt/formatters/basic"
1011
"github.com/mikefarah/yq/v4/pkg/yqlib"
1112
"github.com/sirupsen/logrus"
@@ -15,13 +16,26 @@ import (
1516
// EvaluateExpression evaluates the yq expression, and returns the modified yaml.
1617
func EvaluateExpression(expression string, content []byte) ([]byte, error) {
1718
logrus.Debugf("Evaluating yq expression: %q", expression)
19+
formatter, err := yamlfmtBasicFormatter()
20+
if err != nil {
21+
return nil, err
22+
}
23+
// `ApplyFeatures()` is being called directly before passing content to `yqlib`.
24+
// This results in `ApplyFeatures()` being called twice with `FeatureApplyBefore`:
25+
// once here and once inside `formatter.Format`.
26+
// Currently, calling `ApplyFeatures()` with `FeatureApplyBefore` twice is not an issue,
27+
// but future changes to `yamlfmt` might cause problems if it is called twice.
28+
contentModified, err := formatter.Features.ApplyFeatures(content, yamlfmt.FeatureApplyBefore)
29+
if err != nil {
30+
return nil, err
31+
}
1832
tmpYAMLFile, err := os.CreateTemp("", "lima-yq-*.yaml")
1933
if err != nil {
2034
return nil, err
2135
}
2236
tmpYAMLPath := tmpYAMLFile.Name()
2337
defer os.RemoveAll(tmpYAMLPath)
24-
_, err = tmpYAMLFile.Write(content)
38+
_, err = tmpYAMLFile.Write(contentModified)
2539
if err != nil {
2640
tmpYAMLFile.Close()
2741
return nil, err
@@ -70,7 +84,7 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
7084
return nil, err
7185
}
7286

73-
return yamlfmt(out.Bytes())
87+
return formatter.Format(out.Bytes())
7488
}
7589

7690
func Join(yqExprs []string) string {
@@ -80,17 +94,23 @@ func Join(yqExprs []string) string {
8094
return strings.Join(yqExprs, " | ")
8195
}
8296

83-
func yamlfmt(content []byte) ([]byte, error) {
97+
func yamlfmtBasicFormatter() (*basic.BasicFormatter, error) {
8498
factory := basic.BasicFormatterFactory{}
8599
config := map[string]interface{}{
86-
"indentless_arrays": true,
87-
"line_ending": "lf", // prefer LF even on Windows
88-
"pad_line_comments": 2,
89-
"retain_line_breaks": true, // does not affect to the output because yq removes empty lines before formatting
100+
"indentless_arrays": true,
101+
"line_ending": "lf", // prefer LF even on Windows
102+
"pad_line_comments": 2,
103+
"retain_line_breaks": true,
104+
"retain_line_breaks_single": false,
90105
}
106+
91107
formatter, err := factory.NewFormatter(config)
92108
if err != nil {
93109
return nil, err
94110
}
95-
return formatter.Format(content)
111+
basicFormatter, ok := formatter.(*basic.BasicFormatter)
112+
if !ok {
113+
return nil, fmt.Errorf("unexpected formatter type: %T", formatter)
114+
}
115+
return basicFormatter, nil
96116
}

pkg/yqutil/yqutil_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ memory: null
1919
expected := `
2020
# CPUs
2121
cpus: 2
22+
2223
# Memory size
2324
memory: 2GiB
2425
`

0 commit comments

Comments
 (0)