1 package io.micronaut.build.services;
2
3 import com.google.cloud.tools.jib.api.Credential;
4 import com.google.cloud.tools.jib.maven.MavenProjectProperties;
5 import org.apache.maven.model.Plugin;
6 import org.apache.maven.project.MavenProject;
7 import org.codehaus.plexus.util.xml.Xpp3Dom;
8
9 import javax.inject.Inject;
10 import javax.inject.Singleton;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.Optional;
14 import java.util.Set;
15 import java.util.stream.Collectors;
16
17
18
19
20
21
22
23 @Singleton
24 public class JibConfigurationService {
25
26 private final MavenProject mavenProject;
27
28 private Xpp3Dom configuration;
29 private Xpp3Dom to;
30 private Xpp3Dom from;
31
32 @Inject
33 public JibConfigurationService(MavenProject mavenProject) {
34 this.mavenProject = mavenProject;
35 final Plugin plugin = mavenProject.getPlugin(MavenProjectProperties.PLUGIN_KEY);
36 if (plugin != null && plugin.getConfiguration() != null) {
37 configuration = (Xpp3Dom) plugin.getConfiguration();
38 to = configuration.getChild("to");
39 from = configuration.getChild("from");
40 }
41 }
42
43 public Optional<String> getToImage() {
44 if (to != null) {
45 return Optional.ofNullable(to.getChild("image").getValue());
46 }
47 return Optional.empty();
48 }
49
50 public Optional<String> getFromImage() {
51 if (from != null) {
52 return Optional.ofNullable(from.getChild("image").getValue());
53 }
54 return Optional.empty();
55 }
56
57 public Set<String> getTags() {
58 if (to != null) {
59 Xpp3Dom tags = to.getChild("tags");
60 if (tags != null && tags.getChildCount() > 0) {
61 return Arrays.stream(tags.getChildren())
62 .map(Xpp3Dom::getValue)
63 .collect(Collectors.toSet());
64 }
65 }
66 return Collections.emptySet();
67 }
68
69 public Optional<Credential> getCredentials() {
70 if (to != null) {
71 Xpp3Dom auth = to.getChild("auth");
72 if (auth != null) {
73 Xpp3Dom username = auth.getChild("username");
74 Xpp3Dom password = auth.getChild("password");
75 if (username != null & password != null) {
76 return Optional.of(Credential.from(username.getValue(), password.getValue()));
77 }
78 }
79 }
80 return Optional.empty();
81 }
82
83 public Optional<String> getCredHelper() {
84 if (to != null) {
85 Xpp3Dom credHelper = to.getChild("credHelper");
86 if (credHelper != null) {
87 return Optional.of(credHelper.getValue());
88 }
89 }
90 return Optional.empty();
91 }
92 }