-
Notifications
You must be signed in to change notification settings - Fork 342
/
samplecontent.go
127 lines (113 loc) · 3.44 KB
/
samplecontent.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
package samplerepo
import (
"bufio"
"bytes"
"context"
"io"
"io/fs"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/go-openapi/swag"
"github.com/treeverse/lakefs/pkg/auth/model"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/samplerepo/assets"
"github.com/treeverse/lakefs/pkg/upload"
)
const (
sampleRepoFSRootPath = "sample"
sampleRepoCommitMsg = "Add sample data"
)
func PopulateSampleRepo(ctx context.Context, repo *catalog.Repository, cat *catalog.Catalog, pathProvider upload.PathProvider, blockAdapter block.Adapter, user *model.User) error {
// upload sample data
// we skip checking if the repo and branch exist, since we just created them
// we also skip checking if the file exists, since we know the repo is empty
const tmplSuffix = ".tmpl"
config := map[string]string{
"RepoName": repo.Name,
}
err := fs.WalkDir(assets.SampleData, sampleRepoFSRootPath, func(p string, d fs.DirEntry, topLevelErr error) error {
// handle a top-level error
if topLevelErr != nil {
return topLevelErr
}
if d.IsDir() {
// noop for directories
return nil
}
var (
contentPath string
contentReader io.Reader
contentSize int64
)
if filepath.Ext(p) == tmplSuffix {
tmpl, err := template.ParseFS(assets.SampleData, p)
if err != nil {
return err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, config); err != nil {
return err
}
contentPath = strings.TrimSuffix(p, tmplSuffix)
contentReader = bufio.NewReader(&buf)
contentSize = int64(buf.Len())
} else {
// open file from embedded FS
file, err := assets.SampleData.Open(p)
if err != nil {
return err
}
// embed file close does nothing, we just like to keep it aligned with the open
defer func() { _ = file.Close() }()
fileStat, err := d.Info()
if err != nil {
return err
}
contentPath = p
contentReader = file
contentSize = fileStat.Size()
}
// write file to storage
address := pathProvider.NewPath()
blob, err := upload.WriteBlob(ctx, blockAdapter, repo.StorageNamespace, address, contentReader, contentSize, block.PutOpts{})
if err != nil {
return err
}
// create metadata entry
writeTime := time.Now()
entry := catalog.NewDBEntryBuilder().
Path(strings.TrimPrefix(contentPath, sampleRepoFSRootPath+"/")).
PhysicalAddress(blob.PhysicalAddress).
CreationDate(writeTime).
Size(blob.Size).
Checksum(blob.Checksum).
AddressType(catalog.AddressTypeRelative).
Build()
// write metadata entry
err = cat.CreateEntry(ctx, repo.Name, repo.DefaultBranch, entry)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
// if we succeeded, commit the changes
// commit changes
_, err = cat.Commit(ctx, repo.Name, repo.DefaultBranch, sampleRepoCommitMsg,
user.Username, map[string]string{}, swag.Int64(time.Now().Unix()), nil, false)
return err
}
func AddBranchProtection(ctx context.Context, repo *catalog.Repository, cat *catalog.Catalog) error {
return cat.SetBranchProtectionRules(ctx, repo.Name, &graveler.BranchProtectionRules{
BranchPatternToBlockedActions: map[string]*graveler.BranchProtectionBlockedActions{
repo.DefaultBranch: {Value: []graveler.BranchProtectionBlockedAction{graveler.BranchProtectionBlockedAction_COMMIT,
graveler.BranchProtectionBlockedAction_STAGING_WRITE}},
},
}, swag.String(""))
}