-
Notifications
You must be signed in to change notification settings - Fork 617
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
Improve limactl edit
#2593
Improve limactl edit
#2593
Conversation
@@ -58,6 +59,8 @@ require ( | |||
github.com/VividCortex/ewma v1.2.0 // indirect | |||
github.com/a8m/envsubst v1.4.2 // indirect | |||
github.com/alecthomas/participle/v2 v2.1.1 // indirect | |||
github.com/bmatcuk/doublestar/v4 v4.6.0 // indirect | |||
github.com/braydonk/yaml v0.7.0 // indirect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge fan of importing multiple YAML libraries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the fifth YAML library?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, is this the first time a YAML library forked from https://github.com/go-yaml/yaml is being added?
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to express my gratitude to Go for allowing the use of both a forked library and the original library simultaneously without causing issues. 👏🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both of yqlib and yamlfmt brings enough value, to be allowed to bring-their-own-yaml to the party
Tests are failing on Windows because it is adding |
I like this new library, with some configuration it can actually keep the examples/default.yaml more or less as-is: .yamlfmt line_ending: lf
formatter:
indentless_arrays: true
retain_line_breaks: true The only change is some alignment on the comments, and I don't think that is too bad (and yamllint is OK too)
--- a/examples/default.yaml
+++ b/examples/default.yaml
@@ -264,10 +264,10 @@ containerd:
# Setting of instructions is supported like this: "qemu64,+ssse3".
# 🟢 Builtin default: hard-coded arch map with type (see the output of `limactl info | jq .defaultTemplate.cpuType`)
cpuType:
- # aarch64: "cortex-a72" # (or "host" when running on aarch64 host)
- # armv7l: "cortex-a7" # (or "host" when running on armv7l host)
- # riscv64: "rv64" # (or "host" when running on riscv64 host)
- # x86_64: "qemu64" # (or "host,-pdpe1gb" when running on x86_64 host)
+# aarch64: "cortex-a72" # (or "host" when running on aarch64 host)
+# armv7l: "cortex-a7" # (or "host" when running on armv7l host)
+# riscv64: "rv64" # (or "host" when running on riscv64 host)
+# x86_64: "qemu64" # (or "host,-pdpe1gb" when running on x86_64 host)
rosetta:
# Enable Rosetta for Linux (EXPERIMENTAL; will graduate from experimental in Lima v1.0).
@@ -456,8 +456,8 @@ hostResolver:
# predefined to specify the gateway address to the host.
# 🟢 Builtin default: null
hosts:
- # guest.name: 127.1.1.1
- # host.name: host.lima.internal
+ # guest.name: 127.1.1.1
+ # host.name: host.lima.internal
# If hostResolver.enabled is false, then the following rules apply for configuring dns:
# Explicitly set DNS addresses for qemu user-mode networking. By default qemu picks *one*
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @afbjorklund that the new libraries provide sufficient value to justify adding one more YAML library to the project.
I think the fact that limactl edit
can now edit templates in addition to instances should be noted in the command descriptions:
- Use: "edit INSTANCE",
- Short: "Edit an instance of Lima",
+ Use: "edit INSTANCE|FILE.yaml",
+ Short: "Edit an instance of Lima or a template",
Please squash commits! |
Signed-off-by: Norio Nomura <[email protected]> `limactl edit`: support editing lima.yaml file This allows `limactl edit --set "..." <limayaml>` to be used instead of `yq -i eval "..." <limayaml>` in scripts like `hack/inject-cmdline-to-template.sh`. Signed-off-by: Norio Nomura <[email protected]> yqutil: Use `LF` for line endings regardless of the platform. Signed-off-by: Norio Nomura <[email protected]> `limactl edit`: update command description Signed-off-by: Norio Nomura <[email protected]> yqutil_test: fix typo Signed-off-by: Norio Nomura <[email protected]>
6cfba80
to
a10a8bb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM
@AkihiroSuda Do you have any objections to merging this? |
Super-hacky idea: Replace all empty lines with |
That's exactly the idea that |
Oh, it seems that the following hack works well: diff --git forkSrcPrefix/pkg/yqutil/yqutil.go forkDstPrefix/pkg/yqutil/yqutil.go
index 139d9fcbce3ff6c7ca3df38a04a1fb3ef36ccc1b..8787797f40b6e0a8fcaec1f06b203b25e8ba3c06 100644
--- forkSrcPrefix/pkg/yqutil/yqutil.go
+++ forkDstPrefix/pkg/yqutil/yqutil.go
@@ -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)
- _, 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"
+
+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()
+}
diff --git forkSrcPrefix/pkg/yqutil/yqutil_test.go forkDstPrefix/pkg/yqutil/yqutil_test.go
index fcf1260ec24519a847a9305d978144f0bbdbe7cc..287c51c6be2d13bd75a7984712f0d4aad492fb0b 100644
--- forkSrcPrefix/pkg/yqutil/yqutil_test.go
+++ forkDstPrefix/pkg/yqutil/yqutil_test.go
@@ -19,6 +19,7 @@ memory: null
expected := `
# CPUs
cpus: 2
+
# Memory size
memory: 2GiB
` 🤔 (edited: add |
The changes after running diff --git forkSrcPrefix/examples/default.yaml forkDstPrefix/examples/default.yaml
index 5b269ff7e25530c16ce7a9c4fd00251a35850b0d..c46886072da9ff064754da989698e618daa787e8 100644
--- forkSrcPrefix/examples/default.yaml
+++ forkDstPrefix/examples/default.yaml
@@ -43,7 +43,7 @@ cpus: null
# Memory size
# 🟢 Builtin default: min("4GiB", half of host memory)
-memory: null
+memory: 8GiB
# Disk size
# 🟢 Builtin default: "100GiB"
@@ -264,10 +264,10 @@ containerd:
# Setting of instructions is supported like this: "qemu64,+ssse3".
# 🟢 Builtin default: hard-coded arch map with type (see the output of `limactl info | jq .defaultTemplate.cpuType`)
cpuType:
- # aarch64: "cortex-a72" # (or "host" when running on aarch64 host)
- # armv7l: "cortex-a7" # (or "host" when running on armv7l host)
- # riscv64: "rv64" # (or "host" when running on riscv64 host)
- # x86_64: "qemu64" # (or "host,-pdpe1gb" when running on x86_64 host)
+# aarch64: "cortex-a72" # (or "host" when running on aarch64 host)
+# armv7l: "cortex-a7" # (or "host" when running on armv7l host)
+# riscv64: "rv64" # (or "host" when running on riscv64 host)
+# x86_64: "qemu64" # (or "host,-pdpe1gb" when running on x86_64 host)
rosetta:
# Enable Rosetta for Linux (EXPERIMENTAL; will graduate from experimental in Lima v1.0).
@@ -456,8 +456,8 @@ hostResolver:
# predefined to specify the gateway address to the host.
# 🟢 Builtin default: null
hosts:
- # guest.name: 127.1.1.1
- # host.name: host.lima.internal
+ # guest.name: 127.1.1.1
+ # host.name: host.lima.internal
# If hostResolver.enabled is false, then the following rules apply for configuring dns:
# Explicitly set DNS addresses for qemu user-mode networking. By default qemu picks *one* |
If you create a PR with that hack (which I would like to see once the current PR is merged), then I would suggest that we should also fix up the templates so that the comments round-trip correctly (all comments should be in front of the setting they apply to, and not after it). Then we can add a check to CI to make sure they remain round-trippable. Something like find examples -name '*.yaml' -exec limactl edit --set 'del(.nothing)' {} \;
git diff-index --exit-code HEAD |
@AkihiroSuda I'll take the fact that you assigned this PR to the
So I wasn't sure if you still had objections to merging it or not. But I guess you are ok with it now. |
Thanks! 🙏🏻
Before doing that, I want to finalize #2532. |
Yes, ok to merge, sorry for delay |
limactl edit
: support editinglima.yaml
fileThis allows
limactl edit --set "..." <limayaml>
to be used instead ofyq -i eval "..." <limayaml>
in scripts likehack/inject-cmdline-to-template.sh
.The changes made to
examples/ubuntu.yaml
usinglimactl edit --set
inhack/inject-cmdline-to-template.sh
are as follows:The issue with line breaks disappearing cannot be fixed with yamlfmt, so it still remains. However, the sequence is no longer indented, and I personally think it has become usable.
I'm considering making a separate PR to change
hack/inject-cmdline-to-template.sh
fromyq -i eval
tolimactl edit --set
after #2562, which is likely to have overlapping changes, is resolved.