/
edit.go
136 lines (126 loc) · 3.49 KB
/
edit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/lima-vm/lima/cmd/limactl/editflags"
"github.com/lima-vm/lima/pkg/editutil"
"github.com/lima-vm/lima/pkg/limayaml"
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
"github.com/lima-vm/lima/pkg/start"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/lima-vm/lima/pkg/uiutil"
"github.com/lima-vm/lima/pkg/yqutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func newEditCommand() *cobra.Command {
editCommand := &cobra.Command{
Use: "edit INSTANCE",
Short: "Edit an instance of Lima",
Args: WrapArgsError(cobra.MaximumNArgs(1)),
RunE: editAction,
ValidArgsFunction: editBashComplete,
GroupID: basicCommand,
}
editflags.RegisterEdit(editCommand)
return editCommand
}
func editAction(cmd *cobra.Command, args []string) error {
instName := DefaultInstanceName
if len(args) > 0 {
instName = args[0]
}
inst, err := store.Inspect(instName)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("Instance %q not found", instName)
}
return err
}
if inst.Status == store.StatusRunning {
return errors.New("Cannot edit a running instance")
}
filePath := filepath.Join(inst.Dir, filenames.LimaYAML)
yContent, err := os.ReadFile(filePath)
if err != nil {
return err
}
flags := cmd.Flags()
tty, err := flags.GetBool("tty")
if err != nil {
return err
}
yqExprs, err := editflags.YQExpressions(flags, false)
if err != nil {
return err
}
var yBytes []byte
if len(yqExprs) > 0 {
yq := yqutil.Join(yqExprs)
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
if err != nil {
return err
}
} else if tty {
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
hdr += "# and an empty file will abort the edit.\n"
hdr += "\n"
hdr += editutil.GenerateEditorWarningHeader()
yBytes, err = editutil.OpenEditor(yContent, hdr)
if err != nil {
return err
}
}
if len(yBytes) == 0 {
logrus.Info("Aborting, as requested by saving the file with empty content")
return nil
}
if bytes.Equal(yBytes, yContent) {
logrus.Info("Aborting, no changes made to the instance")
return nil
}
y, err := limayaml.Load(yBytes, filePath)
if err != nil {
return err
}
if err := limayaml.Validate(*y, true); err != nil {
rejectedYAML := "lima.REJECTED.yaml"
if writeErr := os.WriteFile(rejectedYAML, yBytes, 0o644); writeErr != nil {
return fmt.Errorf("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w", rejectedYAML, writeErr, err)
}
// TODO: may need to support editing the rejected YAML
return fmt.Errorf("the YAML is invalid, saved the buffer as %q: %w", rejectedYAML, err)
}
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
return err
}
logrus.Infof("Instance %q configuration edited", instName)
if !tty {
// use "start" to start it
return nil
}
startNow, err := askWhetherToStart()
if err != nil {
return err
}
if !startNow {
return nil
}
ctx := cmd.Context()
err = networks.Reconcile(ctx, inst.Name)
if err != nil {
return err
}
return start.Start(ctx, inst, false)
}
func askWhetherToStart() (bool, error) {
message := "Do you want to start the instance now? "
return uiutil.Confirm(message, true)
}
func editBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return bashCompleteInstanceNames(cmd)
}