Skip to content

Commit e12e71c

Browse files
committed
Add builtin yq support to limactl edit command
Also add --tty parameter, similar to the start command, for avoiding user interaction asking whether to start it. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 6b2c880 commit e12e71c

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

cmd/limactl/edit.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/lima-vm/lima/pkg/start"
1515
"github.com/lima-vm/lima/pkg/store"
1616
"github.com/lima-vm/lima/pkg/store/filenames"
17+
"github.com/lima-vm/lima/pkg/yqutil"
18+
"github.com/mattn/go-isatty"
1719
"github.com/sirupsen/logrus"
1820
"github.com/spf13/cobra"
1921
)
@@ -26,6 +28,9 @@ func newEditCommand() *cobra.Command {
2628
RunE: editAction,
2729
ValidArgsFunction: editBashComplete,
2830
}
31+
// TODO: "survey" does not support using cygwin terminal on windows yet
32+
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")
33+
editCommand.Flags().String("set", "", "modify the template inplace, using yq syntax")
2934
return editCommand
3035
}
3136

@@ -53,14 +58,31 @@ func editAction(cmd *cobra.Command, args []string) error {
5358
if err != nil {
5459
return err
5560
}
56-
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
57-
hdr += "# and an empty file will abort the edit.\n"
58-
hdr += "\n"
59-
hdr += editutil.GenerateEditorWarningHeader()
60-
yBytes, err := editutil.OpenEditor(instName, yContent, hdr)
61+
tty, err := cmd.Flags().GetBool("tty")
6162
if err != nil {
6263
return err
6364
}
65+
yq, err := cmd.Flags().GetString("set")
66+
if err != nil {
67+
return err
68+
}
69+
var yBytes []byte
70+
if yq != "" {
71+
logrus.Warn("`--set` is experimental")
72+
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
73+
if err != nil {
74+
return err
75+
}
76+
} else if tty {
77+
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
78+
hdr += "# and an empty file will abort the edit.\n"
79+
hdr += "\n"
80+
hdr += editutil.GenerateEditorWarningHeader()
81+
yBytes, err = editutil.OpenEditor(instName, yContent, hdr)
82+
if err != nil {
83+
return err
84+
}
85+
}
6486
if len(yBytes) == 0 {
6587
logrus.Info("Aborting, as requested by saving the file with empty content")
6688
return nil
@@ -86,6 +108,10 @@ func editAction(cmd *cobra.Command, args []string) error {
86108
}
87109
logrus.Infof("Instance %q configuration edited", instName)
88110

111+
if !tty {
112+
// use "start" to start it
113+
return nil
114+
}
89115
startNow, err := askWhetherToStart()
90116
if err != nil {
91117
return err

docs/experimental.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ The following features are experimental and subject to change:
1010
The following flags are experimental and subject to change:
1111

1212
- `start --set`, yq expression
13+
- `edit --set`, yq expression

0 commit comments

Comments
 (0)