This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
/
repository.go
130 lines (110 loc) · 3.01 KB
/
repository.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
package catalog
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"mkdeb.sh/recipe"
)
// Repository is a recipes repository.
type Repository struct {
Name string
URL string
Branch string
Path string
}
// NewRepository creates a new recipes repository instance.
func NewRepository(path, name, url, branch string) *Repository {
return &Repository{
Name: name,
URL: url,
Branch: branch,
Path: path,
}
}
// NewRepositoryFromPath creates a new recipes repository instance from an existing path.
func NewRepositoryFromPath(path string) (*Repository, error) {
repo, err := git.PlainOpen(path)
if err != nil {
return nil, fmt.Errorf("cannot open repository: %w", err)
}
remote, err := repo.Remote("origin")
if err != nil {
return nil, fmt.Errorf("cannot get remote: %w", err)
}
remoteCfg := remote.Config()
return &Repository{
Name: path[strings.Index(path, "repositories/")+13:],
URL: remoteCfg.URLs[0],
Branch: filepath.Base(remoteCfg.Fetch[0].Src()),
Path: path,
}, nil
}
// Recipe loads a recipe from the repository.
func (r *Repository) Recipe(name string) (*recipe.Recipe, error) {
return recipe.LoadRecipe(filepath.Join(r.Path, string(name[0]), name))
}
// Update updates the repository from the remote.
func (r *Repository) Update(force bool) error {
// Clone if repository doesn't exist
_, err := os.Stat(r.Path)
if os.IsNotExist(err) {
_, err := git.PlainClone(r.Path, false, &git.CloneOptions{
URL: r.URL,
ReferenceName: plumbing.ReferenceName("refs/heads/" + r.Branch),
SingleBranch: true,
Depth: 1,
Progress: os.Stdout,
})
if err != nil {
return fmt.Errorf("cannot clone repository: %w", err)
}
return nil
}
// Pull changes from remote
repo, err := git.PlainOpen(r.Path)
if err != nil {
return fmt.Errorf("cannot open repository: %w", err)
}
wt, err := repo.Worktree()
if err != nil {
return fmt.Errorf("cannot get worktree: %w", err)
}
// TODO: implement force (git checkout -f && git clean -d -f)
err = wt.Pull(&git.PullOptions{
Depth: 1,
Progress: os.Stdout,
})
if err == git.NoErrAlreadyUpToDate {
return ErrAlreadyUpToDate
} else if err != nil {
return fmt.Errorf("cannot pull repository: %w", err)
}
return nil
}
// Walk walks the repository calling a function for each recipe found.
func (r *Repository) Walk(f func(recipe *recipe.Recipe, err error) error) error {
repo, err := git.PlainOpen(r.Path)
if err != nil {
return fmt.Errorf("cannot open repository: %w", err)
}
idx, err := repo.Storer.Index()
if err != nil {
return fmt.Errorf("cannot get index: %w", err)
}
for _, entry := range idx.Entries {
if strings.HasSuffix(entry.Name, "/recipe.yaml") {
recipe, err := r.Recipe(filepath.Base(filepath.Dir(entry.Name)))
if err != nil {
return fmt.Errorf("cannot load %q recipe: %w", filepath.Join(r.Path, entry.Name), err)
}
err = f(recipe, err)
if err != nil {
return err
}
}
}
return nil
}