Skip to content

Commit 4af98d1

Browse files
committed
Only allow editing of non-running instances
Signed-off-by: ye.sijun <[email protected]>
1 parent 47fb01d commit 4af98d1

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

cmd/limactl/edit.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"os"
88
"path/filepath"
99

10+
"github.com/AlecAivazis/survey/v2"
1011
"github.com/lima-vm/lima/pkg/limayaml"
12+
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
13+
"github.com/lima-vm/lima/pkg/start"
1114
"github.com/lima-vm/lima/pkg/store"
1215
"github.com/lima-vm/lima/pkg/store/filenames"
1316
"github.com/sirupsen/logrus"
@@ -40,12 +43,16 @@ func editAction(cmd *cobra.Command, args []string) error {
4043
return err
4144
}
4245

46+
if inst.Status == store.StatusRunning {
47+
return errors.New("Cannot edit a running instance")
48+
}
49+
4350
filePath := filepath.Join(inst.Dir, filenames.LimaYAML)
4451
yContent, err := os.ReadFile(filePath)
4552
if err != nil {
4653
return err
4754
}
48-
hdr := fmt.Sprintf("# Please edit the folling configuration for Lima instance %q\n", instName)
55+
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
4956
hdr += "# and an empty file will abort the edit.\n"
5057
hdr += "\n"
5158
yBytes, err := openEditor(cmd, instName, yContent, hdr)
@@ -75,8 +82,33 @@ func editAction(cmd *cobra.Command, args []string) error {
7582
if err := os.WriteFile(filePath, yBytes, 0644); err != nil {
7683
return err
7784
}
78-
logrus.Infof("Instance %q edited, you need restart instance to apply it", instName)
79-
return nil
85+
logrus.Infof("Instance %q configuration edited", instName)
86+
87+
startNow, err := askWhetherToStart()
88+
if err != nil {
89+
return err
90+
}
91+
if !startNow {
92+
return nil
93+
}
94+
ctx := cmd.Context()
95+
err = networks.Reconcile(ctx, inst.Name)
96+
if err != nil {
97+
return err
98+
}
99+
return start.Start(ctx, inst)
100+
}
101+
102+
func askWhetherToStart() (bool, error) {
103+
ans := true
104+
prompt := &survey.Confirm{
105+
Message: "Do you want to start the instance now? ",
106+
Default: true,
107+
}
108+
if err := survey.AskOne(prompt, &ans); err != nil {
109+
return false, err
110+
}
111+
return ans, nil
80112
}
81113

82114
func editBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

0 commit comments

Comments
 (0)