|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/AlecAivazis/survey/v2" |
| 11 | + "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" |
| 14 | + "github.com/lima-vm/lima/pkg/store" |
| 15 | + "github.com/lima-vm/lima/pkg/store/filenames" |
| 16 | + "github.com/sirupsen/logrus" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +func newEditCommand() *cobra.Command { |
| 21 | + var editCommand = &cobra.Command{ |
| 22 | + Use: "edit INSTANCE", |
| 23 | + Short: "Edit an instance of Lima", |
| 24 | + Args: cobra.MaximumNArgs(1), |
| 25 | + RunE: editAction, |
| 26 | + ValidArgsFunction: editBashComplete, |
| 27 | + } |
| 28 | + return editCommand |
| 29 | +} |
| 30 | + |
| 31 | +func editAction(cmd *cobra.Command, args []string) error { |
| 32 | + instName := DefaultInstanceName |
| 33 | + if len(args) > 0 { |
| 34 | + instName = args[0] |
| 35 | + } |
| 36 | + |
| 37 | + inst, err := store.Inspect(instName) |
| 38 | + if err != nil { |
| 39 | + if errors.Is(err, os.ErrNotExist) { |
| 40 | + logrus.Infof("Instance %q not found", instName) |
| 41 | + return nil |
| 42 | + } |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + if inst.Status == store.StatusRunning { |
| 47 | + return errors.New("Cannot edit a running instance") |
| 48 | + } |
| 49 | + |
| 50 | + filePath := filepath.Join(inst.Dir, filenames.LimaYAML) |
| 51 | + yContent, err := os.ReadFile(filePath) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName) |
| 56 | + hdr += "# and an empty file will abort the edit.\n" |
| 57 | + hdr += "\n" |
| 58 | + yBytes, err := openEditor(cmd, instName, yContent, hdr) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if len(yBytes) == 0 { |
| 63 | + logrus.Info("Aborting, as requested by saving the file with empty content") |
| 64 | + return nil |
| 65 | + } |
| 66 | + if bytes.Compare(yBytes, yContent) == 0 { |
| 67 | + logrus.Info("Aborting, no changes made to the instance") |
| 68 | + return nil |
| 69 | + } |
| 70 | + y, err := limayaml.Load(yBytes, filePath) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + if err := limayaml.Validate(*y, true); err != nil { |
| 75 | + rejectedYAML := "lima.REJECTED.yaml" |
| 76 | + if writeErr := os.WriteFile(rejectedYAML, yBytes, 0644); writeErr != nil { |
| 77 | + return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %v: %w", rejectedYAML, writeErr, err) |
| 78 | + } |
| 79 | + // TODO: may need to support editing the rejected YAML |
| 80 | + return fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err) |
| 81 | + } |
| 82 | + if err := os.WriteFile(filePath, yBytes, 0644); err != nil { |
| 83 | + return err |
| 84 | + } |
| 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 |
| 112 | +} |
| 113 | + |
| 114 | +func editBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 115 | + return bashCompleteInstanceNames(cmd) |
| 116 | +} |
0 commit comments