-
Notifications
You must be signed in to change notification settings - Fork 2
/
images.go
50 lines (41 loc) · 1.16 KB
/
images.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
package db
import (
"github.com/jinzhu/gorm"
)
// Image represents cached image information
type Image struct {
gorm.Model
Tag string `gorm:"index:tag;"`
Action string `gorm:"index:action;"`
Owner string
}
// ImageAction defines how handle a image
type ImageAction string
// ImageActions
const (
ImageActionPulling ImageAction = "pulling"
ImageActionBuilding ImageAction = "building"
)
// Create persists its data to database
func (image *Image) Create() error {
return db.Create(image).Error
}
// FindImages search images based on the ImageType
func FindImages(action ImageAction) ([]*Image, error) {
result := []*Image{}
if err := db.Where("action = ?", action).Find(&result).Error; err != nil {
return nil, err
}
return result, nil
}
// RemovePullingImage removes from PULLING cache
func RemovePullingImage(name string) error {
return removeImage(name, ImageActionPulling)
}
// RemoveBuildingImage removes from BUILDING cache
func RemoveBuildingImage(name string) error {
return removeImage(name, ImageActionBuilding)
}
func removeImage(tag string, action ImageAction) error {
return db.Delete(Image{}, "tag = ? and action = ?", tag, action).Error
}