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 ) {
@@ -301,66 +298,6 @@ func applyYQExpressionToExistingInstance(inst *store.Instance, yq string) (*stor
301
298
return store .Inspect (inst .Name )
302
299
}
303
300
304
- func createInstance (ctx context.Context , st * creatorState , saveBrokenEditorBuffer bool ) (* store.Instance , error ) {
305
- if st .instName == "" {
306
- return nil , errors .New ("got empty st.instName" )
307
- }
308
- if len (st .yBytes ) == 0 {
309
- return nil , errors .New ("got empty st.yBytes" )
310
- }
311
-
312
- instDir , err := store .InstanceDir (st .instName )
313
- if err != nil {
314
- return nil , err
315
- }
316
-
317
- // the full path of the socket name must be less than UNIX_PATH_MAX chars.
318
- maxSockName := filepath .Join (instDir , filenames .LongestSock )
319
- if len (maxSockName ) >= osutil .UnixPathMax {
320
- return nil , fmt .Errorf ("instance name %q too long: %q must be less than UNIX_PATH_MAX=%d characters, but is %d" ,
321
- st .instName , maxSockName , osutil .UnixPathMax , len (maxSockName ))
322
- }
323
- if _ , err := os .Stat (instDir ); ! errors .Is (err , os .ErrNotExist ) {
324
- return nil , fmt .Errorf ("instance %q already exists (%q)" , st .instName , instDir )
325
- }
326
- // limayaml.Load() needs to pass the store file path to limayaml.FillDefault() to calculate default MAC addresses
327
- filePath := filepath .Join (instDir , filenames .LimaYAML )
328
- y , err := limayaml .Load (st .yBytes , filePath )
329
- if err != nil {
330
- return nil , err
331
- }
332
- if err := limayaml .Validate (y , true ); err != nil {
333
- if ! saveBrokenEditorBuffer {
334
- return nil , err
335
- }
336
- rejectedYAML := "lima.REJECTED.yaml"
337
- if writeErr := os .WriteFile (rejectedYAML , st .yBytes , 0o644 ); writeErr != nil {
338
- return nil , fmt .Errorf ("the YAML is invalid, attempted to save the buffer as %q but failed: %w: %w" , rejectedYAML , writeErr , err )
339
- }
340
- return nil , fmt .Errorf ("the YAML is invalid, saved the buffer as %q: %w" , rejectedYAML , err )
341
- }
342
- if err := os .MkdirAll (instDir , 0o700 ); err != nil {
343
- return nil , err
344
- }
345
- if err := os .WriteFile (filePath , st .yBytes , 0o644 ); err != nil {
346
- return nil , err
347
- }
348
- if err := os .WriteFile (filepath .Join (instDir , filenames .LimaVersion ), []byte (version .Version ), 0o444 ); err != nil {
349
- return nil , err
350
- }
351
-
352
- inst , err := store .Inspect (st .instName )
353
- if err != nil {
354
- return nil , err
355
- }
356
-
357
- if err := start .Register (ctx , inst ); err != nil {
358
- return nil , err
359
- }
360
-
361
- return inst , nil
362
- }
363
-
364
301
type creatorState struct {
365
302
instName string // instance name
366
303
yBytes []byte // yaml bytes
0 commit comments