-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
244 lines (214 loc) · 6.18 KB
/
update.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package update
import (
"fmt"
"path/filepath"
"sync"
"github.com/kick-project/kick/internal/resources/client"
"github.com/kick-project/kick/internal/resources/config"
"github.com/kick-project/kick/internal/resources/errs"
"github.com/kick-project/kick/internal/resources/logger"
"github.com/kick-project/kick/internal/resources/marshal"
"github.com/kick-project/kick/internal/resources/model"
_ "github.com/mattn/go-sqlite3" // Required by 'database/sql'
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Update build metadata
//go:generate ifacemaker -f update.go -s Update -p update -i UpdateIface -o update_interfaces.go -c "AUTO GENERATED. DO NOT EDIT."
type Update struct {
client *client.Client
configFile *config.File `validate:"required"`
err errs.HandlerIface `validate:"required"`
orm *gorm.DB `validate:"required"`
log logger.OutputIface `validate:"required"`
metadataDir string `validate:"required"`
}
// Options constructor options
type Options struct {
Client *client.Client `validate:"required"`
ConfigFile *config.File `validate:"required"`
Err errs.HandlerIface `validate:"required"`
ORM *gorm.DB `validate:"required"`
Log logger.OutputIface `validate:"required"`
MetadataDir string `validate:"required"`
}
// New constructor
func New(opts *Options) *Update {
return &Update{
client: opts.Client,
configFile: opts.ConfigFile,
err: opts.Err,
orm: opts.ORM,
log: opts.Log,
metadataDir: opts.MetadataDir,
}
}
// Build metadata. Conf defaults to globals.Config if Conf is nil.
func (m *Update) Build() error {
conf := m.configFile
c := workers{
client: m.client,
err: m.err,
wait: &sync.WaitGroup{},
log: m.log,
}
churl := make(chan string, 64)
chtemplates := make(chan *Template, 64)
c.concurClones(6, churl, chtemplates)
c.concurInserts(m.orm, chtemplates)
for _, url := range conf.RepoURLs {
c.wait.Add(1)
churl <- url
}
// Wait for all all processing to finish
c.wait.Wait()
return nil
}
type workers struct {
client *client.Client
err errs.HandlerIface
log logger.OutputIface
wait *sync.WaitGroup
}
// concurClones concurrent cloning of git repositories.
// where num is the number of concurrent downloads, churl is a string url and tchan is a channel of resulting templates.
func (c *workers) concurClones(num int, churl <-chan string, tchan chan<- *Template) {
for i := 0; i < num; i++ {
go func() {
for {
url, ok := <-churl
switch {
case !ok:
return
default:
c.processURL(url, tchan)
c.wait.Done()
}
}
}()
}
}
func (c *workers) processURL(url string, chtemplate chan<- *Template) {
p, err := c.client.GetRepo(url, "")
if c.err.LogF(`error cloning "%s": %w`, url, err) {
return
}
localpath := p.Path()
mpath := filepath.Clean(fmt.Sprintf("%s/repo.yml", localpath))
if c.err.LogF("error: can not open %s: %w: skipping %s", mpath, err, url) {
return
}
repo := &Repo{URL: url}
err = repo.Load(mpath)
if c.err.LogF("error: %w: skipping %s\n", err, url) {
return
}
paths, err := filepath.Glob(filepath.Clean(fmt.Sprintf("%s/templates/*.yml", localpath)))
if c.err.LogF("error: getting a lists of paths: %w: skipping %s\n", err, url) {
return
}
for _, curpath := range paths {
t := &Template{}
err := t.Load(curpath)
if c.err.LogF("error: loading template metadata from %s: %w: skipping", curpath, err) {
continue
}
t.Repo = *repo
c.wait.Add(1)
chtemplate <- t
}
}
// concurInserts populates the database
// where num is the number of concurrent routines
// and ch is the channel to read templates from.
func (c *workers) concurInserts(orm *gorm.DB, ch <-chan *Template) {
go func() {
for {
t, ok := <-ch
switch {
case !ok:
return
default:
c.insert(orm, t)
c.wait.Done()
}
}
}()
}
func (c *workers) insert(orm *gorm.DB, t *Template) {
modRepo := model.Repo{
Name: t.Repo.Name,
URL: t.Repo.URL,
Desc: t.Repo.Description,
}
result := orm.Clauses(clause.Insert{Modifier: "OR IGNORE"}).Create(&modRepo)
if result.RowsAffected != 1 {
result = orm.First(&modRepo, "url = ?", t.Repo.URL)
if result.Error != nil {
c.err.Panic(result.Error)
}
modRepo.Name = t.Repo.Name
modRepo.URL = t.Repo.URL
modRepo.Desc = t.Repo.Description
orm.Model(&modRepo).Updates(&modRepo)
}
modTemplate := model.Template{
Name: t.Name,
URL: t.URL,
Desc: t.Description,
Repo: []model.Repo{modRepo},
}
result = orm.Clauses(clause.Insert{Modifier: "OR IGNORE"}).Create(&modTemplate)
if result.RowsAffected != 1 {
result = orm.First(&modTemplate, "url = ?", t.URL)
if result.Error != nil {
c.err.Panic(result.Error)
}
modTemplate.Name = t.Name
modTemplate.URL = t.URL
modTemplate.Desc = t.Description
modTemplate.Repo = append(modTemplate.Repo, modRepo)
orm.Model(&modTemplate).Updates(&modTemplate)
}
modVersion := model.Versions{
Template: modTemplate,
}
for _, version := range t.Versions {
modVersion.ID = 0
modVersion.Version = version
result = orm.Clauses(clause.Insert{Modifier: "OR IGNORE"}).Create(&modVersion)
if result.Error != nil {
c.err.Panic(result.Error)
}
}
}
// Repo is the repo struct
type Repo struct {
Name string `json:"name" yaml:"name"`
URL string `json:"url" yaml:"url"`
Description string `json:"description" yaml:"description"`
}
// Load loads from a json or yaml file, depending on the file suffix.
func (m *Repo) Load(path string) error {
return marshal.FromFile(m, path)
}
// Save saves to json or yaml file, depending on the file suffix.
func (m *Repo) Save(path string) error {
return marshal.ToFile(m, path)
}
// Template is a template creator
type Template struct {
Name string `json:"name" yaml:"name"`
URL string `json:"url" yaml:"url"`
Description string `json:"description" yaml:"description"`
Versions []string `json:"versions" yaml:"versions"`
Repo Repo
}
// Load loads from a json or yaml file
func (g *Template) Load(path string) error {
return marshal.FromFile(g, path)
}
// Save saves to json or yaml file.
func (g *Template) Save(path string) error {
return marshal.ToFile(g, path)
}