-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
143 lines (130 loc) · 3.41 KB
/
storage.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package storage
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/kobtea/gorgo/log"
"go.uber.org/zap"
)
const (
MetadataDirname = "metadata"
SourceDirname = "src"
RepoFilename = "repo.json"
)
type Storage struct {
workingDir string
gcCandidates []string
logger *zap.SugaredLogger
}
func NewStorage(workingDir string) (*Storage, error) {
s := &Storage{
workingDir: workingDir,
logger: log.GetLogger().Named("storage"),
}
if err := s.prepareGc(); err != nil {
return nil, err
}
s.logger.Debug("initialized storage")
return s, nil
}
func (s *Storage) RepoPath(prefix, baseUrl, owner, repo string) string {
return filepath.Clean(filepath.Join(s.workingDir, prefix, baseUrl, owner, repo))
}
func (s *Storage) ListRepoPaths(prefix, baseUrl, owner string, regex *regexp.Regexp, glob string) ([]string, error) {
var res []string
root := filepath.Join(s.workingDir, prefix, baseUrl, owner)
dirs, err := os.ReadDir(root)
if err != nil {
return []string{}, err
}
for _, dir := range dirs {
if regex.Match([]byte(dir.Name())) {
files, err := filepath.Glob(filepath.Join(root, dir.Name(), glob))
if err != nil {
return []string{}, err
}
res = append(res, files...)
}
}
return res, nil
}
func (s *Storage) ListDirs() ([]string, error) {
dirs, err := filepath.Glob(filepath.Join(s.workingDir, "*/*/*/*"))
if err != nil {
return []string{}, err
}
return dirs, nil
}
func (s *Storage) UpdateRepoMetadata(domain, name, repo string, data []byte) error {
s.logger.Info(fmt.Sprintf("update repo metadata: %s/%s/%s", domain, name, repo))
path := s.RepoPath(MetadataDirname, domain, name, repo)
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(path, RepoFilename), data, 0644); err != nil {
return err
}
s.flagActive(path)
return nil
}
func (s *Storage) UpdateSource(domain, name, repo, cloneUrl, tokenEnvvarName string) error {
s.logger.Info(fmt.Sprintf("update source: %s/%s/%s", domain, name, repo))
token := os.Getenv(tokenEnvvarName)
path := s.RepoPath(SourceDirname, domain, name, repo)
gitRepo, err := git.PlainOpen(path)
if errors.Is(err, git.ErrRepositoryNotExists) {
_, err = git.PlainClone(path, false, &git.CloneOptions{
URL: cloneUrl,
Depth: 1,
Auth: &http.BasicAuth{Username: "username", Password: token},
})
if err != nil {
return err
}
} else if err != nil {
return err
} else {
wt, err := gitRepo.Worktree()
if err != nil {
return err
}
if err = wt.Pull(&git.PullOptions{
Depth: 1,
Auth: &http.BasicAuth{Username: "username", Password: token},
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err
}
}
s.flagActive(path)
return nil
}
func (s *Storage) prepareGc() error {
dirs, err := s.ListDirs()
if err != nil {
return fmt.Errorf("failed preparing for gc: %s", err.Error())
}
s.gcCandidates = dirs
return nil
}
func (s *Storage) flagActive(path string) {
for i, v := range s.gcCandidates {
if v == path {
s.gcCandidates = append(s.gcCandidates[:i], s.gcCandidates[i+1:]...)
break
}
}
}
func (s *Storage) DoGc() error {
for _, path := range s.gcCandidates {
s.logger.Info(fmt.Sprintf("gc: remove unused dir: %s", path))
if err := os.RemoveAll(path); err != nil {
return err
}
// TODO: remove parent directory if empty
}
return nil
}