Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/spicedb/schema.go
2492 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package spicedb
6
7
import (
8
"embed"
9
"fmt"
10
"io/fs"
11
"sort"
12
"strings"
13
14
"gopkg.in/yaml.v2"
15
)
16
17
//go:embed schema/*.yaml
18
var bootstrapFiles embed.FS
19
20
type File struct {
21
Name string
22
Data string
23
}
24
25
type SpiceDBSchema struct {
26
Schema string `yaml:"schema"`
27
Relationships string `yaml:"relationships"`
28
}
29
30
func GetBootstrapFiles() ([]File, error) {
31
files, err := fs.ReadDir(bootstrapFiles, "schema")
32
if err != nil {
33
return nil, fmt.Errorf("failed to read bootstrap files: %w", err)
34
}
35
36
var filesWithContents []File
37
for _, f := range files {
38
b, err := fs.ReadFile(bootstrapFiles, fmt.Sprintf("%s/%s", "schema", f.Name()))
39
if err != nil {
40
return nil, err
41
}
42
43
var schema SpiceDBSchema
44
err = yaml.Unmarshal(b, &schema)
45
if err != nil {
46
return nil, fmt.Errorf("failed to parse file %s as yaml: %w", f.Name(), err)
47
}
48
49
data, err := yaml.Marshal(SpiceDBSchema{
50
Schema: schema.Schema,
51
})
52
if err != nil {
53
return nil, fmt.Errorf("failed to serialize contents: %w", err)
54
}
55
56
// We only want to populate spicedb with the schema - we don't want to persist relationships or other data
57
// This is because the relationships defined in this schema are used for validation, but can also be used to
58
// import data into a running instance - we do not want that.
59
// We cannot split the definitions across multiple files as that would prevent us from performing CI validation,
60
// and we do not want to duplicate the schema.
61
filesWithContents = append(filesWithContents, File{
62
Name: f.Name(),
63
Data: string(data),
64
})
65
}
66
67
// ensure output is stable
68
sort.Slice(filesWithContents, func(i, j int) bool {
69
return strings.Compare(filesWithContents[i].Name, filesWithContents[j].Name) == -1
70
})
71
72
return filesWithContents, nil
73
}
74
75