-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
62 lines (52 loc) · 1.36 KB
/
cache.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
package main
import (
"fmt"
"io/ioutil"
"log"
"cloud.google.com/go/storage"
"golang.org/x/net/context"
sg "google.golang.org/api/storage/v1"
)
const (
projectID = "goa-swagger"
bucketName = "artifacts.goa-swagger.appspot.com"
)
var (
ctx context.Context
bucket *storage.BucketHandle
)
const (
scope = sg.DevstorageReadWriteScope
)
func init() {
client, err := storage.NewClient(context.Background())
if err != nil {
log.Fatalf("Unable to get storage client: %v", err)
}
bucket = client.Bucket(bucketName)
}
// Load attempts to load the swagger spec for the given package and given revision SHA.
// It returns the swagger spec content and true on success, nil and false if not found.
func Load(sha string) ([]byte, error) {
object := bucket.Object(ObjectName(sha))
rc, err := object.NewReader(ctx)
if err != nil {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
}
// Save saves the given swagger spec to the cache.
func Save(b []byte, sha string) error {
object := bucket.Object(ObjectName(sha))
wc := object.NewWriter(ctx)
defer wc.Close()
wc.ContentType = "text/plain"
wc.ACL = []storage.ACLRule{{storage.AllAuthenticatedUsers, storage.RoleOwner}}
_, err := wc.Write(b)
return err
}
// ObjectName returns the cloud storage object name for the given SHA.
func ObjectName(sha string) string {
return fmt.Sprintf("specs/%s", sha)
}