-
Notifications
You must be signed in to change notification settings - Fork 117
/
project.go
138 lines (109 loc) · 3.02 KB
/
project.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package rillv1beta
import (
"bytes"
"context"
"fmt"
"os"
"path"
"strings"
"github.com/go-yaml/yaml"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
)
const Version = "rill-beta"
type Codec struct {
Repo drivers.RepoStore
InstanceID string
}
func New(repo drivers.RepoStore, instanceID string) *Codec {
return &Codec{Repo: repo, InstanceID: instanceID}
}
func (c *Codec) IsInit(ctx context.Context) bool {
_, err := c.Repo.Get(ctx, c.InstanceID, "rill.yaml")
return err == nil
}
func (c *Codec) InitEmpty(ctx context.Context, name, rillVersion string) error {
err := c.Repo.Put(ctx, c.InstanceID, "rill.yaml", strings.NewReader(fmt.Sprintf("compiler: %s\nrill_version: %s\n\nname: %s\n", Version, rillVersion, name)))
if err != nil {
return err
}
gitignore, _ := c.Repo.Get(ctx, c.InstanceID, ".gitignore")
if gitignore != "" {
gitignore += "\n"
}
gitignore += "# Rill\n*.db\n*.db.wal\ndata/\n"
err = c.Repo.Put(ctx, c.InstanceID, ".gitignore", strings.NewReader(gitignore))
if err != nil {
return err
}
err = c.Repo.Put(ctx, c.InstanceID, "sources/.gitkeep", strings.NewReader(""))
if err != nil {
return err
}
err = c.Repo.Put(ctx, c.InstanceID, "models/.gitkeep", strings.NewReader(""))
if err != nil {
return err
}
err = c.Repo.Put(ctx, c.InstanceID, "dashboards/.gitkeep", strings.NewReader(""))
if err != nil {
return err
}
return nil
}
func (c *Codec) PutSource(ctx context.Context, repo drivers.RepoStore, instanceID string, source *runtimev1.Source, force bool) (string, error) {
props := source.Properties.AsMap()
out := Source{
Type: source.Connector,
}
if val, ok := props["uri"].(string); ok {
out.URI = val
}
if val, ok := props["path"].(string); ok {
out.Path = val
}
if val, ok := props["region"].(string); ok {
out.Region = val
}
if val, ok := props["csv.delimiter"].(string); ok {
out.CSVDelimiter = val
}
blob, err := yaml.Marshal(out)
if err != nil {
return "", err
}
p := path.Join("sources", source.Name+".yaml")
// TODO: Use create and createOnly when they're added to repo.Put
if _, err := os.Stat(path.Join(repo.DSN(), p)); err == nil {
if !force {
return "", os.ErrExist
}
}
err = repo.Put(ctx, c.InstanceID, p, bytes.NewReader(blob))
if err != nil {
return "", err
}
return p, nil
}
func (c *Codec) DeleteSource(ctx context.Context, name string) (string, error) {
p := path.Join("sources", name+".yaml")
err := c.Repo.Delete(ctx, c.InstanceID, p)
if err != nil {
return "", err
}
return p, nil
}
func (c *Codec) ProjectConfig(ctx context.Context) (*ProjectConfig, error) {
content, err := c.Repo.Get(ctx, c.InstanceID, "rill.yaml")
// rill.yaml is not guaranteed to exist in case of older projects
if os.IsNotExist(err) {
return &ProjectConfig{Env: make(map[string]string)}, nil
}
if err != nil {
return nil, err
}
r := &ProjectConfig{Env: make(map[string]string)}
if err := yaml.Unmarshal([]byte(content), r); err != nil {
return nil, err
}
return r, nil
}