Skip to content

Commit c65ecd8

Browse files
committed
cmd/limactl: split editutil (no code change)
Signed-off-by: Akihiro Suda <[email protected]>
1 parent fb24d47 commit c65ecd8

File tree

3 files changed

+97
-83
lines changed

3 files changed

+97
-83
lines changed

cmd/limactl/edit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/AlecAivazis/survey/v2"
11+
"github.com/lima-vm/lima/pkg/editutil"
1112
"github.com/lima-vm/lima/pkg/limayaml"
1213
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
1314
"github.com/lima-vm/lima/pkg/start"
@@ -55,8 +56,8 @@ func editAction(cmd *cobra.Command, args []string) error {
5556
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
5657
hdr += "# and an empty file will abort the edit.\n"
5758
hdr += "\n"
58-
hdr += generateEditorWarningHeader()
59-
yBytes, err := openEditor(instName, yContent, hdr)
59+
hdr += editutil.GenerateEditorWarningHeader()
60+
yBytes, err := editutil.OpenEditor(instName, yContent, hdr)
6061
if err != nil {
6162
return err
6263
}

cmd/limactl/start.go

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@ import (
88
"net/http"
99
"net/url"
1010
"os"
11-
"os/exec"
1211
"path"
1312
"path/filepath"
1413
"strings"
1514

1615
"github.com/AlecAivazis/survey/v2"
1716
"github.com/containerd/containerd/identifiers"
1817
securejoin "github.com/cyphar/filepath-securejoin"
18+
"github.com/lima-vm/lima/pkg/editutil"
1919
"github.com/lima-vm/lima/pkg/limayaml"
2020
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
2121
"github.com/lima-vm/lima/pkg/osutil"
2222
"github.com/lima-vm/lima/pkg/start"
2323
"github.com/lima-vm/lima/pkg/store"
24-
"github.com/lima-vm/lima/pkg/store/dirnames"
2524
"github.com/lima-vm/lima/pkg/store/filenames"
2625
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
2726
"github.com/mattn/go-isatty"
28-
"github.com/norouter/norouter/cmd/norouter/editorcmd"
2927
"github.com/sirupsen/logrus"
3028
"github.com/spf13/cobra"
3129
)
@@ -288,9 +286,9 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
288286
}
289287
hdr += "# - To cancel starting Lima, just save this file as an empty file.\n"
290288
hdr += "\n"
291-
hdr += generateEditorWarningHeader()
289+
hdr += editutil.GenerateEditorWarningHeader()
292290
var err error
293-
st.yBytes, err = openEditor(st.instName, st.yBytes, hdr)
291+
st.yBytes, err = editutil.OpenEditor(st.instName, st.yBytes, hdr)
294292
if err != nil {
295293
return st, err
296294
}
@@ -367,82 +365,6 @@ func listTemplateYAMLs() ([]TemplateYAML, error) {
367365
}
368366
return res, nil
369367
}
370-
371-
func fileWarning(filename string) string {
372-
b, err := os.ReadFile(filename)
373-
if err != nil || len(b) == 0 {
374-
return ""
375-
}
376-
s := "# WARNING: " + filename + " includes the following settings,\n"
377-
s += "# which are applied before applying this YAML:\n"
378-
s += "# -----------\n"
379-
for _, line := range strings.Split(strings.TrimSuffix(string(b), "\n"), "\n") {
380-
s += "#"
381-
if len(line) > 0 {
382-
s += " " + line
383-
}
384-
s += "\n"
385-
}
386-
s += "# -----------\n"
387-
s += "\n"
388-
return s
389-
}
390-
391-
func generateEditorWarningHeader() string {
392-
var s string
393-
configDir, err := dirnames.LimaConfigDir()
394-
if err != nil {
395-
s += "# WARNING: failed to load the config dir\n"
396-
s += "\n"
397-
return s
398-
}
399-
400-
s += fileWarning(filepath.Join(configDir, filenames.Default))
401-
s += fileWarning(filepath.Join(configDir, filenames.Override))
402-
return s
403-
}
404-
405-
// openEditor opens an editor, and returns the content (not path) of the modified yaml.
406-
//
407-
// openEditor returns nil when the file was saved as an empty file, optionally with whitespaces.
408-
func openEditor(name string, content []byte, hdr string) ([]byte, error) {
409-
editor := editorcmd.Detect()
410-
if editor == "" {
411-
return nil, errors.New("could not detect a text editor binary, try setting $EDITOR")
412-
}
413-
tmpYAMLFile, err := os.CreateTemp("", "lima-editor-")
414-
if err != nil {
415-
return nil, err
416-
}
417-
tmpYAMLPath := tmpYAMLFile.Name()
418-
defer os.RemoveAll(tmpYAMLPath)
419-
if err := os.WriteFile(tmpYAMLPath,
420-
append([]byte(hdr), content...),
421-
0o600); err != nil {
422-
return nil, err
423-
}
424-
425-
editorCmd := exec.Command(editor, tmpYAMLPath)
426-
editorCmd.Env = os.Environ()
427-
editorCmd.Stdin = os.Stdin
428-
editorCmd.Stdout = os.Stdout
429-
editorCmd.Stderr = os.Stderr
430-
logrus.Debugf("opening editor %q for a file %q", editor, tmpYAMLPath)
431-
if err := editorCmd.Run(); err != nil {
432-
return nil, fmt.Errorf("could not execute editor %q for a file %q: %w", editor, tmpYAMLPath, err)
433-
}
434-
b, err := os.ReadFile(tmpYAMLPath)
435-
if err != nil {
436-
return nil, err
437-
}
438-
modifiedInclHdr := string(b)
439-
modifiedExclHdr := strings.TrimPrefix(modifiedInclHdr, hdr)
440-
if strings.TrimSpace(modifiedExclHdr) == "" {
441-
return nil, nil
442-
}
443-
return []byte(modifiedExclHdr), nil
444-
}
445-
446368
func startAction(cmd *cobra.Command, args []string) error {
447369
if listTemplates, err := cmd.Flags().GetBool("list-templates"); err != nil {
448370
return err

pkg/editutil/editutil.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package editutil
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/lima-vm/lima/pkg/store/dirnames"
12+
"github.com/lima-vm/lima/pkg/store/filenames"
13+
"github.com/norouter/norouter/cmd/norouter/editorcmd"
14+
"github.com/sirupsen/logrus"
15+
)
16+
17+
func fileWarning(filename string) string {
18+
b, err := os.ReadFile(filename)
19+
if err != nil || len(b) == 0 {
20+
return ""
21+
}
22+
s := "# WARNING: " + filename + " includes the following settings,\n"
23+
s += "# which are applied before applying this YAML:\n"
24+
s += "# -----------\n"
25+
for _, line := range strings.Split(strings.TrimSuffix(string(b), "\n"), "\n") {
26+
s += "#"
27+
if len(line) > 0 {
28+
s += " " + line
29+
}
30+
s += "\n"
31+
}
32+
s += "# -----------\n"
33+
s += "\n"
34+
return s
35+
}
36+
37+
// GenerateEditorWarningHeader generates the editor warning header.
38+
func GenerateEditorWarningHeader() string {
39+
var s string
40+
configDir, err := dirnames.LimaConfigDir()
41+
if err != nil {
42+
s += "# WARNING: failed to load the config dir\n"
43+
s += "\n"
44+
return s
45+
}
46+
47+
s += fileWarning(filepath.Join(configDir, filenames.Default))
48+
s += fileWarning(filepath.Join(configDir, filenames.Override))
49+
return s
50+
}
51+
52+
// OpenEditor opens an editor, and returns the content (not path) of the modified yaml.
53+
//
54+
// OpenEditor returns nil when the file was saved as an empty file, optionally with whitespaces.
55+
func OpenEditor(name string, content []byte, hdr string) ([]byte, error) {
56+
editor := editorcmd.Detect()
57+
if editor == "" {
58+
return nil, errors.New("could not detect a text editor binary, try setting $EDITOR")
59+
}
60+
tmpYAMLFile, err := os.CreateTemp("", "lima-editor-")
61+
if err != nil {
62+
return nil, err
63+
}
64+
tmpYAMLPath := tmpYAMLFile.Name()
65+
defer os.RemoveAll(tmpYAMLPath)
66+
if err := os.WriteFile(tmpYAMLPath,
67+
append([]byte(hdr), content...),
68+
0o600); err != nil {
69+
return nil, err
70+
}
71+
72+
editorCmd := exec.Command(editor, tmpYAMLPath)
73+
editorCmd.Env = os.Environ()
74+
editorCmd.Stdin = os.Stdin
75+
editorCmd.Stdout = os.Stdout
76+
editorCmd.Stderr = os.Stderr
77+
logrus.Debugf("opening editor %q for a file %q", editor, tmpYAMLPath)
78+
if err := editorCmd.Run(); err != nil {
79+
return nil, fmt.Errorf("could not execute editor %q for a file %q: %w", editor, tmpYAMLPath, err)
80+
}
81+
b, err := os.ReadFile(tmpYAMLPath)
82+
if err != nil {
83+
return nil, err
84+
}
85+
modifiedInclHdr := string(b)
86+
modifiedExclHdr := strings.TrimPrefix(modifiedInclHdr, hdr)
87+
if strings.TrimSpace(modifiedExclHdr) == "" {
88+
return nil, nil
89+
}
90+
return []byte(modifiedExclHdr), nil
91+
}

0 commit comments

Comments
 (0)