Skip to content

Commit eabffb0

Browse files
authored
Merge pull request #805 from AkihiroSuda/fix-785
limactl start: support subdirectories
2 parents ae3e43e + 5ef019d commit eabffb0

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

cmd/limactl/start.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"io/fs"
78
"net/http"
89
"net/url"
910
"os"
@@ -14,6 +15,7 @@ import (
1415

1516
"github.com/AlecAivazis/survey/v2"
1617
"github.com/containerd/containerd/identifiers"
18+
securejoin "github.com/cyphar/filepath-securejoin"
1719
"github.com/lima-vm/lima/pkg/limayaml"
1820
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
1921
"github.com/lima-vm/lima/pkg/osutil"
@@ -63,10 +65,10 @@ func readTemplate(name string) ([]byte, error) {
6365
if err != nil {
6466
return nil, err
6567
}
66-
if strings.Contains(name, string(os.PathSeparator)) {
67-
return nil, fmt.Errorf("invalid template name %q", name)
68+
defaultYAMLPath, err := securejoin.SecureJoin(filepath.Join(dir, "examples"), name+".yaml")
69+
if err != nil {
70+
return nil, err
6871
}
69-
defaultYAMLPath := filepath.Join(dir, "examples", name+".yaml")
7072
return os.ReadFile(defaultYAMLPath)
7173
}
7274

@@ -91,10 +93,12 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
9193
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
9294

9395
if ok, u := argSeemsTemplateURL(arg); ok {
94-
templateName := u.Host
96+
// No need to use SecureJoin here. https://github.com/lima-vm/lima/pull/805#discussion_r853411702
97+
templateName := filepath.Join(u.Host, u.Path)
9598
logrus.Debugf("interpreting argument %q as a template name %q", arg, templateName)
9699
if st.instName == "" {
97-
st.instName = templateName
100+
// e.g., templateName = "deprecated/centos-7" , st.instName = "centos-7"
101+
st.instName = filepath.Base(templateName)
98102
}
99103
st.yBytes, err = readTemplate(templateName)
100104
if err != nil {
@@ -340,21 +344,26 @@ func listTemplateYAMLs() ([]TemplateYAML, error) {
340344
return nil, err
341345
}
342346
examplesDir := filepath.Join(usrlocalsharelimaDir, "examples")
343-
glob := filepath.Join(examplesDir, "*.yaml")
344-
globbed, err := filepath.Glob(glob)
345-
if err != nil {
346-
return nil, err
347-
}
347+
348348
var res []TemplateYAML
349-
for _, f := range globbed {
350-
base := filepath.Base(f)
351-
if strings.HasPrefix(base, ".") {
352-
continue
349+
walkDirFn := func(p string, d fs.DirEntry, err error) error {
350+
if err != nil {
351+
return err
352+
}
353+
base := filepath.Base(p)
354+
if strings.HasPrefix(base, ".") || !strings.HasSuffix(base, ".yaml") {
355+
return nil
356+
}
357+
x := TemplateYAML{
358+
// Name is like "default", "debian", "deprecated/centos-7", ...
359+
Name: strings.TrimSuffix(strings.TrimPrefix(p, examplesDir+"/"), ".yaml"),
360+
Location: p,
353361
}
354-
res = append(res, TemplateYAML{
355-
Name: strings.TrimSuffix(filepath.Base(f), ".yaml"),
356-
Location: f,
357-
})
362+
res = append(res, x)
363+
return nil
364+
}
365+
if err = filepath.WalkDir(examplesDir, walkDirFn); err != nil {
366+
return nil, err
358367
}
359368
return res, nil
360369
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/containerd/containerd v1.6.2
1010
github.com/containerd/continuity v0.3.0
1111
github.com/coreos/go-semver v0.3.0
12+
github.com/cyphar/filepath-securejoin v0.2.3
1213
github.com/digitalocean/go-qemu v0.0.0-20210326154740-ac9e0b687001
1314
github.com/diskfs/go-diskfs v1.2.0
1415
github.com/docker/go-units v0.4.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
3737
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3838
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
3939
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
40+
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
41+
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
4042
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4143
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4244
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)