-
Notifications
You must be signed in to change notification settings - Fork 25
/
imagelog.go
78 lines (64 loc) · 1.54 KB
/
imagelog.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
70
71
72
73
74
75
76
77
78
package clients
import (
"bufio"
"fmt"
"os"
"strings"
)
// ImageTypeDocker defines a type for a Docker image
const ImageTypeDocker string = "Docker"
// ImageLog logs machine images to make cleanup possible
type ImageLog interface {
Log(string, string) error
Read(string) ([]string, error)
Clear() error
}
type ImageFileLog struct {
f string
}
// NewImageFileLog creates an ImageLog which uses a file as the underlying
// Datastore
func NewImageFileLog(file string) *ImageFileLog {
return &ImageFileLog{file}
}
// Log an image has been downloaded by Shypyard
func (i *ImageFileLog) Log(name, t string) error {
// check the existing entries do not add if allready in there
// ignore errors as the file may not exist
entries, _ := i.Read(t)
for _, v := range entries {
// found just exit
if v == name {
return nil
}
}
f, err := os.OpenFile(i.f, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf("%s,%s\n", name, t))
return err
}
// Read a list of images which have been downloaded by Shipyard
func (i *ImageFileLog) Read(t string) ([]string, error) {
f, err := os.Open(i.f)
if err != nil {
return nil, err
}
defer f.Close()
output := []string{}
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ",")
if parts[1] == t {
output = append(output, parts[0])
}
}
return output, nil
}
// Clear the list of images
func (i *ImageFileLog) Clear() error {
return os.Remove(i.f)
}