Skip to content

Commit 0822d08

Browse files
committed
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]>
1 parent 901e3fe commit 0822d08

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

cmd/limactl/edit.go

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/lima-vm/lima/cmd/limactl/editflags"
11+
"github.com/lima-vm/lima/cmd/limactl/guessarg"
1112
"github.com/lima-vm/lima/pkg/editutil"
1213
"github.com/lima-vm/lima/pkg/instance"
1314
"github.com/lima-vm/lima/pkg/limayaml"
@@ -34,24 +35,43 @@ func newEditCommand() *cobra.Command {
3435
}
3536

3637
func editAction(cmd *cobra.Command, args []string) error {
37-
instName := DefaultInstanceName
38+
var arg string
3839
if len(args) > 0 {
39-
instName = args[0]
40+
arg = args[0]
4041
}
4142

42-
inst, err := store.Inspect(instName)
43-
if err != nil {
44-
if errors.Is(err, os.ErrNotExist) {
45-
return fmt.Errorf("instance %q not found", instName)
43+
var filePath string
44+
var err error
45+
var inst *store.Instance
46+
switch {
47+
case guessarg.SeemsYAMLPath(arg):
48+
// absolute path is required for `limayaml.Validate`
49+
filePath, err = filepath.Abs(arg)
50+
if err != nil {
51+
return err
52+
}
53+
default:
54+
var instName string
55+
if arg != "" {
56+
instName = arg
57+
} else {
58+
instName = DefaultInstanceName
4659
}
47-
return err
48-
}
4960

50-
if inst.Status == store.StatusRunning {
51-
return errors.New("cannot edit a running instance")
61+
inst, err = store.Inspect(instName)
62+
if err != nil {
63+
if errors.Is(err, os.ErrNotExist) {
64+
return fmt.Errorf("instance %q not found", instName)
65+
}
66+
return err
67+
}
68+
69+
if inst.Status == store.StatusRunning {
70+
return errors.New("cannot edit a running instance")
71+
}
72+
filePath = filepath.Join(inst.Dir, filenames.LimaYAML)
5273
}
5374

54-
filePath := filepath.Join(inst.Dir, filenames.LimaYAML)
5575
yContent, err := os.ReadFile(filePath)
5676
if err != nil {
5777
return err
@@ -73,7 +93,12 @@ func editAction(cmd *cobra.Command, args []string) error {
7393
return err
7494
}
7595
} else if tty {
76-
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
96+
var hdr string
97+
if inst != nil {
98+
hdr = fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", inst.Name)
99+
} else {
100+
hdr = fmt.Sprintf("# Please edit the following configuration %q\n", filePath)
101+
}
77102
hdr += "# and an empty file will abort the edit.\n"
78103
hdr += "\n"
79104
hdr += editutil.GenerateEditorWarningHeader()
@@ -105,12 +130,18 @@ func editAction(cmd *cobra.Command, args []string) error {
105130
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
106131
return err
107132
}
108-
logrus.Infof("Instance %q configuration edited", instName)
133+
if inst != nil {
134+
logrus.Infof("Instance %q configuration edited", inst.Name)
135+
}
109136

110137
if !tty {
111138
// use "start" to start it
112139
return nil
113140
}
141+
if inst == nil {
142+
// edited a limayaml file directly
143+
return nil
144+
}
114145
startNow, err := askWhetherToStart()
115146
if err != nil {
116147
return err

0 commit comments

Comments
 (0)