/
manifest.go
53 lines (42 loc) · 1.24 KB
/
manifest.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
package db
import (
"bytes"
"encoding/json"
"github.com/crazy-max/diun/v3/pkg/registry"
bolt "go.etcd.io/bbolt"
)
// First checks if a Docker image has ever been analyzed
func (c *Client) First(image registry.Image) (bool, error) {
found := false
err := c.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketManifest)).Cursor()
name := []byte(image.Name())
for k, _ := c.Seek(name); k != nil && bytes.HasPrefix(k, name); k, _ = c.Next() {
found = true
return nil
}
return nil
})
return !found, err
}
// GetManifest returns Docker image manifest
func (c *Client) GetManifest(image registry.Image) (registry.Manifest, error) {
var manifest registry.Manifest
err := c.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketManifest))
if entryBytes := b.Get([]byte(image.String())); entryBytes != nil {
return json.Unmarshal(entryBytes, &manifest)
}
return nil
})
return manifest, err
}
// PutManifest add Docker image manifest in db
func (c *Client) PutManifest(image registry.Image, manifest registry.Manifest) error {
entryBytes, _ := json.Marshal(manifest)
err := c.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketManifest))
return b.Put([]byte(image.String()), entryBytes)
})
return err
}