/
updater.go
53 lines (47 loc) 路 963 Bytes
/
updater.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
package updater
import (
"fmt"
"path"
"regexp"
)
type FilesUpdater interface {
Init(map[string]string) error
Name() string
Version() string
ForFiles() string
Apply(file, newVersion string) error
}
type ChainedUpdater struct {
Updaters []FilesUpdater
}
func (u *ChainedUpdater) Init(conf map[string]string) error {
for _, fu := range u.Updaters {
err := fu.Init(conf)
if err != nil {
return err
}
}
return nil
}
func (u *ChainedUpdater) GetNameVersionPairs() []string {
ret := make([]string, len(u.Updaters))
for i, fu := range u.Updaters {
ret[i] = fmt.Sprintf("%s@%s", fu.Name(), fu.Version())
}
return ret
}
func (u *ChainedUpdater) Apply(file, newVersion string) error {
for _, fu := range u.Updaters {
re, err := regexp.Compile(fu.ForFiles())
if err != nil {
return err
}
if !re.MatchString(path.Base(file)) {
continue
}
if err := fu.Apply(file, newVersion); err != nil {
return err
}
}
return nil
}