-
Notifications
You must be signed in to change notification settings - Fork 783
/
dockerfile.go
72 lines (67 loc) · 1.93 KB
/
dockerfile.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 docker
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/pkg/errors"
)
var (
dockerfileNameRegex = regexp.MustCompile(`^(Dockerfile|Dockerfile\..*)$`)
)
//UpdateVersions scans the directory structure rooted in dir for files that match dockerfileNameRegex and replaces any
// lines starting with FROM <name>:, ENV <name> or ARG=<name> with the newVersion
func UpdateVersions(dir string, newVersion string, name string) ([]string, error) {
linePrefixes := []string{
fmt.Sprintf("FROM %s:", name),
fmt.Sprintf("ENV %s ", name),
fmt.Sprintf("ARG %s=", name),
} // #nosec
oldVersions := make(map[string]bool)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Logger().Warnf("looking for Dockerfiles in %s %v", path, err)
} else {
if dockerfileNameRegex.MatchString(filepath.Base(path)) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "reading %s", path)
}
dockerfile := string(bytes)
answer := make([]string, 0)
for _, line := range strings.Split(dockerfile, "\n") {
found := false
for _, prefix := range linePrefixes {
if strings.HasPrefix(line, prefix) {
oldVersions[strings.TrimPrefix(line, prefix)] = true
answer = append(answer, fmt.Sprintf("%s%s", prefix, newVersion))
found = true
continue
}
}
if !found {
answer = append(answer, line)
}
}
err = ioutil.WriteFile(path, []byte(strings.Join(answer, "\n")), info.Mode())
if err != nil {
return errors.Wrapf(err, "writing %s", path)
}
}
}
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "walking %s to file Dockerfiles", dir)
}
answer := make([]string, 0)
for oldVersion := range oldVersions {
answer = append(answer, oldVersion)
}
sort.Strings(answer)
return answer, nil
}