@@ -8,24 +8,22 @@ import (
8
8
"net/http"
9
9
"net/url"
10
10
"os"
11
- "os/exec"
12
11
"path"
13
12
"path/filepath"
14
13
"strings"
15
14
16
15
"github.com/AlecAivazis/survey/v2"
17
16
"github.com/containerd/containerd/identifiers"
18
17
securejoin "github.com/cyphar/filepath-securejoin"
18
+ "github.com/lima-vm/lima/pkg/editutil"
19
19
"github.com/lima-vm/lima/pkg/limayaml"
20
20
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
21
21
"github.com/lima-vm/lima/pkg/osutil"
22
22
"github.com/lima-vm/lima/pkg/start"
23
23
"github.com/lima-vm/lima/pkg/store"
24
- "github.com/lima-vm/lima/pkg/store/dirnames"
25
24
"github.com/lima-vm/lima/pkg/store/filenames"
26
25
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
27
26
"github.com/mattn/go-isatty"
28
- "github.com/norouter/norouter/cmd/norouter/editorcmd"
29
27
"github.com/sirupsen/logrus"
30
28
"github.com/spf13/cobra"
31
29
)
@@ -288,9 +286,9 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
288
286
}
289
287
hdr += "# - To cancel starting Lima, just save this file as an empty file.\n "
290
288
hdr += "\n "
291
- hdr += generateEditorWarningHeader ()
289
+ hdr += editutil . GenerateEditorWarningHeader ()
292
290
var err error
293
- st .yBytes , err = openEditor (st .instName , st .yBytes , hdr )
291
+ st .yBytes , err = editutil . OpenEditor (st .instName , st .yBytes , hdr )
294
292
if err != nil {
295
293
return st , err
296
294
}
@@ -367,82 +365,6 @@ func listTemplateYAMLs() ([]TemplateYAML, error) {
367
365
}
368
366
return res , nil
369
367
}
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
-
446
368
func startAction (cmd * cobra.Command , args []string ) error {
447
369
if listTemplates , err := cmd .Flags ().GetBool ("list-templates" ); err != nil {
448
370
return err
0 commit comments