Skip to content

Commit d219535

Browse files
authored
Remove DockerRightPadding for literals and options (#3)
* Remove DockerRightPadded complexity from FROM * Remove DockerRightPadded complexity from ADD * Remove DockerRightPadded complexity from ADD/COPY * Remove DockerRightPadded complexity from CMD * Remove DockerRightPadded complexity from COMMENT * Remove DockerRightPadded complexity from ENTRYPOINT * Remove DockerRightPadded complexity from HEALTHCHECK * Remove DockerRightPadded complexity from RUN * Remove DockerRightPadded complexity from SHELL * Remove DockerRightPadded complexity from VOLUME * Reuse Space.build("\n")
1 parent 651dbe4 commit d219535

File tree

13 files changed

+495
-535
lines changed

13 files changed

+495
-535
lines changed

src/main/java/com/github/jimschubert/rewrite/docker/ChangeImage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ public Docker.From visitFrom(Docker.From from, ExecutionContext executionContext
108108
if (newPlatform != null) {
109109
platform = newPlatform;
110110
} else {
111-
platform = from.getPlatform().getElement().getText();
111+
platform = from.getPlatform().getText();
112112
}
113113

114-
if (image != null && !image.equals(from.getImage().getElement().getText())) {
114+
if (image != null && !image.equals(from.getImage().getText())) {
115115
from = from.image(image);
116116
}
117117

118-
if (version != null && !version.equals(from.getVersion().getElement().getText())) {
118+
if (version != null && !version.equals(from.getVersion().getText())) {
119119
from = from.version(version);
120120
}
121121

122-
if (platform != null && !platform.equals(from.getPlatform().getElement().getText())) {
122+
if (platform != null && !platform.equals(from.getPlatform().getText())) {
123123
from = from.platform(platform);
124124
}
125125

src/main/java/com/github/jimschubert/rewrite/docker/DockerVisitor.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public Docker visitStage(Docker.Stage stage, P p) {
5353

5454
public Docker visitFrom(Docker.From from, P p) {
5555
return from.withPrefix(visitSpace(from.getPrefix(), p))
56-
.withImage(visitDockerRightPadded(from.getImage(), p))
57-
.withPlatform(visitDockerRightPadded(from.getPlatform(), p))
58-
.withVersion(visitDockerRightPadded(from.getVersion(), p))
56+
.withImage(visitAndCast(from.getImage(), p))
57+
.withPlatform(visitAndCast(from.getPlatform(), p))
58+
.withVersion(visitAndCast(from.getVersion(), p))
5959
.withAs(visitAndCast(from.getAs(), p))
60-
.withAlias(visitDockerRightPadded(from.getAlias(), p))
60+
.withAlias(visitAndCast(from.getAlias(), p))
6161
.withMarkers(visitMarkers(from.getMarkers(), p));
6262
}
6363

@@ -70,8 +70,8 @@ public Docker visitComment(Docker.Comment comment, P p) {
7070
public Docker visitRun(Docker.Run run, P p) {
7171
return run
7272
.withPrefix(visitSpace(run.getPrefix(), p))
73-
.withOptions(ListUtils.map(run.getOptions(), o -> visitDockerRightPadded(o, p)))
74-
.withCommands(ListUtils.map(run.getCommands(), c -> visitDockerRightPadded(c, p)))
73+
.withOptions(ListUtils.map(run.getOptions(), o -> visitAndCast(o, p)))
74+
.withCommands(ListUtils.map(run.getCommands(), c -> visitAndCast(c, p)))
7575
.withMarkers(visitMarkers(run.getMarkers(), p));
7676
}
7777

@@ -80,7 +80,7 @@ public Docker visitCmd(Docker.Cmd cmd, P p) {
8080
.withForm(cmd.getForm())
8181
.withPrefix(visitSpace(cmd.getPrefix(), p))
8282
.withExecFormPrefix(visitSpace(cmd.getExecFormPrefix(), p))
83-
.withCommands(ListUtils.map(cmd.getCommands(), c -> visitDockerRightPadded(c, p)))
83+
.withCommands(ListUtils.map(cmd.getCommands(), c -> visitAndCast(c, p)))
8484
.withExecFormSuffix(visitSpace(cmd.getExecFormSuffix(), p))
8585
.withMarkers(visitMarkers(cmd.getMarkers(), p));
8686
}
@@ -117,18 +117,18 @@ public Docker visitEnv(Docker.Env env, P p) {
117117
public Docker visitAdd(Docker.Add add, P p) {
118118
return add
119119
.withPrefix(visitSpace(add.getPrefix(), p))
120-
.withOptions(ListUtils.map(add.getOptions(), o -> visitDockerRightPadded(o, p)))
121-
.withSources(ListUtils.map(add.getSources(), s -> visitDockerRightPadded(s, p)))
122-
.withDestination(visitDockerRightPadded(add.getDestination(), p))
120+
.withOptions(ListUtils.map(add.getOptions(), o -> visitAndCast(o, p)))
121+
.withSources(ListUtils.map(add.getSources(), s -> visitAndCast(s, p)))
122+
.withDestination(visitAndCast(add.getDestination(), p))
123123
.withMarkers(visitMarkers(add.getMarkers(), p));
124124
}
125125

126126
public Docker visitCopy(Docker.Copy copy, P p) {
127127
return copy
128128
.withPrefix(visitSpace(copy.getPrefix(), p))
129-
.withOptions(ListUtils.map(copy.getOptions(), o -> visitDockerRightPadded(o, p)))
130-
.withSources(ListUtils.map(copy.getSources(), s -> visitDockerRightPadded(s, p)))
131-
.withDestination(visitDockerRightPadded(copy.getDestination(), p))
129+
.withOptions(ListUtils.map(copy.getOptions(), o -> visitAndCast(o, p)))
130+
.withSources(ListUtils.map(copy.getSources(), s -> visitAndCast(s, p)))
131+
.withDestination(visitAndCast(copy.getDestination(), p))
132132
.withMarkers(visitMarkers(copy.getMarkers(), p));
133133
}
134134

@@ -137,7 +137,7 @@ public Docker visitEntrypoint(Docker.Entrypoint entrypoint, P p) {
137137
.withForm(entrypoint.getForm())
138138
.withPrefix(visitSpace(entrypoint.getPrefix(), p))
139139
.withExecFormPrefix(visitSpace(entrypoint.getExecFormPrefix(), p))
140-
.withCommands(ListUtils.map(entrypoint.getCommands(), c -> visitDockerRightPadded(c, p)))
140+
.withCommands(ListUtils.map(entrypoint.getCommands(), c -> visitAndCast(c, p)))
141141
.withExecFormSuffix(visitSpace(entrypoint.getExecFormSuffix(), p))
142142
.withMarkers(visitMarkers(entrypoint.getMarkers(), p));
143143
}
@@ -147,7 +147,7 @@ public Docker visitVolume(Docker.Volume volume, P p) {
147147
.withForm(volume.getForm())
148148
.withPrefix(visitSpace(volume.getPrefix(), p))
149149
.withExecFormPrefix(visitSpace(volume.getExecFormPrefix(), p))
150-
.withPaths(ListUtils.map(volume.getPaths(), path -> visitDockerRightPadded(path, p)))
150+
.withPaths(ListUtils.map(volume.getPaths(), path -> visitAndCast(path, p)))
151151
.withExecFormSuffix(visitSpace(volume.getExecFormSuffix(), p))
152152
.withMarkers(visitMarkers(volume.getMarkers(), p));
153153
}
@@ -194,15 +194,15 @@ public Docker visitHealthcheck(Docker.Healthcheck healthcheck, P p) {
194194
.withPrefix(visitSpace(healthcheck.getPrefix(), p))
195195
.withType(healthcheck.getType())
196196
.withOptions(ListUtils.map(healthcheck.getOptions(), o -> visitDockerRightPadded(o, p)))
197-
.withCommands(ListUtils.map(healthcheck.getCommands(), c -> visitDockerRightPadded(c, p)))
197+
.withCommands(ListUtils.map(healthcheck.getCommands(), c -> visitAndCast(c, p)))
198198
.withMarkers(visitMarkers(healthcheck.getMarkers(), p));
199199
}
200200

201201
public Docker visitShell(Docker.Shell shell, P p) {
202202
return shell
203203
.withPrefix(visitSpace(shell.getPrefix(), p))
204204
.withExecFormPrefix(visitSpace(shell.getExecFormPrefix(), p))
205-
.withCommands(ListUtils.map(shell.getCommands(), c -> visitDockerRightPadded(c, p)))
205+
.withCommands(ListUtils.map(shell.getCommands(), c -> visitAndCast(c, p)))
206206
.withExecFormSuffix(visitSpace(shell.getExecFormSuffix(), p))
207207
.withMarkers(visitMarkers(shell.getMarkers(), p));
208208
}

src/main/java/com/github/jimschubert/rewrite/docker/NameAllStages.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Docker.From visitFrom(Docker.From from, ExecutionContext executionContext
6262
Docker.Stage stage = Objects.requireNonNull(getCursor().getParent()).firstEnclosing(Docker.Stage.class);
6363
if (stage != null && stage.getMarkers() != null && stage.getMarkers().findFirst(Counter.class).isPresent()) {
6464
Counter counter = stage.getMarkers().findFirst(Counter.class).get();
65-
if (!counter.isLast && (from.getAlias().getElement().getText() == null || from.getAlias().getElement().getText().isEmpty())) {
65+
if (!counter.isLast && (from.getAlias().getText() == null || from.getAlias().getText().isEmpty())) {
6666
return from.alias("stage" + counter.index);
6767
}
6868
}
@@ -74,19 +74,19 @@ public Docker.From visitFrom(Docker.From from, ExecutionContext executionContext
7474
public Docker.Copy visitCopy(Docker.Copy copy, ExecutionContext executionContext) {
7575
if (copy.getOptions() != null && copy.getOptions().stream().anyMatch(
7676
o -> {
77-
Docker.KeyArgs key = o.getElement().getKeyArgs();
77+
Docker.KeyArgs key = o.getKeyArgs();
7878
return (key != null && "--from".equals(key.key())) && (key.value() != null && key.value().matches("^\\d+$"));
7979
})) {
8080
return copy.withOptions(
8181
ListUtils.map(copy.getOptions(), o -> {
82-
if (o == null || o.getElement() == null) {
82+
if (o == null) {
8383
return o;
8484
}
8585

86-
Docker.KeyArgs key = o.getElement().getKeyArgs();
86+
Docker.KeyArgs key = o.getKeyArgs();
8787
if (key != null && "--from".equals(key.key()) && (key.value() != null && key.value().matches("^\\d+$"))) {
8888
int index = Integer.parseInt(key.value());
89-
return o.map(e -> e.withKeyArgs(key.withValue(key.getValue().withText("stage" + index))));
89+
return o.withKeyArgs(key.withValue(key.getValue().withText("stage" + index)));
9090
}
9191

9292
return o;

src/main/java/com/github/jimschubert/rewrite/docker/analysis/ListImages.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public Docker.Document visitDocument(Docker.Document dockerfile, ExecutionContex
6363
if (child instanceof Docker.From) {
6464
Docker.From from = (Docker.From) child;
6565
String platformSwitch = null;
66-
if (from.getPlatform().getElement() != null && from.getPlatform().getElement().getText() != null) {
67-
platformSwitch = from.getPlatform().getElement().getText().split("=")[1];
66+
if (from.getPlatform().getText() != null) {
67+
platformSwitch = from.getPlatform().getText().split("=")[1];
6868
}
6969

7070
acc.add(new ImageUseReport.Row(
@@ -73,7 +73,7 @@ public Docker.Document visitDocument(Docker.Document dockerfile, ExecutionContex
7373
from.getTag(),
7474
from.getDigest(),
7575
platformSwitch,
76-
from.getAlias().getElement().getText()
76+
from.getAlias().getText()
7777
));
7878
}
7979
}

src/main/java/com/github/jimschubert/rewrite/docker/analysis/ListRemoteFiles.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.github.jimschubert.rewrite.docker.DockerIsoVisitor;
1818
import com.github.jimschubert.rewrite.docker.table.RemoteFileReport;
1919
import com.github.jimschubert.rewrite.docker.tree.Docker;
20-
import com.github.jimschubert.rewrite.docker.tree.DockerRightPadded;
2120
import lombok.EqualsAndHashCode;
2221
import org.openrewrite.ExecutionContext;
2322
import org.openrewrite.ScanningRecipe;
@@ -61,15 +60,15 @@ public Docker.Document visitDocument(Docker.Document dockerfile, ExecutionContex
6160
for (Docker child : stage.getChildren()) {
6261
if (child instanceof Docker.Add) {
6362
Docker.Add instruction = (Docker.Add) child;
64-
List<DockerRightPadded<Docker.Literal>> urls = instruction.getSources().stream()
65-
.filter(s -> s.getElement().getText().startsWith("http"))
66-
.collect(Collectors.toCollection(ArrayList::new));
63+
List<Docker.Literal> urls = instruction.getSources().stream()
64+
.filter(s -> s.getText().startsWith("http"))
65+
.collect(Collectors.toList());
6766

6867
if (!urls.isEmpty()) {
6968
urls.forEach(url -> {
7069
acc.add(new RemoteFileReport.Row(
7170
dockerfile.getSourcePath().toString(),
72-
url.getElement().getText()
71+
url.getText()
7372
));
7473
});
7574
}

0 commit comments

Comments
 (0)