1
1
package main
2
2
3
3
import (
4
- "context"
5
4
"errors"
6
5
"fmt"
7
6
"io"
@@ -15,16 +14,14 @@ import (
15
14
"github.com/lima-vm/lima/cmd/limactl/editflags"
16
15
"github.com/lima-vm/lima/cmd/limactl/guessarg"
17
16
"github.com/lima-vm/lima/pkg/editutil"
17
+ "github.com/lima-vm/lima/pkg/instance"
18
18
"github.com/lima-vm/lima/pkg/ioutilx"
19
- "github.com/lima-vm/lima/pkg/limayaml"
20
19
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
21
- "github.com/lima-vm/lima/pkg/osutil"
22
20
"github.com/lima-vm/lima/pkg/start"
23
21
"github.com/lima-vm/lima/pkg/store"
24
22
"github.com/lima-vm/lima/pkg/store/filenames"
25
23
"github.com/lima-vm/lima/pkg/templatestore"
26
24
"github.com/lima-vm/lima/pkg/uiutil"
27
- "github.com/lima-vm/lima/pkg/version"
28
25
"github.com/lima-vm/lima/pkg/yqutil"
29
26
"github.com/sirupsen/logrus"
30
27
"github.com/spf13/cobra"
@@ -229,7 +226,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
229
226
inst , err := store .Inspect (st .instName )
230
227
if err == nil {
231
228
if createOnly {
232
- return nil , fmt .Errorf ("Instance %q already exists" , st .instName )
229
+ return nil , fmt .Errorf ("instance %q already exists" , st .instName )
233
230
}
234
231
logrus .Infof ("Using the existing instance %q" , st .instName )
235
232
yqExprs , err := editflags .YQExpressions (flags , false )
@@ -276,8 +273,8 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
276
273
return nil , err
277
274
}
278
275
}
279
- saveBrokenEditorBuffer := tty
280
- return createInstance (cmd .Context (), st , saveBrokenEditorBuffer )
276
+ saveBrokenYAML := tty
277
+ return instance . Create (cmd .Context (), st . instName , st . yBytes , saveBrokenYAML )
281
278
}
282
279
283
280
func applyYQExpressionToExistingInstance (inst * store.Instance , yq string ) (* store.Instance , error ) {
@@ -313,66 +310,6 @@ func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*stor
313
310
return store .Inspect (inst .Name )
314
311
}
315
312
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
-
376
313
type creatorState struct {
377
314
instName string // instance name
378
315
yBytes []byte // yaml bytes
0 commit comments