/
Wrapper.java
140 lines (124 loc) · 4.41 KB
/
Wrapper.java
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
package com.deliveredtechnologies.maven.terraform.mojo;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.Set;
/**
* This is the wrapper class.
* This class will
* 1) Create the properties file including any -D command line modifications
* 2) Determine the OS and add that to the properties file
*/
@Mojo(name = "wrapper")
public class Wrapper extends TerraformMojo<String> {
/*
* These are all the possible input parameters the user can supply with
* the -D switch
*/
@Parameter( property = "distributionSite")
String indistributionSite;
@Parameter(property = "releaseDir")
String inreleaseDir;
@Parameter(property = "releaseName")
String inreleaseName;
@Parameter(property = "releaseVer")
String inreleaseVer;
@Parameter(property = "releaseOS")
String inreleaseOS;
@Parameter(property = "releaseSuffix")
String inreleaseSuffix;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
/*
* Here we determine the OS in order to add that to the prop file
*/
final String os_name = System.getProperty("os.name");
int windowsIndex = os_name.indexOf("indow");
if (inreleaseOS == null) {
if (windowsIndex != -1) {
inreleaseOS = "windows";
} else {
inreleaseOS = "linux";
}
}
/*
* Here we create the .tf directory if it doesn't already exist
*/
final File tf_dir = new File(".tf");
if (!tf_dir.exists()) {
if (tf_dir.mkdir()) {
getLog().info("Directory .tf is created");
} else {
getLog().info("Failed to create directory .tf");
}
}
/*
* Here we copy the scripts and properties file (if it doesn't already exist)
* from the jar file to the .tf directory
*/
String[] tfwFileNames = {"tfw", "tfw.cmd", "tfw.ps1", "terraform-maven.properties"};
Path tfWrapperPath = Paths.get(".tf");
for (String tfwFileName : tfwFileNames) {
File tfwFile = tfWrapperPath.resolve(tfwFileName).toFile();
if (!(tfwFileName.endsWith(".properties") && tfwFile.exists())) {
try {
FileUtils.copyURLToFile(this.getClass().getClassLoader().getResource("tf/" + tfwFileName), tfWrapperPath.resolve(tfwFileName).toFile());
} catch (IOException e) {
throw new MojoExecutionException(String.format("Unable to create %s!", tfwFileName), e);
}
}
}
/*
* Here we check for command line arguments (if any) and update the properties file
*/
// Read from properties file
File propFile = new File(String.format(".tf%sterraform-maven.properties", File.separator));
Properties prop = new Properties();
prop.setProperty("releaseDir", ".tf");
try (InputStream fis = new FileInputStream(propFile)) {
prop.load(fis);
} catch (IOException e) {
throw new MojoExecutionException(String.format("Unable to load properties from %s!", propFile.getName()), e);
}
// Update properties from command line args if supplied
if (indistributionSite != null) {
prop.setProperty("distributionSite", indistributionSite);
}
if (inreleaseDir != null) {
prop.setProperty("releaseDir", inreleaseDir);
}
if (inreleaseName != null) {
prop.setProperty("releaseName", inreleaseName);
}
if (inreleaseVer != null) {
prop.setProperty("releaseVer", inreleaseVer);
}
if (inreleaseOS != null) {
prop.setProperty("releaseOS", inreleaseOS);
}
if (inreleaseSuffix != null) {
prop.setProperty("releaseSuffix", inreleaseSuffix);
}
// Write to properties file
try (BufferedWriter propOut = new BufferedWriter(new FileWriter(propFile))) {
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
propOut.write(key + "=" + prop.getProperty(key) + "\n");
}
propOut.flush();
} catch (IOException e) {
throw new MojoExecutionException(String.format("Unable to write properties to %s!", propFile.getName()), e);
}
}
}