-
Notifications
You must be signed in to change notification settings - Fork 199
/
git_repo.go
74 lines (59 loc) · 1.67 KB
/
git_repo.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
package gitdata
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/werf/werf/pkg/util/timestamps"
"github.com/werf/werf/pkg/volumeutils"
)
type GitRepoDesc struct {
Path string
LastAccessAt time.Time
Size uint64
}
func (entry *GitRepoDesc) GetPaths() []string {
return []string{entry.Path}
}
func (entry *GitRepoDesc) GetSize() uint64 {
return entry.Size
}
func (entry *GitRepoDesc) GetLastAccessAt() time.Time {
return entry.LastAccessAt
}
func GetExistingGitRepos(cacheVersionRoot string) ([]*GitRepoDesc, error) {
var res []*GitRepoDesc
if _, err := os.Stat(cacheVersionRoot); os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("error accessing dir %q: %s", cacheVersionRoot, err)
}
files, err := ioutil.ReadDir(cacheVersionRoot)
if err != nil {
return nil, fmt.Errorf("error reading dir %q: %s", cacheVersionRoot, err)
}
for _, finfo := range files {
repoPath := filepath.Join(cacheVersionRoot, finfo.Name())
if !finfo.IsDir() {
if err := os.RemoveAll(repoPath); err != nil {
return nil, fmt.Errorf("unable to remove %q: %s", repoPath, err)
}
}
size, err := volumeutils.DirSizeBytes(repoPath)
if err != nil {
return nil, fmt.Errorf("error getting dir %q size: %s", repoPath, err)
}
lastAccessAtPath := filepath.Join(repoPath, "last_access_at")
lastAccessAt, err := timestamps.ReadTimestampFile(lastAccessAtPath)
if err != nil {
return nil, fmt.Errorf("error reading last access timestamp file %q: %s", lastAccessAtPath, err)
}
res = append(res, &GitRepoDesc{
Path: repoPath,
Size: size,
LastAccessAt: lastAccessAt,
})
}
return res, nil
}