| 1 | |
| 2 | |
| 3 | |
| 4 | package editutil |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | |
| 15 | "github.com/sirupsen/logrus" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/editutil/editorcmd" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatype/dirnames" |
| 19 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 20 | ) |
| 21 | |
| 22 | func fileWarning(filename string) string { |
| 23 | b, err := os.ReadFile(filename) |
| 24 | if err != nil || len(b) == 0 { |
| 25 | return "" |
| 26 | } |
| 27 | var sb strings.Builder |
| 28 | sb.WriteString("# WARNING: " + filename + " includes the following settings,\n") |
| 29 | sb.WriteString("# which are applied before applying this YAML:\n") |
| 30 | sb.WriteString("# -----------\n") |
| 31 | for line := range strings.SplitSeq(strings.TrimSuffix(string(b), "\n"), "\n") { |
| 32 | sb.WriteByte('#') |
| 33 | if line != "" { |
| 34 | sb.WriteString(" " + line) |
| 35 | } |
| 36 | sb.WriteByte('\n') |
| 37 | } |
| 38 | sb.WriteString("# -----------\n\n") |
| 39 | return sb.String() |
| 40 | } |
| 41 | |
| 42 | |
| 43 | func GenerateEditorWarningHeader() string { |
| 44 | var s string |
| 45 | configDir, err := dirnames.LimaConfigDir() |
| 46 | if err != nil { |
| 47 | s += "# WARNING: failed to load the config dir\n" |
| 48 | s += "\n" |
| 49 | return s |
| 50 | } |
| 51 | |
| 52 | s += fileWarning(filepath.Join(configDir, filenames.Default)) |
| 53 | s += fileWarning(filepath.Join(configDir, filenames.Override)) |
| 54 | return s |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | func OpenEditor(ctx context.Context, content []byte, hdr string) ([]byte, error) { |
| 61 | editor := editorcmd.Detect() |
| 62 | if editor == "" { |
| 63 | return nil, errors.New("could not detect a text editor binary, try setting $EDITOR") |
| 64 | } |
| 65 | tmpYAMLFile, err := os.CreateTemp("", "lima-editor-") |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | tmpYAMLPath := tmpYAMLFile.Name() |
| 70 | defer os.RemoveAll(tmpYAMLPath) |
| 71 | if _, err := tmpYAMLFile.Write(append([]byte(hdr), content...)); err != nil { |
| 72 | tmpYAMLFile.Close() |
| 73 | return nil, err |
| 74 | } |
| 75 | if err := tmpYAMLFile.Close(); err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | |
| 79 | editorCmd := exec.CommandContext(ctx, editor, tmpYAMLPath) |
| 80 | editorCmd.Env = os.Environ() |
| 81 | editorCmd.Stdin = os.Stdin |
| 82 | editorCmd.Stdout = os.Stdout |
| 83 | editorCmd.Stderr = os.Stderr |
| 84 | logrus.Debugf("opening editor %#q for a file %#q", editor, tmpYAMLPath) |
| 85 | if err := editorCmd.Run(); err != nil { |
| 86 | return nil, fmt.Errorf("could not execute editor %#q for a file %#q: %w", editor, tmpYAMLPath, err) |
| 87 | } |
| 88 | b, err := os.ReadFile(tmpYAMLPath) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | modifiedInclHdr := string(b) |
| 93 | modifiedExclHdr := strings.TrimPrefix(modifiedInclHdr, hdr) |
| 94 | if strings.TrimSpace(modifiedExclHdr) == "" { |
| 95 | return nil, nil |
| 96 | } |
| 97 | return []byte(modifiedExclHdr), nil |
| 98 | } |