-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.go
65 lines (49 loc) · 1.45 KB
/
build.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
package store
import (
"context"
"fmt"
"sort"
"cloud.google.com/go/datastore"
pb "github.com/datacol-io/datacol/api/models"
)
func (g *DSBackend) BuildGet(app, id string) (*pb.Build, error) {
var b pb.Build
ctx, key := g.nestedKey(buildKind, id)
if err := g.datastore().Get(ctx, key, &b); err != nil {
return nil, fmt.Errorf("fetching build: %v", err)
}
return &b, nil
}
func (g *DSBackend) BuildDelete(app, id string) error {
ctx, key := g.nestedKey(buildKind, id)
return g.datastore().Delete(ctx, key)
}
func (g *DSBackend) BuildList(app string, limit int64) (pb.Builds, error) {
q := datastore.NewQuery(buildKind).Namespace(g.DeploymentName).
Filter("app = ", app)
// Limit(limit).
// Order("-" + "created_at") // FIXME:: need compound for created_at in desc order
var builds pb.Builds
_, err := g.datastore().GetAll(context.Background(), q, &builds)
sort.Slice(builds, func(i, j int) bool {
return builds[i].CreatedAt > builds[j].CreatedAt
})
size := int64(len(builds))
if limit > 0 && size < limit {
limit = size
}
return builds[0:limit], err
}
func (g *DSBackend) BuildCreate(app string, req *pb.CreateBuildOptions) (*pb.Build, error) {
return nil, notImplemented
}
func (g *DSBackend) BuildSave(b *pb.Build) error {
if b.Id == "" {
b.Id = generateId("B", 5)
}
ctx, key := g.nestedKey(buildKind, b.Id)
if _, err := g.datastore().Put(ctx, key, b); err != nil {
return fmt.Errorf("saving build err: %v", err)
}
return nil
}