-
Notifications
You must be signed in to change notification settings - Fork 608
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
Add builtin yq support to lima start command #1359
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package yqutil | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/mikefarah/yq/v4/pkg/yqlib" | ||
"github.com/sirupsen/logrus" | ||
logging "gopkg.in/op/go-logging.v1" | ||
) | ||
|
||
// EvaluateExpression evaluates the yq expression, and returns the modified yaml. | ||
func EvaluateExpression(expression string, content []byte) ([]byte, error) { | ||
tmpYAMLFile, err := os.CreateTemp("", "lima-yq-*.yaml") | ||
if err != nil { | ||
return nil, err | ||
} | ||
tmpYAMLPath := tmpYAMLFile.Name() | ||
defer os.RemoveAll(tmpYAMLPath) | ||
err = os.WriteFile(tmpYAMLPath, content, 0o600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
memory := logging.NewMemoryBackend(0) | ||
backend := logging.AddModuleLevel(memory) | ||
logging.SetBackend(backend) | ||
yqlib.InitExpressionParser() | ||
|
||
indent := 2 | ||
encoder := yqlib.NewYamlEncoder(indent, false, yqlib.ConfiguredYamlPreferences) | ||
out := new(bytes.Buffer) | ||
printer := yqlib.NewPrinter(encoder, yqlib.NewSinglePrinterWriter(out)) | ||
decoder := yqlib.NewYamlDecoder(yqlib.ConfiguredYamlPreferences) | ||
|
||
streamEvaluator := yqlib.NewStreamEvaluator() | ||
files := []string{tmpYAMLPath} | ||
err = streamEvaluator.EvaluateFiles(expression, files, printer, decoder) | ||
if err != nil { | ||
logger := logrus.StandardLogger() | ||
for node := memory.Head(); node != nil; node = node.Next() { | ||
entry := logrus.NewEntry(logger).WithTime(node.Record.Time) | ||
prefix := fmt.Sprintf("[%s] ", node.Record.Module) | ||
message := prefix + node.Record.Message() | ||
switch node.Record.Level { | ||
case logging.CRITICAL: | ||
entry.Fatal(message) | ||
case logging.ERROR: | ||
entry.Error(message) | ||
case logging.WARNING: | ||
entry.Warn(message) | ||
case logging.NOTICE: | ||
afbjorklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entry.Info(message) | ||
case logging.INFO: | ||
entry.Info(message) | ||
case logging.DEBUG: | ||
entry.Debug(message) | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
return out.Bytes(), nil | ||
|
||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package yqutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestEvaluateExpressionSimple(t *testing.T) { | ||
expression := `.cpus = 2 | .memory = "2GiB"` | ||
afbjorklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
content := ` | ||
# CPUs | ||
cpus: null | ||
|
||
# Memory size | ||
memory: null | ||
` | ||
// Note: yq currently removes empty lines, but not comments | ||
afbjorklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected := ` | ||
# CPUs | ||
cpus: 2 | ||
# Memory size | ||
memory: 2GiB | ||
` | ||
out, err := EvaluateExpression(expression, []byte(content)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, expected, string(out)) | ||
} | ||
|
||
func TestEvaluateExpressionComplex(t *testing.T) { | ||
expression := `.mounts += {"location": "foo", "mountPoint": "bar"}` | ||
content := ` | ||
# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest | ||
# 🟢 Builtin default: null (Mount nothing) | ||
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable | ||
mounts: | ||
- location: "~" | ||
# Configure the mountPoint inside the guest. | ||
# 🟢 Builtin default: value of location | ||
mountPoint: null | ||
` | ||
// Note: yq will use canonical yaml, with indented sequences | ||
afbjorklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Note: yq will not explicitly quote strings, when not needed | ||
expected := ` | ||
# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest | ||
# 🟢 Builtin default: null (Mount nothing) | ||
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable | ||
mounts: | ||
- location: "~" | ||
# Configure the mountPoint inside the guest. | ||
# 🟢 Builtin default: value of location | ||
mountPoint: null | ||
- location: foo | ||
mountPoint: bar | ||
` | ||
out, err := EvaluateExpression(expression, []byte(content)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, expected, string(out)) | ||
} | ||
|
||
func TestEvaluateExpressionError(t *testing.T) { | ||
expression := `arch: aarch64` | ||
_, err := EvaluateExpression(expression, []byte("")) | ||
assert.ErrorContains(t, err, "invalid input text") | ||
} |
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.
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 wonder if we need to include a reference to the
yq
documentation, so people can learn to construct more powerful expressions for e.g. scripting:$ limactl start --set '.env.HOSTPATH=strenv(PATH)' template://alpine
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 expected the same syntax to be available for the
limactl edit
command as well.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 guess I should have specified: I would expect
limactl edit --set ...
to just edit the config, but not to invoke an editor.It would just be a mechanism to modify the config programmatically without starting the instance.
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.
We can use something like
--eval
, since that is the name of theyq
command being invoked ?I agree that
--set
is somewhat misleading, and that--yq
might be a bit cryptic for lima users...Adding it to
edit
should be straight-forward.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.
👍
--eval
sounds much more misleadingThere 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.
limactl edit --set
can be another PR if complicatedThere 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.
The error handling can be a bit tricky to do, so I'd like that.