-
Notifications
You must be signed in to change notification settings - Fork 117
/
artifacts.go
118 lines (95 loc) · 3.08 KB
/
artifacts.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package artifacts
import (
"bytes"
"context"
"errors"
"fmt"
"regexp"
"strings"
"text/template"
"unicode"
"github.com/Masterminds/sprig/v3"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/fileutil"
)
var Artifacts = make(map[string]Artifact)
var (
ErrFileRead = errors.New("failed to read artifact")
ErrInvalidFileName = errors.New("invalid file name")
)
func Register(name string, artifact Artifact) {
if Artifacts[name] != nil {
panic(fmt.Errorf("already registered artifact type with name '%s'", name))
}
Artifacts[name] = artifact
}
type Artifact interface {
DeSerialise(ctx context.Context, filePath string, blob string) (*drivers.CatalogEntry, error)
Serialise(ctx context.Context, catalogObject *drivers.CatalogEntry) (string, error)
}
func Read(ctx context.Context, repoStore drivers.RepoStore, registryStore drivers.RegistryStore, instID, filePath string) (*drivers.CatalogEntry, error) {
extension := fileutil.FullExt(filePath)
artifact, ok := Artifacts[extension]
if !ok {
return nil, fmt.Errorf("no artifact found for %s", extension)
}
blob, err := repoStore.Get(ctx, instID, filePath)
if err != nil {
return nil, ErrFileRead
}
instance, err := registryStore.FindInstance(ctx, instID)
if err != nil {
return nil, err
}
// this is required in order to be able to use .env.KEY and not .KEY in template placeholders
env := map[string]map[string]string{"env": instance.EnvironmentVariables()}
// Add Sprig template functions (removing functions that leak host info)
// Derived from Helm: https://github.com/helm/helm/blob/main/pkg/engine/funcs.go
funcMap := sprig.TxtFuncMap()
delete(funcMap, "env")
delete(funcMap, "expandenv")
// convert templatised artifact
t, err := template.New("source").Funcs(funcMap).Option("missingkey=error").Parse(blob)
if err != nil {
return nil, err
}
bw := new(bytes.Buffer)
if err := t.Execute(bw, env); err != nil {
return nil, err
}
catalog, err := artifact.DeSerialise(ctx, filePath, bw.String())
if err != nil {
return nil, err
}
if !IsValidName(fileutil.Stem(filePath)) {
return nil, ErrInvalidFileName
}
catalog.Path = filePath
return catalog, nil
}
func Write(ctx context.Context, repoStore drivers.RepoStore, instID string, catalog *drivers.CatalogEntry) error {
extension := fileutil.FullExt(catalog.Path)
artifact, ok := Artifacts[extension]
if !ok {
return fmt.Errorf("no artifact found for %s", extension)
}
blob, err := artifact.Serialise(ctx, catalog)
if err != nil {
return err
}
return repoStore.Put(ctx, instID, catalog.Path, strings.NewReader(blob))
}
var regex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
func IsValidName(itemName string) bool {
return regex.MatchString(itemName)
}
var invalidChars = regexp.MustCompile(`[^a-zA-Z_\d]`)
// SanitizedName returns a sanitized name for an artifact from file path.
func SanitizedName(filePath string) string {
name := invalidChars.ReplaceAllString(fileutil.Stem(filePath), "_")
if unicode.IsNumber(rune(name[0])) {
// prepend underscore if name starts with a number
name = "_" + name
}
return name
}