forked from fnet123/ihub
/
oct.go
69 lines (56 loc) · 1.74 KB
/
oct.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
package storage
import (
"fmt"
"path/filepath"
"strings"
beegoContext "github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
dockerContext "github.com/docker/distribution/context"
)
const octPrefix = "oct"
var (
ok []byte
fail []byte
)
func initData() {
var ctx dockerContext.Context
ok, _ = Driver().GetContent(ctx, fmt.Sprintf("%s-icons/ok", octPrefix))
fail, _ = Driver().GetContent(ctx, fmt.Sprintf("%s-icons/fail", octPrefix))
}
func checkData(data []byte) []byte {
if len(ok) == 0 {
initData()
}
content := string(data)
strs := strings.Split(content, "\n")
for end := len(strs) - 1; end >= 0; end-- {
if strings.HasPrefix(strs[end], "Result: PASS") {
return ok
}
}
return fail
}
// GetOctStatus returns the status of a repo
func GetOctStatus(ctx *beegoContext.Context, repo string) ([]byte, error) {
storagePath := fmt.Sprintf("%s/%s/status", octPrefix, repo)
logs.Debug("Get '%s'.", storagePath)
return Driver().GetContent(*BC2DC(ctx), storagePath)
}
// GetOctImage returns the image of a repo
func GetOctImage(ctx *beegoContext.Context, repo string) ([]byte, error) {
storagePath := fmt.Sprintf("%s/%s/image", octPrefix, repo)
logs.Debug("Get '%s'.", storagePath)
return Driver().GetContent(*BC2DC(ctx), storagePath)
}
// AddOctOutput writes the output to storage
func AddOctOutput(ctx *beegoContext.Context, repo string, data []byte) error {
storageDir := fmt.Sprintf("%s/%s", octPrefix, repo)
logs.Debug("AddOctOutput '%s'.", storageDir)
imageData := checkData(data)
err := Driver().PutContent(*BC2DC(ctx), filepath.Join(storageDir, "image"), imageData)
if err != nil {
return err
}
//FIXME: rollback to remove 'image'
return Driver().PutContent(*BC2DC(ctx), filepath.Join(storageDir, "status"), data)
}