Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/lima-vm/lima/pkg/start"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/lima-vm/lima/pkg/yqutil"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -26,6 +28,9 @@ func newEditCommand() *cobra.Command {
RunE: editAction,
ValidArgsFunction: editBashComplete,
}
// TODO: "survey" does not support using cygwin terminal on windows yet
editCommand.Flags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "enable TUI interactions such as opening an editor, defaults to true when stdout is a terminal")
editCommand.Flags().String("set", "", "modify the template inplace, using yq syntax")
return editCommand
}

Expand Down Expand Up @@ -53,14 +58,31 @@ func editAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
hdr += "# and an empty file will abort the edit.\n"
hdr += "\n"
hdr += editutil.GenerateEditorWarningHeader()
yBytes, err := editutil.OpenEditor(instName, yContent, hdr)
tty, err := cmd.Flags().GetBool("tty")
if err != nil {
return err
}
yq, err := cmd.Flags().GetString("set")
if err != nil {
return err
}
var yBytes []byte
if yq != "" {
logrus.Warn("`--set` is experimental")
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
if err != nil {
return err
}
} else if tty {
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
hdr += "# and an empty file will abort the edit.\n"
hdr += "\n"
hdr += editutil.GenerateEditorWarningHeader()
yBytes, err = editutil.OpenEditor(instName, yContent, hdr)
if err != nil {
return err
}
}
if len(yBytes) == 0 {
logrus.Info("Aborting, as requested by saving the file with empty content")
return nil
Expand All @@ -86,6 +108,10 @@ func editAction(cmd *cobra.Command, args []string) error {
}
logrus.Infof("Instance %q configuration edited", instName)

if !tty {
// use "start" to start it
return nil
}
startNow, err := askWhetherToStart()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions docs/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ The following features are experimental and subject to change:
The following flags are experimental and subject to change:

- `start --set`, yq expression
- `edit --set`, yq expression