Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 24, 2024
1 parent 27ffeb5 commit 8766668
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
34 changes: 18 additions & 16 deletions src/main/java/org/openrewrite/docker/trait/YamlImageReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ public Reference.Kind getKind() {
}

public static class Provider extends YamlProvider {
@Override
public SimpleTraitMatcher<YamlReference> getMatcher() {
return new SimpleTraitMatcher<YamlReference>() {
private final AtomicBoolean found = new AtomicBoolean(false);
private static final SimpleTraitMatcher<YamlReference> matcher = new SimpleTraitMatcher<YamlReference>() {
private final AtomicBoolean found = new AtomicBoolean(false);

@Override
protected @Nullable YamlReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Yaml.Scalar) {
if (found.get()) {
found.set(false);
return new YamlImageReference(cursor);
} else if ("image".equals(((Yaml.Scalar) value).getValue())) {
found.set(true);
}
@Override
protected @Nullable YamlReference test(Cursor cursor) {
Object value = cursor.getValue();
if (value instanceof Yaml.Scalar) {
if (found.get()) {
found.set(false);
return new YamlImageReference(cursor);
} else if ("image".equals(((Yaml.Scalar) value).getValue())) {
found.set(true);
}
return null;
}
};
return null;
}
};

@Override
public SimpleTraitMatcher<YamlReference> getMatcher() {
return matcher;
}
}
}
12 changes: 10 additions & 2 deletions src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,41 +352,49 @@ void kubernetesFile() {
spec:
containers:
- image: image
name: image-container
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: app:v1.2.3
- name: my-container
image: app:v1.2.3
initContainers:
- image: account/image:latest
name: my-init-container
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: repo.id/account/bucket/image:v1.2.3@digest
name: my-container
""",
"""
apiVersion: v1
kind: Pod
spec:
containers:
- image: ~~(image)~~>image
name: image-container
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: ~~(app:v1.2.3)~~>app:v1.2.3
- name: my-container
image: ~~(app:v1.2.3)~~>app:v1.2.3
initContainers:
- image: ~~(account/image:latest)~~>account/image:latest
name: my-init-container
---
apiVersion: v1
kind: Pod
spec:
containers:
- image: ~~(repo.id/account/bucket/image:v1.2.3@digest)~~>repo.id/account/bucket/image:v1.2.3@digest
name: my-container
""",
spec -> spec.path(".gitlab-ci")
)
Expand Down

0 comments on commit 8766668

Please sign in to comment.