-
Notifications
You must be signed in to change notification settings - Fork 3
/
archive_generator.go
72 lines (59 loc) · 1.5 KB
/
archive_generator.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
package main
import (
"crypto/sha1"
"fmt"
)
type ArchiveGenerator struct {
repositoryStore RepositoryStore
requestQueue chan *ArchiveRequest
}
func (g *ArchiveGenerator) GenerateArchive(path, ref, prefix, format string) (string, error) {
repository, err := g.repositoryStore.GetRepository(path)
if err != nil {
return "", REPOSITORY_NOT_FOUND
}
commit, err := repository.ResolveRef(ref)
if err != nil {
return "", REF_NOT_FOUND
}
request := g.newArchiveRequest(repository.Path(), commit, prefix, format)
g.requestQueue <- request
result := <-request.ResultChan
return result.Path, result.Error
}
func (g *ArchiveGenerator) newArchiveRequest(repoPath, commit, prefix, format string) *ArchiveRequest {
return NewArchiveRequest(NewArchiveJob(repoPath, commit, prefix, format))
}
type ArchiveRequest struct {
Job *ArchiveJob
ResultChan chan *ArchiveResult
}
func NewArchiveRequest(job *ArchiveJob) *ArchiveRequest {
return &ArchiveRequest{
Job: job,
ResultChan: make(chan *ArchiveResult),
}
}
type ArchiveJob struct {
RepoPath string
Commit string
Prefix string
Format string
Filename string
Result *ArchiveResult
}
func NewArchiveJob(repoPath, commit, prefix, format string) *ArchiveJob {
job := &ArchiveJob{
RepoPath: repoPath,
Commit: commit,
Prefix: prefix,
Format: format,
}
data := []byte(repoPath + commit + prefix)
job.Filename = fmt.Sprintf("%x.%v", sha1.Sum(data), format)
return job
}
type ArchiveResult struct {
Path string
Error error
}