forked from openshift/ci-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bumper.go
110 lines (89 loc) · 2.59 KB
/
bumper.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
package bumper
import (
"errors"
"fmt"
"os"
"path"
"github.com/sirupsen/logrus"
"github.com/openshift/ci-tools/pkg/util"
)
type Bumper[T any] interface {
GetFiles() ([]string, error)
Unmarshall(file string) (T, error)
BumpFilename(filename string, obj T) (string, error)
BumpContent(obj T) (T, error)
Marshall(obj T, bumpedFilename, dir string) error
}
type BumpingOptions struct {
OutDir string
}
// Bump bumps files using the Bumpers b according to the BumpingOptions.
func Bump[T any](b Bumper[T], o *BumpingOptions) error {
filesCh := make(chan string)
produce := func() error {
defer close(filesCh)
files, err := b.GetFiles()
logrus.Debugf("files: %+v", files)
if err != nil {
return err
}
for _, f := range files {
filesCh <- f
}
return nil
}
errsChan := make(chan error)
map_ := func() error {
for f := range filesCh {
if err := BumpObject(b, f, o.OutDir); err != nil {
errsChan <- err
}
}
return nil
}
return util.ProduceMap(0, produce, map_, errsChan)
}
func BumpObject[T any](b Bumper[T], file, outDir string) error {
logrus.Infof("bumping config %s", file)
srcFileFullPath := file
obj, err := b.Unmarshall(srcFileFullPath)
if err != nil {
logrus.WithError(err).Errorf("failed to unmarshall file %s", srcFileFullPath)
return fmt.Errorf("unmarshall file %s: %w", file, err)
}
filename := path.Base(file)
logrus.Infof("bumping filename %s", filename)
bumpedFilename, err := b.BumpFilename(filename, obj)
if err != nil {
logrus.WithError(err).Errorf("error bumping file %s", bumpedFilename)
return fmt.Errorf("bump filename: %w", err)
}
logrus.Infof("bumped filename %s", bumpedFilename)
outDir = getOutDir(file, outDir)
logrus.Debugf("out dir: %s", outDir)
dstFileFullPath := path.Join(outDir, bumpedFilename)
if _, err := os.Stat(dstFileFullPath); err == nil {
logrus.WithError(err).Warnf("file %s already exists, skipping", dstFileFullPath)
return fmt.Errorf("file %s already exists", dstFileFullPath)
} else if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("file exists: %w", err)
}
logrus.Infof("bumping obj")
bumpedObj, err := b.BumpContent(obj)
if err != nil {
logrus.WithError(err).Error("error bumping obj")
return fmt.Errorf("bump object: %w", err)
}
logrus.Infof("marshalling obj %s to %s", bumpedFilename, outDir)
if err := b.Marshall(bumpedObj, bumpedFilename, outDir); err != nil {
logrus.WithError(err).Error("error marshalling2 obj")
return fmt.Errorf("marshall obj: %w", err)
}
return nil
}
func getOutDir(file string, dir string) string {
if dir != "" {
return dir
}
return path.Dir(file)
}