Skip to content

Commit 800ae74

Browse files
committed
restart: accept editflags to modify configuration before restarting
Closes #4249
1 parent 2d4314e commit 800ae74

1 file changed

Lines changed: 67 additions & 3 deletions

File tree

cmd/limactl/restart.go

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44
package main
55

66
import (
7+
"bytes"
8+
"fmt"
9+
"os"
10+
"path/filepath"
11+
12+
"github.com/sirupsen/logrus"
713
"github.com/spf13/cobra"
814

15+
"github.com/lima-vm/lima/v2/cmd/limactl/editflags"
16+
"github.com/lima-vm/lima/v2/pkg/driverutil"
917
"github.com/lima-vm/lima/v2/pkg/instance"
18+
"github.com/lima-vm/lima/v2/pkg/limatype"
19+
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
20+
"github.com/lima-vm/lima/v2/pkg/limayaml"
1021
"github.com/lima-vm/lima/v2/pkg/store"
22+
"github.com/lima-vm/lima/v2/pkg/yqutil"
1123
)
1224

1325
func newRestartCommand() *cobra.Command {
@@ -22,6 +34,7 @@ func newRestartCommand() *cobra.Command {
2234

2335
restartCmd.Flags().BoolP("force", "f", false, "Force stop and restart the instance")
2436
restartCmd.Flags().Bool("progress", false, "Show provision script progress by tailing cloud-init logs")
37+
editflags.RegisterEdit(restartCmd, "")
2538
return restartCmd
2639
}
2740

@@ -37,19 +50,70 @@ func restartAction(cmd *cobra.Command, args []string) error {
3750
return err
3851
}
3952

40-
force, err := cmd.Flags().GetBool("force")
53+
flags := cmd.Flags()
54+
force, err := flags.GetBool("force")
55+
if err != nil {
56+
return err
57+
}
58+
progress, err := flags.GetBool("progress")
59+
if err != nil {
60+
return err
61+
}
62+
63+
filePath := filepath.Join(inst.Dir, filenames.LimaYAML)
64+
yContent, err := os.ReadFile(filePath)
4165
if err != nil {
4266
return err
4367
}
44-
progress, err := cmd.Flags().GetBool("progress")
68+
69+
var params map[string]string
70+
if flags.Changed("param") {
71+
var y limatype.LimaYAML
72+
if err := limayaml.Unmarshal(yContent, &y, filePath); err != nil {
73+
return err
74+
}
75+
params = y.Param
76+
}
77+
78+
yqExprs, err := editflags.YQExpressions(flags, false, params)
4579
if err != nil {
4680
return err
4781
}
4882

83+
if len(yqExprs) > 0 {
84+
yq := yqutil.Join(yqExprs)
85+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
86+
if err != nil {
87+
return err
88+
}
89+
if !bytes.Equal(yBytes, yContent) {
90+
y, err := limayaml.LoadWithWarnings(ctx, yBytes, filePath)
91+
if err != nil {
92+
return err
93+
}
94+
if err := driverutil.ResolveVMType(ctx, y, filePath); err != nil {
95+
return fmt.Errorf("failed to resolve vm for %#q: %w", filePath, err)
96+
}
97+
if err := limayaml.Validate(y, true); err != nil {
98+
return saveRejectedYAML(yBytes, err)
99+
}
100+
if err := limayaml.ValidateAgainstLatestConfig(ctx, yBytes, yContent); err != nil {
101+
return saveRejectedYAML(yBytes, err)
102+
}
103+
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
104+
return err
105+
}
106+
logrus.Infof("Instance %#q configuration edited", inst.Name)
107+
inst, err = store.Inspect(ctx, instName)
108+
if err != nil {
109+
return err
110+
}
111+
}
112+
}
113+
49114
if force {
50115
return instance.RestartForcibly(ctx, inst, progress)
51116
}
52-
53117
return instance.Restart(ctx, inst, progress)
54118
}
55119

0 commit comments

Comments
 (0)