Skip to content

Commit 89e926a

Browse files
committed
Make sure the Templates() function returns all templates from LIMA_TEMPLATES_PATH
Signed-off-by: Jan Dubois <[email protected]>
1 parent 4245fee commit 89e926a

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

pkg/templatestore/templatestore.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package templatestore
55

66
import (
7+
"cmp"
78
"errors"
89
"fmt"
910
"io/fs"
1011
"os"
1112
"path/filepath"
13+
"slices"
1214
"strings"
1315
"unicode"
1416

@@ -21,25 +23,33 @@ type Template struct {
2123
Location string `json:"location"`
2224
}
2325

24-
func Read(name string) ([]byte, error) {
25-
var pathList []string
26+
func TemplatesPaths() ([]string, error) {
27+
var paths []string
2628
if tmplPath := os.Getenv("LIMA_TEMPLATES_PATH"); tmplPath != "" {
27-
pathList = strings.Split(tmplPath, string(filepath.ListSeparator))
29+
paths = strings.Split(tmplPath, string(filepath.ListSeparator))
2830
} else {
2931
dir, err := usrlocalsharelima.Dir()
3032
if err != nil {
3133
return nil, err
3234
}
33-
pathList = []string{filepath.Join(dir, "templates")}
35+
paths = []string{filepath.Join(dir, "templates")}
36+
}
37+
return paths, nil
38+
}
39+
40+
func Read(name string) ([]byte, error) {
41+
paths, err := TemplatesPaths()
42+
if err != nil {
43+
return nil, err
3444
}
3545
ext := filepath.Ext(name)
3646
// Append .yaml extension if name doesn't have an extension, or if it starts with a digit.
3747
// So "docker.sh" would remain unchanged but "ubuntu-24.04" becomes "ubuntu-24.04.yaml".
3848
if len(ext) < 2 || unicode.IsDigit(rune(ext[1])) {
3949
name += ".yaml"
4050
}
41-
for _, path := range pathList {
42-
filePath, err := securejoin.SecureJoin(path, name)
51+
for _, templatesDir := range paths {
52+
filePath, err := securejoin.SecureJoin(templatesDir, name)
4353
if err != nil {
4454
return nil, err
4555
}
@@ -53,31 +63,36 @@ func Read(name string) ([]byte, error) {
5363
const Default = "default"
5464

5565
func Templates() ([]Template, error) {
56-
usrlocalsharelimaDir, err := usrlocalsharelima.Dir()
66+
paths, err := TemplatesPaths()
5767
if err != nil {
5868
return nil, err
5969
}
60-
templatesDir := filepath.Join(usrlocalsharelimaDir, "templates")
6170

62-
var res []Template
63-
walkDirFn := func(p string, _ fs.DirEntry, err error) error {
64-
if err != nil {
65-
return err
66-
}
67-
base := filepath.Base(p)
68-
if strings.HasPrefix(base, ".") || !strings.HasSuffix(base, ".yaml") {
71+
templates := make(map[string]string)
72+
for _, templatesDir := range paths {
73+
walkDirFn := func(p string, _ fs.DirEntry, err error) error {
74+
if err != nil {
75+
return err
76+
}
77+
base := filepath.Base(p)
78+
if strings.HasPrefix(base, ".") || !strings.HasSuffix(base, ".yaml") {
79+
return nil
80+
}
81+
// Name is like "default", "debian", "deprecated/centos-7", ...
82+
name := strings.TrimSuffix(strings.TrimPrefix(p, templatesDir+"/"), ".yaml")
83+
if _, ok := templates[name]; !ok {
84+
templates[name] = p
85+
}
6986
return nil
7087
}
71-
x := Template{
72-
// Name is like "default", "debian", "deprecated/centos-7", ...
73-
Name: strings.TrimSuffix(strings.TrimPrefix(p, templatesDir+"/"), ".yaml"),
74-
Location: p,
88+
if err = filepath.WalkDir(templatesDir, walkDirFn); err != nil {
89+
return nil, err
7590
}
76-
res = append(res, x)
77-
return nil
7891
}
79-
if err = filepath.WalkDir(templatesDir, walkDirFn); err != nil {
80-
return nil, err
92+
var res []Template
93+
for name, loc := range templates {
94+
res = append(res, Template{Name: name, Location: loc})
8195
}
96+
slices.SortFunc(res, func(i, j Template) int { return cmp.Compare(i.Name, j.Name) })
8297
return res, nil
8398
}

0 commit comments

Comments
 (0)