Skip to content

Commit cd9db12

Browse files
committed
limactl edit: support editing a running instance
Previously `limactl edit` returned an error when the instance was running. - Interactive (tty, no --set): prompt the user to stop the instance first; abort if declined. After editing and validation, prompt to restart as usual. - Non-interactive (--set): validate the new config first so the instance stays running if validation fails. Stop, apply, and restart automatically only after validation succeeds. Fixes: #3346 Signed-off-by: Ramadhan Gerry Akbar <ramadhan.gerry@gmail.com>
1 parent 2d4314e commit cd9db12

1 file changed

Lines changed: 47 additions & 6 deletions

File tree

cmd/limactl/edit.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ func editAction(cmd *cobra.Command, args []string) error {
5656
if arg == "" {
5757
arg = DefaultInstanceName
5858
}
59+
flags := cmd.Flags()
60+
tty, err := flags.GetBool("tty")
61+
if err != nil {
62+
return err
63+
}
64+
65+
wasRunning := false
5966
if err := dirnames.ValidateInstName(arg); err == nil {
6067
inst, err = store.Inspect(ctx, arg)
6168
if err != nil {
@@ -65,7 +72,24 @@ func editAction(cmd *cobra.Command, args []string) error {
6572
return err
6673
}
6774
if inst.Status == limatype.StatusRunning {
68-
return errors.New("cannot edit a running instance")
75+
wasRunning = true
76+
if tty {
77+
stop, err := askWhetherToStop(cmd, inst.Name)
78+
if err != nil {
79+
return err
80+
}
81+
if !stop {
82+
return nil
83+
}
84+
logrus.Infof("Stopping %q for editing", inst.Name)
85+
if err := instance.StopGracefully(ctx, inst, false); err != nil {
86+
return err
87+
}
88+
} else if flags.Changed("set") {
89+
// non-interactive: validate first, stop after (below)
90+
} else {
91+
return errors.New("cannot edit a running instance")
92+
}
6993
}
7094
filePath = filepath.Join(inst.Dir, filenames.LimaYAML)
7195
} else {
@@ -80,11 +104,6 @@ func editAction(cmd *cobra.Command, args []string) error {
80104
if err != nil {
81105
return err
82106
}
83-
flags := cmd.Flags()
84-
tty, err := flags.GetBool("tty")
85-
if err != nil {
86-
return err
87-
}
88107
var params map[string]string
89108
if flags.Changed("param") {
90109
var y limatype.LimaYAML
@@ -142,6 +161,17 @@ func editAction(cmd *cobra.Command, args []string) error {
142161
return saveRejectedYAML(yBytes, err)
143162
}
144163

164+
if wasRunning && flags.Changed("set") {
165+
logrus.Infof("Stopping %q to apply changes", inst.Name)
166+
inst, err = store.Inspect(ctx, inst.Name)
167+
if err != nil {
168+
return err
169+
}
170+
if err := instance.StopGracefully(ctx, inst, false); err != nil {
171+
return err
172+
}
173+
}
174+
145175
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
146176
return err
147177
}
@@ -165,6 +195,8 @@ func editAction(cmd *cobra.Command, args []string) error {
165195
if err != nil {
166196
return err
167197
}
198+
} else if wasRunning && flags.Changed("set") && !flags.Changed("start") {
199+
start = true
168200
}
169201
if !start {
170202
return nil
@@ -188,6 +220,15 @@ func editAction(cmd *cobra.Command, args []string) error {
188220
return instance.Start(ctx, inst, false, false)
189221
}
190222

223+
func askWhetherToStop(cmd *cobra.Command, name string) (bool, error) {
224+
isTTY := uiutil.InputIsTTY(cmd.InOrStdin())
225+
if isTTY {
226+
message := fmt.Sprintf("Instance %q is running. Stop it to proceed with editing? ", name)
227+
return uiutil.Confirm(message, true)
228+
}
229+
return false, nil
230+
}
231+
191232
func askWhetherToStart(cmd *cobra.Command) (bool, error) {
192233
isTTY := uiutil.InputIsTTY(cmd.InOrStdin())
193234
if isTTY {

0 commit comments

Comments
 (0)