11package main
22
33import (
4- "context"
54 "errors"
65 "fmt"
76 "io"
@@ -15,16 +14,14 @@ import (
1514 "github.com/lima-vm/lima/cmd/limactl/editflags"
1615 "github.com/lima-vm/lima/cmd/limactl/guessarg"
1716 "github.com/lima-vm/lima/pkg/editutil"
17+ "github.com/lima-vm/lima/pkg/instance"
1818 "github.com/lima-vm/lima/pkg/ioutilx"
19- "github.com/lima-vm/lima/pkg/limayaml"
2019 networks "github.com/lima-vm/lima/pkg/networks/reconcile"
21- "github.com/lima-vm/lima/pkg/osutil"
2220 "github.com/lima-vm/lima/pkg/start"
2321 "github.com/lima-vm/lima/pkg/store"
2422 "github.com/lima-vm/lima/pkg/store/filenames"
2523 "github.com/lima-vm/lima/pkg/templatestore"
2624 "github.com/lima-vm/lima/pkg/uiutil"
27- "github.com/lima-vm/lima/pkg/version"
2825 "github.com/lima-vm/lima/pkg/yqutil"
2926 "github.com/sirupsen/logrus"
3027 "github.com/spf13/cobra"
@@ -229,7 +226,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
229226 inst , err := store .Inspect (st .instName )
230227 if err == nil {
231228 if createOnly {
232- return nil , fmt .Errorf ("Instance %q already exists" , st .instName )
229+ return nil , fmt .Errorf ("instance %q already exists" , st .instName )
233230 }
234231 logrus .Infof ("Using the existing instance %q" , st .instName )
235232 yqExprs , err := editflags .YQExpressions (flags , false )
@@ -276,8 +273,8 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
276273 return nil , err
277274 }
278275 }
279- saveBrokenEditorBuffer := tty
280- return createInstance (cmd .Context (), st , saveBrokenEditorBuffer )
276+ saveBrokenYAML := tty
277+ return instance . Create (cmd .Context (), st . instName , st . yBytes , saveBrokenYAML )
281278}
282279
283280func applyYQExpressionToExistingInstance (inst * store.Instance , yq string ) (* store.Instance , error ) {
@@ -313,66 +310,6 @@ func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*stor
313310 return store .Inspect (inst .Name )
314311}
315312
316- func createInstance (ctx context.Context , st * creatorState , saveBrokenEditorBuffer bool ) (* store.Instance , error ) {
317- if st .instName == "" {
318- return nil , errors .New ("got empty st.instName" )
319- }
320- if len (st .yBytes ) == 0 {
321- return nil , errors .New ("got empty st.yBytes" )
322- }
323-
324- instDir , err := store .InstanceDir (st .instName )
325- if err != nil {
326- return nil , err
327- }
328-
329- // the full path of the socket name must be less than UNIX_PATH_MAX chars.
330- maxSockName := filepath .Join (instDir , filenames .LongestSock )
331- if len (maxSockName ) >= osutil .UnixPathMax {
332- return nil , fmt .Errorf ("instance name %q too long: %q must be less than UNIX_PATH_MAX=%d characters, but is %d" ,
333- st .instName , maxSockName , osutil .UnixPathMax , len (maxSockName ))
334- }
335- if _ , err := os .Stat (instDir ); ! errors .Is (err , os .ErrNotExist ) {
336- return nil , fmt .Errorf ("instance %q already exists (%q)" , st .instName , instDir )
337- }
338- // limayaml.Load() needs to pass the store file path to limayaml.FillDefault() to calculate default MAC addresses
339- filePath := filepath .Join (instDir , filenames .LimaYAML )
340- y , err := limayaml .Load (st .yBytes , filePath )
341- if err != nil {
342- return nil , err
343- }
344- if err := limayaml .Validate (y , true ); err != nil {
345- if ! saveBrokenEditorBuffer {
346- return nil , err
347- }
348- rejectedYAML := "lima.REJECTED.yaml"
349- if writeErr := os .WriteFile (rejectedYAML , st .yBytes , 0o644 ); writeErr != nil {
350- return nil , fmt .Errorf ("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w" , rejectedYAML , writeErr , err )
351- }
352- return nil , fmt .Errorf ("the YAML is invalid, saved the buffer as %q: %w" , rejectedYAML , err )
353- }
354- if err := os .MkdirAll (instDir , 0o700 ); err != nil {
355- return nil , err
356- }
357- if err := os .WriteFile (filePath , st .yBytes , 0o644 ); err != nil {
358- return nil , err
359- }
360- if err := os .WriteFile (filepath .Join (instDir , filenames .LimaVersion ), []byte (version .Version ), 0o444 ); err != nil {
361- return nil , err
362- }
363-
364- inst , err := store .Inspect (st .instName )
365- if err != nil {
366- return nil , err
367- }
368-
369- if err := start .Register (ctx , inst ); err != nil {
370- return nil , err
371- }
372-
373- return inst , nil
374- }
375-
376313type creatorState struct {
377314 instName string // instance name
378315 yBytes []byte // yaml bytes
0 commit comments