forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfileStatement.java
More file actions
225 lines (170 loc) · 6.55 KB
/
Copy pathDockerfileStatement.java
File metadata and controls
225 lines (170 loc) · 6.55 KB
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
package com.github.dockerjava.core.dockerfile;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import com.github.dockerjava.api.exception.DockerClientException;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
/**
* A statement present in a dockerfile.
*/
public abstract class DockerfileStatement<T extends DockerfileStatement<?>> {
private DockerfileStatement() {
}
public T transform(Map<String, String> env) {
return (T) this;
}
protected String filterForEnvironmentVars(Map<String, String> environmentMap, String extractedResource) {
if (environmentMap.size() > 0) {
String currentResourceContent = extractedResource;
for (Map.Entry<String, String> entry : environmentMap.entrySet()) {
String variable = entry.getKey();
String replacementValue = entry.getValue();
// handle: $VARIABLE case
currentResourceContent = currentResourceContent.replaceAll("\\$" + variable,
Matcher.quoteReplacement(replacementValue));
// handle ${VARIABLE} case
currentResourceContent = currentResourceContent.replaceAll("\\$\\{" + variable + "\\}",
Matcher.quoteReplacement(replacementValue));
}
return currentResourceContent;
} else {
return extractedResource;
}
}
/**
* A statement that we don't particularly care about.
*/
public static class OtherLine extends DockerfileStatement {
public final String statement;
public OtherLine(String statement) {
this.statement = statement;
}
@Override
public String toString() {
return statement;
}
}
/**
* An ADD or a COPY
*/
public static class Add extends DockerfileStatement<Add> {
private static final Pattern ARGUMENT_TOKENIZER = Pattern.compile("(?:\"[^\"]+\")|(\\S+)");
public final Collection<String> sources;
public final String destination;
private Add(Collection<String> sources, String destination) {
this.sources = sources;
this.destination = destination;
}
@Override
public Add transform(final Map<String, String> env) {
Collection<String> resources = Collections2.transform(sources, new Function<String, String>() {
@Override
public String apply(String source) {
return filterForEnvironmentVars(env, source).trim();
}
});
return new Add(resources, destination);
}
public Iterable<String> getFileResources() {
return Collections2.filter(sources, new Predicate<String>() {
@Override
public boolean apply(String source) {
URI uri;
try {
uri = new URI(source);
} catch (URISyntaxException e) {
return false;
}
return uri.getScheme() == null || "file".equals(uri.getScheme());
}
});
}
/**
* Createa an Add if it matches, or missing if not.
*
* @param statement
* statement that may be an ADD or a COPY
* @return optional typed item.
*/
public static Optional<Add> create(String statement) {
Matcher argumentMatcher = ARGUMENT_TOKENIZER.matcher(statement.trim());
if (!argumentMatcher.find()) {
return Optional.absent();
}
String commandName = argumentMatcher.group();
if (!(StringUtils.equals(commandName, "ADD") || StringUtils.equals(commandName, "COPY"))) {
return Optional.absent();
}
String lastToken = null;
Collection<String> sources = new ArrayList<>();
while (argumentMatcher.find()) {
if (lastToken != null) {
sources.add(lastToken);
}
lastToken = argumentMatcher.group().replaceAll("(^\")|(\"$)", "");
}
if (sources.isEmpty()) {
throw new DockerClientException("Wrong ADD or COPY format");
}
return Optional.of(new Add(sources, lastToken));
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("sources", sources).add("destination", destination).toString();
}
}
public static class Env extends DockerfileStatement<Env> {
private static final Pattern ENV_PATTERN = Pattern.compile("^ENV\\s+(.*)\\s+(.*)$");
public final String variable;
public final String value;
private Env(String variable, String value) {
this.variable = variable;
this.value = value;
}
private Env(Matcher envMatcher) {
this.variable = envMatcher.group(1).trim();
this.value = envMatcher.group(2).trim();
}
public static Optional<Env> create(String statement) {
Matcher matcher = ENV_PATTERN.matcher(statement.trim());
if (!matcher.find()) {
return Optional.absent();
}
if (matcher.groupCount() != 2) {
throw new DockerClientException("Wrong ENV format");
}
return Optional.of(new Env(matcher));
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("variable", variable).add("value", value).toString();
}
}
/**
* Return a dockerfile statement
*/
public static Optional<? extends DockerfileStatement> createFromLine(String cmd) {
if (cmd.trim().isEmpty() || cmd.startsWith("#")) {
return Optional.absent();
}
Optional<? extends DockerfileStatement> line;
line = Add.create(cmd);
if (line.isPresent()) {
return line;
}
line = Env.create(cmd);
if (line.isPresent()) {
return line;
}
return Optional.of(new OtherLine(cmd));
}
}