-
Notifications
You must be signed in to change notification settings - Fork 582
/
docker.go
167 lines (145 loc) · 3.8 KB
/
docker.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
package versioning
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// Docker defines an artifact based on a Dockerfile
type Docker struct {
artifact Artifact
content []byte
utils Utils
options *Options
path string
versionSource string
versioningScheme string
readFile func(string) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
}
func (d *Docker) init() {
if d.readFile == nil {
d.readFile = os.ReadFile
}
if d.writeFile == nil {
d.writeFile = os.WriteFile
}
}
func (d *Docker) initDockerfile() {
if len(d.path) == 0 {
d.path = "Dockerfile"
}
}
// VersioningScheme returns the relevant versioning scheme
func (d *Docker) VersioningScheme() string {
if len(d.versioningScheme) == 0 {
return "docker"
}
return d.versioningScheme
}
// GetVersion returns the current version of the artifact
func (d *Docker) GetVersion() (string, error) {
d.init()
var err error
switch d.versionSource {
case "FROM":
var err error
d.initDockerfile()
d.content, err = d.readFile(d.path)
if err != nil {
return "", errors.Wrapf(err, "failed to read file '%v'", d.path)
}
version := d.versionFromBaseImageTag()
if len(version) == 0 {
return "", fmt.Errorf("no version information available in FROM statement")
}
return version, nil
case "":
if len(d.path) == 0 {
d.path = "VERSION"
}
d.versionSource = "custom"
fallthrough
case "custom", "dub", "golang", "maven", "mta", "npm", "pip", "sbt":
if d.options == nil {
d.options = &Options{}
}
d.artifact, err = GetArtifact(d.versionSource, d.path, d.options, d.utils)
if err != nil {
return "", err
}
return d.artifact.GetVersion()
default:
d.initDockerfile()
d.content, err = d.readFile(d.path)
if err != nil {
return "", errors.Wrapf(err, "failed to read file '%v'", d.path)
}
version := d.versionFromEnv(d.versionSource)
if len(version) == 0 {
return "", fmt.Errorf("no version information available in ENV '%v'", d.versionSource)
}
return version, nil
}
}
// SetVersion updates the version of the artifact
func (d *Docker) SetVersion(version string) error {
d.init()
dir := ""
if d.artifact != nil {
err := d.artifact.SetVersion(version)
if err != nil {
return err
}
dir = filepath.Dir(d.path)
}
err := d.writeFile(filepath.Join(dir, "VERSION"), []byte(version), 0700)
if err != nil {
return errors.Wrap(err, "failed to write file 'VERSION'")
}
return nil
}
func (d *Docker) versionFromEnv(env string) string {
lines := strings.Split(string(d.content), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "ENV") && strings.Fields(line)[1] == env {
return strings.Fields(line)[2]
}
}
return ""
}
func (d *Docker) versionFromBaseImageTag() string {
lines := strings.Split(string(d.content), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "FROM") {
imageParts := strings.Split(line, ":")
partsCount := len(imageParts)
if partsCount == 1 {
return ""
}
version := imageParts[partsCount-1]
return strings.TrimSpace(version)
}
}
return ""
}
// GetCoordinates returns the coordinates
func (d *Docker) GetCoordinates() (Coordinates, error) {
result := Coordinates{}
result.GroupID = ""
result.ArtifactID, _ = d.GetArtifactID()
result.Version = ""
// cannot properly resolve version unless all options are provided. Can we ensure proper parameterization?
// result.Version, err = d.GetVersion()
// if err != nil {
// return nil, err
// }
return result, nil
}
// GetArtifactID returns the current ID of the artifact
func (d *Docker) GetArtifactID() (string, error) {
d.init()
artifactID := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(d.options.DockerImage, "/", "_"), ":", "_"), ".", "_")
return artifactID, nil
}