From 8766668e4369528664c30984b999916f6d2d144a Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 16:15:48 +0100 Subject: [PATCH] Small improvements --- .../docker/trait/YamlImageReference.java | 34 ++++++++++--------- .../docker/FindDockerImagesUsedTest.java | 12 +++++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java b/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java index 6671013..fb4fa80 100644 --- a/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java +++ b/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java @@ -22,25 +22,27 @@ public Reference.Kind getKind() { } public static class Provider extends YamlProvider { - @Override - public SimpleTraitMatcher getMatcher() { - return new SimpleTraitMatcher() { - private final AtomicBoolean found = new AtomicBoolean(false); + private static final SimpleTraitMatcher matcher = new SimpleTraitMatcher() { + 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 getMatcher() { + return matcher; } } } diff --git a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java index 2683caf..ca3b94d 100644 --- a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java +++ b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java @@ -352,20 +352,24 @@ 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 @@ -373,20 +377,24 @@ void kubernetesFile() { 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") )