Skip to content

Commit ece0e51

Browse files
authored
Merge pull request #1066 from AkihiroSuda/dev
refactor (no substantial code change)
2 parents 42e4f43 + 6386046 commit ece0e51

File tree

26 files changed

+380
-327
lines changed

26 files changed

+380
-327
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Run golangci-lint
2323
uses: golangci/golangci-lint-action@v3
2424
with:
25-
version: v1.48.0
25+
version: v1.49.0
2626
args: --verbose
2727
- name: Run yamllint
2828
run: yamllint .

.golangci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ run:
2222
linters:
2323
disable-all: true
2424
enable:
25-
- deadcode
2625
- depguard
2726
- gofmt
2827
- goimports
@@ -31,9 +30,7 @@ linters:
3130
- misspell
3231
- nakedret
3332
# - prealloc
34-
- structcheck
3533
- typecheck
36-
- varcheck
3734
# - asciicheck
3835
# - bodyclose
3936
# - dogsled
@@ -76,9 +73,10 @@ linters:
7673
# - stylecheck
7774
# - testpackage
7875
# - tparallel
76+
- revive
7977
# - unconvert
8078
# - unparam
81-
# - unused
79+
- unused
8280
# - whitespace
8381
# - wrapcheck
8482
# - wsl

cmd/lima-guestagent/install_systemd_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010

11-
"github.com/lima-vm/lima/pkg/templateutil"
11+
"github.com/lima-vm/lima/pkg/textutil"
1212
"github.com/sirupsen/logrus"
1313
"github.com/spf13/cobra"
1414
)
@@ -68,5 +68,5 @@ func generateSystemdUnit() ([]byte, error) {
6868
m := map[string]string{
6969
"Binary": selfExeAbs,
7070
}
71-
return templateutil.Execute(systemdUnitTemplate, m)
71+
return textutil.ExecuteTemplate(systemdUnitTemplate, m)
7272
}

cmd/limactl/edit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/AlecAivazis/survey/v2"
11+
"github.com/lima-vm/lima/pkg/editutil"
1112
"github.com/lima-vm/lima/pkg/limayaml"
1213
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
1314
"github.com/lima-vm/lima/pkg/start"
@@ -55,8 +56,8 @@ func editAction(cmd *cobra.Command, args []string) error {
5556
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
5657
hdr += "# and an empty file will abort the edit.\n"
5758
hdr += "\n"
58-
hdr += generateEditorWarningHeader()
59-
yBytes, err := openEditor(instName, yContent, hdr)
59+
hdr += editutil.GenerateEditorWarningHeader()
60+
yBytes, err := editutil.OpenEditor(instName, yContent, hdr)
6061
if err != nil {
6162
return err
6263
}

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/info.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/lima-vm/lima/pkg/limayaml"
8-
"github.com/lima-vm/lima/pkg/store/dirnames"
9-
"github.com/lima-vm/lima/pkg/version"
7+
"github.com/lima-vm/lima/pkg/infoutil"
108
"github.com/spf13/cobra"
119
)
1210

@@ -20,37 +18,8 @@ func newInfoCommand() *cobra.Command {
2018
return infoCommand
2119
}
2220

23-
type TemplateYAML struct {
24-
Name string `json:"name"`
25-
Location string `json:"location"`
26-
}
27-
28-
type Info struct {
29-
Version string `json:"version"`
30-
Templates []TemplateYAML `json:"templates"`
31-
DefaultTemplate *limayaml.LimaYAML `json:"defaultTemplate"`
32-
LimaHome string `json:"limaHome"`
33-
// TODO: add diagnostic info of QEMU
34-
}
35-
3621
func infoAction(cmd *cobra.Command, args []string) error {
37-
b, err := readDefaultTemplate()
38-
if err != nil {
39-
return err
40-
}
41-
y, err := limayaml.Load(b, "")
42-
if err != nil {
43-
return err
44-
}
45-
info := &Info{
46-
Version: version.Version,
47-
DefaultTemplate: y,
48-
}
49-
info.Templates, err = listTemplateYAMLs()
50-
if err != nil {
51-
return err
52-
}
53-
info.LimaHome, err = dirnames.LimaDir()
22+
info, err := infoutil.GetInfo()
5423
if err != nil {
5524
return err
5625
}

0 commit comments

Comments
 (0)