Skip to content

Commit 6386046

Browse files
committed
cmd/limactl: split guessarg (no code change)
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 1777a3f commit 6386046

File tree

3 files changed

+75
-64
lines changed

3 files changed

+75
-64
lines changed

cmd/limactl/guessarg/guessarg.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package guessarg
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
"path"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/containerd/containerd/identifiers"
11+
)
12+
13+
func SeemsTemplateURL(arg string) (bool, *url.URL) {
14+
u, err := url.Parse(arg)
15+
if err != nil {
16+
return false, u
17+
}
18+
return u.Scheme == "template", u
19+
}
20+
21+
func SeemsHTTPURL(arg string) bool {
22+
u, err := url.Parse(arg)
23+
if err != nil {
24+
return false
25+
}
26+
if u.Scheme != "http" && u.Scheme != "https" {
27+
return false
28+
}
29+
return true
30+
}
31+
32+
func SeemsFileURL(arg string) bool {
33+
u, err := url.Parse(arg)
34+
if err != nil {
35+
return false
36+
}
37+
return u.Scheme == "file"
38+
}
39+
40+
func SeemsYAMLPath(arg string) bool {
41+
if strings.Contains(arg, "/") {
42+
return true
43+
}
44+
lower := strings.ToLower(arg)
45+
return strings.HasSuffix(lower, ".yml") || strings.HasSuffix(lower, ".yaml")
46+
}
47+
48+
func InstNameFromURL(urlStr string) (string, error) {
49+
u, err := url.Parse(urlStr)
50+
if err != nil {
51+
return "", err
52+
}
53+
return InstNameFromYAMLPath(path.Base(u.Path))
54+
}
55+
56+
func InstNameFromYAMLPath(yamlPath string) (string, error) {
57+
s := strings.ToLower(filepath.Base(yamlPath))
58+
s = strings.TrimSuffix(strings.TrimSuffix(s, ".yml"), ".yaml")
59+
s = strings.ReplaceAll(s, ".", "-")
60+
if err := identifiers.Validate(s); err != nil {
61+
return "", fmt.Errorf("filename %q is invalid: %w", yamlPath, err)
62+
}
63+
return s, nil
64+
}

cmd/limactl/start.go

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7-
"net/url"
87
"os"
9-
"path"
108
"path/filepath"
119
"strings"
1210

1311
"github.com/AlecAivazis/survey/v2"
1412
"github.com/containerd/containerd/identifiers"
13+
"github.com/lima-vm/lima/cmd/limactl/guessarg"
1514
"github.com/lima-vm/lima/pkg/editutil"
1615
"github.com/lima-vm/lima/pkg/ioutilx"
1716
"github.com/lima-vm/lima/pkg/limayaml"
@@ -73,7 +72,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
7372
}
7473
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
7574

76-
if ok, u := argSeemsTemplateURL(arg); ok {
75+
if ok, u := guessarg.SeemsTemplateURL(arg); ok {
7776
// No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
7877
templateName := filepath.Join(u.Host, u.Path)
7978
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
8584
if err != nil {
8685
return nil, err
8786
}
88-
} else if argSeemsHTTPURL(arg) {
87+
} else if guessarg.SeemsHTTPURL(arg) {
8988
if st.instName == "" {
90-
st.instName, err = instNameFromURL(arg)
89+
st.instName, err = guessarg.InstNameFromURL(arg)
9190
if err != nil {
9291
return nil, err
9392
}
@@ -102,9 +101,9 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
102101
if err != nil {
103102
return nil, err
104103
}
105-
} else if argSeemsFileURL(arg) {
104+
} else if guessarg.SeemsFileURL(arg) {
106105
if st.instName == "" {
107-
st.instName, err = instNameFromURL(arg)
106+
st.instName, err = guessarg.InstNameFromURL(arg)
108107
if err != nil {
109108
return nil, err
110109
}
@@ -119,9 +118,9 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
119118
if err != nil {
120119
return nil, err
121120
}
122-
} else if argSeemsYAMLPath(arg) {
121+
} else if guessarg.SeemsYAMLPath(arg) {
123122
if st.instName == "" {
124-
st.instName, err = instNameFromYAMLPath(arg)
123+
st.instName, err = guessarg.InstNameFromYAMLPath(arg)
125124
if err != nil {
126125
return nil, err
127126
}
@@ -300,7 +299,7 @@ func chooseNextCreatorState(st *creatorState) (*creatorState, error) {
300299
return st, fmt.Errorf("invalid answer %d for %d entries", ansEx, len(examples))
301300
}
302301
yamlPath := examples[ansEx].Location
303-
st.instName, err = instNameFromYAMLPath(yamlPath)
302+
st.instName, err = guessarg.InstNameFromYAMLPath(yamlPath)
304303
if err != nil {
305304
return nil, err
306305
}
@@ -357,59 +356,6 @@ func startAction(cmd *cobra.Command, args []string) error {
357356
return start.Start(ctx, inst)
358357
}
359358

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-
413359
func startBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
414360
comp, _ := bashCompleteInstanceNames(cmd)
415361
if templates, err := templatestore.Templates(); err == nil {

cmd/limactl/validate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/lima-vm/lima/cmd/limactl/guessarg"
67
"github.com/lima-vm/lima/pkg/store"
78
"github.com/spf13/cobra"
89

@@ -25,7 +26,7 @@ func validateAction(cmd *cobra.Command, args []string) error {
2526
if err != nil {
2627
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
2728
}
28-
if _, err := instNameFromYAMLPath(f); err != nil {
29+
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
2930
return err
3031
}
3132
logrus.Infof("%q: OK", f)

0 commit comments

Comments
 (0)