4
4
"errors"
5
5
"fmt"
6
6
"io"
7
- "io/fs"
8
7
"net/http"
9
8
"net/url"
10
9
"os"
@@ -14,15 +13,14 @@ import (
14
13
15
14
"github.com/AlecAivazis/survey/v2"
16
15
"github.com/containerd/containerd/identifiers"
17
- securejoin "github.com/cyphar/filepath-securejoin"
18
16
"github.com/lima-vm/lima/pkg/editutil"
19
17
"github.com/lima-vm/lima/pkg/limayaml"
20
18
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
21
19
"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
- "github.com/lima-vm/lima/pkg/usrlocalsharelima "
23
+ "github.com/lima-vm/lima/pkg/templatestore "
26
24
"github.com/mattn/go-isatty"
27
25
"github.com/sirupsen/logrus"
28
26
"github.com/spf13/cobra"
@@ -59,22 +57,6 @@ $ limactl start --name=default https://raw.githubusercontent.com/lima-vm/lima/ma
59
57
return startCommand
60
58
}
61
59
62
- func readTemplate (name string ) ([]byte , error ) {
63
- dir , err := usrlocalsharelima .Dir ()
64
- if err != nil {
65
- return nil , err
66
- }
67
- defaultYAMLPath , err := securejoin .SecureJoin (filepath .Join (dir , "examples" ), name + ".yaml" )
68
- if err != nil {
69
- return nil , err
70
- }
71
- return os .ReadFile (defaultYAMLPath )
72
- }
73
-
74
- func readDefaultTemplate () ([]byte , error ) {
75
- return readTemplate ("default" )
76
- }
77
-
78
60
func loadOrCreateInstance (cmd * cobra.Command , args []string ) (* store.Instance , error ) {
79
61
var arg string // can be empty
80
62
if len (args ) > 0 {
@@ -99,7 +81,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
99
81
// e.g., templateName = "deprecated/centos-7" , st.instName = "centos-7"
100
82
st .instName = filepath .Base (templateName )
101
83
}
102
- st .yBytes , err = readTemplate (templateName )
84
+ st .yBytes , err = templatestore . Read (templateName )
103
85
if err != nil {
104
86
return nil , err
105
87
}
@@ -184,7 +166,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
184
166
logrus .Warnf ("This form is deprecated. Use `limactl start --name=%s template://default` instead" , st .instName )
185
167
}
186
168
// Read the default template for creating a new instance
187
- st .yBytes , err = readDefaultTemplate ( )
169
+ st .yBytes , err = templatestore . Read ( templatestore . Default )
188
170
if err != nil {
189
171
return nil , err
190
172
}
@@ -299,7 +281,7 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
299
281
}
300
282
return st , nil
301
283
case prompt .Options [2 ]: // "Choose another example..."
302
- examples , err := listTemplateYAMLs ()
284
+ examples , err := templatestore . Templates ()
303
285
if err != nil {
304
286
return st , err
305
287
}
@@ -336,40 +318,11 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
336
318
}
337
319
}
338
320
339
- func listTemplateYAMLs () ([]TemplateYAML , error ) {
340
- usrlocalsharelimaDir , err := usrlocalsharelima .Dir ()
341
- if err != nil {
342
- return nil , err
343
- }
344
- examplesDir := filepath .Join (usrlocalsharelimaDir , "examples" )
345
-
346
- var res []TemplateYAML
347
- walkDirFn := func (p string , d fs.DirEntry , err error ) error {
348
- if err != nil {
349
- return err
350
- }
351
- base := filepath .Base (p )
352
- if strings .HasPrefix (base , "." ) || ! strings .HasSuffix (base , ".yaml" ) {
353
- return nil
354
- }
355
- x := TemplateYAML {
356
- // Name is like "default", "debian", "deprecated/centos-7", ...
357
- Name : strings .TrimSuffix (strings .TrimPrefix (p , examplesDir + "/" ), ".yaml" ),
358
- Location : p ,
359
- }
360
- res = append (res , x )
361
- return nil
362
- }
363
- if err = filepath .WalkDir (examplesDir , walkDirFn ); err != nil {
364
- return nil , err
365
- }
366
- return res , nil
367
- }
368
321
func startAction (cmd * cobra.Command , args []string ) error {
369
322
if listTemplates , err := cmd .Flags ().GetBool ("list-templates" ); err != nil {
370
323
return err
371
324
} else if listTemplates {
372
- if templates , err := listTemplateYAMLs (); err == nil {
325
+ if templates , err := templatestore . Templates (); err == nil {
373
326
w := cmd .OutOrStdout ()
374
327
for _ , f := range templates {
375
328
fmt .Fprintln (w , f .Name )
@@ -459,7 +412,7 @@ func instNameFromYAMLPath(yamlPath string) (string, error) {
459
412
460
413
func startBashComplete (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
461
414
comp , _ := bashCompleteInstanceNames (cmd )
462
- if templates , err := listTemplateYAMLs (); err == nil {
415
+ if templates , err := templatestore . Templates (); err == nil {
463
416
for _ , f := range templates {
464
417
comp = append (comp , "template://" + f .Name )
465
418
}
0 commit comments