@@ -4,14 +4,13 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"net/http"
7
- "net/url"
8
7
"os"
9
- "path"
10
8
"path/filepath"
11
9
"strings"
12
10
13
11
"github.com/AlecAivazis/survey/v2"
14
12
"github.com/containerd/containerd/identifiers"
13
+ "github.com/lima-vm/lima/cmd/limactl/guessarg"
15
14
"github.com/lima-vm/lima/pkg/editutil"
16
15
"github.com/lima-vm/lima/pkg/ioutilx"
17
16
"github.com/lima-vm/lima/pkg/limayaml"
@@ -73,7 +72,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
73
72
}
74
73
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
75
74
76
- if ok , u := argSeemsTemplateURL (arg ); ok {
75
+ if ok , u := guessarg . SeemsTemplateURL (arg ); ok {
77
76
// No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
78
77
templateName := filepath .Join (u .Host , u .Path )
79
78
logrus .Debugf ("interpreting argument %q as a template name %q" , arg , templateName )
@@ -85,9 +84,9 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
85
84
if err != nil {
86
85
return nil , err
87
86
}
88
- } else if argSeemsHTTPURL (arg ) {
87
+ } else if guessarg . SeemsHTTPURL (arg ) {
89
88
if st .instName == "" {
90
- st .instName , err = instNameFromURL (arg )
89
+ st .instName , err = guessarg . InstNameFromURL (arg )
91
90
if err != nil {
92
91
return nil , err
93
92
}
@@ -102,9 +101,9 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
102
101
if err != nil {
103
102
return nil , err
104
103
}
105
- } else if argSeemsFileURL (arg ) {
104
+ } else if guessarg . SeemsFileURL (arg ) {
106
105
if st .instName == "" {
107
- st .instName , err = instNameFromURL (arg )
106
+ st .instName , err = guessarg . InstNameFromURL (arg )
108
107
if err != nil {
109
108
return nil , err
110
109
}
@@ -119,9 +118,9 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
119
118
if err != nil {
120
119
return nil , err
121
120
}
122
- } else if argSeemsYAMLPath (arg ) {
121
+ } else if guessarg . SeemsYAMLPath (arg ) {
123
122
if st .instName == "" {
124
- st .instName , err = instNameFromYAMLPath (arg )
123
+ st .instName , err = guessarg . InstNameFromYAMLPath (arg )
125
124
if err != nil {
126
125
return nil , err
127
126
}
@@ -300,7 +299,7 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
300
299
return st , fmt .Errorf ("invalid answer %d for %d entries" , ansEx , len (examples ))
301
300
}
302
301
yamlPath := examples [ansEx ].Location
303
- st .instName , err = instNameFromYAMLPath (yamlPath )
302
+ st .instName , err = guessarg . InstNameFromYAMLPath (yamlPath )
304
303
if err != nil {
305
304
return nil , err
306
305
}
@@ -357,59 +356,6 @@ func startAction(cmd *cobra.Command, args []string) error {
357
356
return start .Start (ctx , inst )
358
357
}
359
358
360
- func argSeemsTemplateURL (arg string ) (bool , * url.URL ) {
361
- u , err := url .Parse (arg )
362
- if err != nil {
363
- return false , u
364
- }
365
- return u .Scheme == "template" , u
366
- }
367
-
368
- func argSeemsHTTPURL (arg string ) bool {
369
- u , err := url .Parse (arg )
370
- if err != nil {
371
- return false
372
- }
373
- if u .Scheme != "http" && u .Scheme != "https" {
374
- return false
375
- }
376
- return true
377
- }
378
-
379
- func argSeemsFileURL (arg string ) bool {
380
- u , err := url .Parse (arg )
381
- if err != nil {
382
- return false
383
- }
384
- return u .Scheme == "file"
385
- }
386
-
387
- func argSeemsYAMLPath (arg string ) bool {
388
- if strings .Contains (arg , "/" ) {
389
- return true
390
- }
391
- lower := strings .ToLower (arg )
392
- return strings .HasSuffix (lower , ".yml" ) || strings .HasSuffix (lower , ".yaml" )
393
- }
394
-
395
- func instNameFromURL (urlStr string ) (string , error ) {
396
- u , err := url .Parse (urlStr )
397
- if err != nil {
398
- return "" , err
399
- }
400
- return instNameFromYAMLPath (path .Base (u .Path ))
401
- }
402
-
403
- func instNameFromYAMLPath (yamlPath string ) (string , error ) {
404
- s := strings .ToLower (filepath .Base (yamlPath ))
405
- s = strings .TrimSuffix (strings .TrimSuffix (s , ".yml" ), ".yaml" )
406
- s = strings .ReplaceAll (s , "." , "-" )
407
- if err := identifiers .Validate (s ); err != nil {
408
- return "" , fmt .Errorf ("filename %q is invalid: %w" , yamlPath , err )
409
- }
410
- return s , nil
411
- }
412
-
413
359
func startBashComplete (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
414
360
comp , _ := bashCompleteInstanceNames (cmd )
415
361
if templates , err := templatestore .Templates (); err == nil {
0 commit comments