-
Notifications
You must be signed in to change notification settings - Fork 634
/
ImageNameFormatter.java
251 lines (202 loc) · 7.83 KB
/
ImageNameFormatter.java
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
245
246
247
248
249
250
251
package io.fabric8.maven.docker.util;
/*
*
* Copyright 2016 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Strings;
import io.fabric8.maven.docker.config.ConfigHelper;
import org.apache.maven.project.MavenProject;
/**
* Replace placeholders in an image name with certain properties found in the
* project
*
* @author roland
* @since 07/06/16
*/
public class ImageNameFormatter implements ConfigHelper.NameFormatter {
public static final String TIMESTAMP_FORMAT = "yyMMdd-HHmmss-SSSS";
private final FormatParameterReplacer formatParamReplacer;
private final Date now;
public ImageNameFormatter(MavenProject project, Date now) {
this.now = now;
formatParamReplacer = new FormatParameterReplacer(initLookups(project));
}
@Override
public String format(String name) {
if (name == null) {
return null;
}
return formatParamReplacer.replace(name);
}
// =====================================================================================
// Lookup classes
private Map<String, FormatParameterReplacer.Lookup> initLookups(final MavenProject project) {
// Sanitized group id
final Map<String, FormatParameterReplacer.Lookup> lookups = new HashMap<>();
lookups.put("g", new DefaultUserLookup(project));
// Sanitized artifact id
lookups.put("a", new DefaultNameLookup(project));
// Various ways for adding a version
lookups.put("v", new DefaultTagLookup(project, DefaultTagLookup.Mode.PLAIN, now));
lookups.put("t", new DefaultTagLookup(project, DefaultTagLookup.Mode.SNAPSHOT_WITH_TIMESTAMP, now));
lookups.put("l", new DefaultTagLookup(project, DefaultTagLookup.Mode.SNAPSHOT_LATEST, now));
// Simple Timestamp
lookups.put("T", new DefaultTimestampLookup(project, now));
return lookups;
}
// ==============================================================================================
public static abstract class AbstractLookup implements FormatParameterReplacer.Lookup {
protected final MavenProject project;
private AbstractLookup(MavenProject project) {
this.project = project;
}
protected String getProperty(String key) {
return project.getProperties().getProperty(key);
}
}
private static class DefaultUserLookup extends AbstractLookup {
/**
* Property to lookup for image user which overwrites the calculated default (group).
* Used with format modifier %g
*/
private static final String DOCKER_IMAGE_USER = "docker.image.user";
private DefaultUserLookup(MavenProject project) {
super(project);
}
public String lookup() {
String user = getProperty(DOCKER_IMAGE_USER);
if (user != null) {
return user;
}
String groupId = project.getGroupId();
while (groupId.endsWith(".")) {
groupId = groupId.substring(0, groupId.length() - 1);
}
int idx = groupId.lastIndexOf(".");
return sanitizeName(groupId.substring(idx != -1 ? idx + 1 : 0));
}
}
private static class DefaultNameLookup extends AbstractLookup {
private DefaultNameLookup(MavenProject project) {
super(project);
}
public String lookup() {
return sanitizeName(project.getArtifactId());
}
}
private static class DefaultTagLookup extends AbstractLookup {
/**
* Property to lookup for image name which overwrites the calculated default, which is calculated
* on the project version and depends whether it is a snapshot project or not.
* Used with format modifier %v
*/
private static final String DOCKER_IMAGE_TAG = "docker.image.tag";
// how to resolve the version
private final Mode mode;
// timestamp indicating now
private final Date now;
private enum Mode {
PLAIN('v') {
public String doTransform(String tag, Date now) {
return tag;
}
},
SNAPSHOT_WITH_TIMESTAMP('t') {
public String doTransform(String tag, Date now) {
if (tag.endsWith("-SNAPSHOT")) {
return "snapshot-" + new SimpleDateFormat(TIMESTAMP_FORMAT).format(now);
}
return tag;
}
},
SNAPSHOT_LATEST('l') {
public String doTransform(String tag, Date now) {
if (tag.endsWith("-SNAPSHOT")) {
return "latest";
}
return tag;
}
};
private final char letter;
Mode(char letter) {
this.letter = letter;
}
protected abstract String doTransform(String tag, Date now);
public String transform(MavenProject project, String tag, Date now) {
// In case the Maven property is also a placeholder, replace it as well
if (Strings.isNullOrEmpty(tag) || tag.equals("%" + letter)) {
tag = project.getVersion();
}
return doTransform(tag, now);
}
}
private DefaultTagLookup(MavenProject project, Mode mode, Date now) {
super(project);
this.mode = mode;
this.now = now;
}
public String lookup() {
String tag = getProperty(DOCKER_IMAGE_TAG);
return mode.transform(project, tag, now);
}
}
private static class DefaultTimestampLookup extends AbstractLookup {
// timestamp indicating now
private final Date now;
private DefaultTimestampLookup(MavenProject project, Date now) {
super(project);
this.now = now;
}
public String lookup() {
return new SimpleDateFormat(TIMESTAMP_FORMAT).format(now);
}
}
// ==========================================================================================
// See also ImageConfiguration#doValidate()
private static String sanitizeName(String name) {
StringBuilder ret = new StringBuilder();
int underscores = 0;
boolean lastWasADot = false;
for (char c : name.toCharArray()) {
if (c == '_') {
underscores++;
// Only _ in a row are allowed
if (underscores <= 2) {
ret.append(c);
}
continue;
}
if (c == '.') {
// Only one dot in a row is allowed
if (!lastWasADot) {
ret.append(c);
}
lastWasADot = true;
continue;
}
underscores = 0;
lastWasADot = false;
if (Character.isLetter(c) || Character.isDigit(c) || c == '-') {
ret.append(c);
}
}
// All characters must be lowercase
return ret.toString().toLowerCase();
}
}