-
Notifications
You must be signed in to change notification settings - Fork 111
/
example.go
71 lines (57 loc) · 1.3 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package examples
import (
"embed"
"errors"
"io/fs"
"os"
"path/filepath"
"github.com/rilldata/rill/runtime/compilers/rillv1beta"
)
//go:embed all:embed
var examplesFS embed.FS
var ErrExampleNotFound = errors.New("example not found")
type Example struct {
Name string
Title string
Description string
}
func List() ([]Example, error) {
entries, err := examplesFS.ReadDir("embed/dist")
if err != nil {
return nil, err
}
exampleList := make([]Example, 0, len(entries))
for _, entry := range entries {
if !entry.IsDir() {
continue
}
rillYamlContents, err := examplesFS.ReadFile(filepath.Join("embed/dist", entry.Name(), "rill.yaml"))
if err != nil {
return nil, err
}
rillYaml, err := rillv1beta.ParseProjectConfig(rillYamlContents)
if err != nil {
return nil, err
}
exampleList = append(exampleList, Example{
Name: entry.Name(),
Title: rillYaml.Title,
Description: rillYaml.Description,
})
}
return exampleList, nil
}
func Get(name string) (fs.FS, error) {
exampleFS, err := fs.Sub(examplesFS, filepath.Join("embed", "dist", name))
if err != nil {
return nil, err
}
_, err = fs.Stat(exampleFS, "rill.yaml")
if err != nil {
if os.IsNotExist(err) {
return nil, ErrExampleNotFound
}
return nil, err
}
return exampleFS, nil
}