Skip to content

Commit 1bdb4a9

Browse files
committed
ID-21: add files generator
1 parent 1b99c2b commit 1bdb4a9

15 files changed

Lines changed: 679 additions & 6 deletions

File tree

api/src/main/java/com/axway/yamles/utils/plugins/Evaluator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.axway.yamles.utils.plugins;
22

3+
import java.io.File;
34
import java.util.Objects;
45

56
public class Evaluator {
@@ -31,7 +32,16 @@ public String evaluate(String template) throws TemplateEngineException {
3132
return this.engine.evaluate(template);
3233
}
3334

35+
public String evaluate(File template) throws TemplateEngineException {
36+
return this.engine.evaluate(template);
37+
}
38+
3439
public static String eval(String template) throws TemplateEngineException {
3540
return evaluator.evaluate(template);
3641
}
42+
43+
public static String eval(File template) throws TemplateEngineException {
44+
return evaluator.evaluate(template);
45+
}
46+
3747
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.axway.yamles.utils.plugins;
22

3+
import java.io.File;
4+
35
public interface TemplateEngine {
46
public default String evaluate(String template) throws TemplateEngineException {
57
return template;
68
}
9+
10+
public default String evaluate(File template) throws TemplateEngineException {
11+
return template.getAbsolutePath();
12+
}
713
}

cmd/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<!-- Library versions -->
4949
<apache-commons-io>2.13.0</apache-commons-io>
5050
<picoli.version>4.7.5</picoli.version>
51-
<pebble.version>3.2.1</pebble.version>
51+
<pebble.version>3.2.2</pebble.version>
5252
<jsonpath.version>2.8.0</jsonpath.version>
5353
</properties>
5454
<dependencies>

cmd/src/main/java/com/axway/yamles/utils/ConfigCommand.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import com.axway.yamles.utils.merge.AbstractLookupEnabledCommand;
1010
import com.axway.yamles.utils.merge.certs.CertificatesConfigurator;
1111
import com.axway.yamles.utils.merge.config.FieldConfigurator;
12+
import com.axway.yamles.utils.merge.files.FileGenerator;
13+
import com.axway.yamles.utils.merge.files.FilesCommand.FilesArg;
1214
import com.axway.yamles.utils.plugins.ExecutionMode;
1315

16+
import picocli.CommandLine.ArgGroup;
1417
import picocli.CommandLine.Command;
1518
import picocli.CommandLine.Option;
1619

@@ -32,6 +35,9 @@ public class ConfigCommand extends AbstractLookupEnabledCommand {
3235
"--certs" }, description = "Certificate configuration file.", paramLabel = "FILE", required = false)
3336
private List<File> certConfigs;
3437

38+
@ArgGroup(exclusive = false, multiplicity = "1..*")
39+
private List<FilesArg> fileGeneratorConfigs;
40+
3541
@Option(names = {
3642
"--expiration-warning" }, description = "Audit warning in case of certificate expires within the next days.", paramLabel = "DAYS", required = false)
3743
private int expirationWarningDays = CertificatesConfigurator.DEFAULT_EXP_WARNING_DAYS;
@@ -44,7 +50,8 @@ public class ConfigCommand extends AbstractLookupEnabledCommand {
4450
"--expiration-fail" }, description = "Fail in case of certificate expires within the next days.", paramLabel = "DAYS", required = false)
4551
private int expirationFailDays = CertificatesConfigurator.DEFAULT_EXP_FAIL_DAYS;
4652

47-
@Option(names = { "-f", "--fragment", "--config" }, description = "Configuration fragment for values.yaml file.", paramLabel = "FILE", required = false)
53+
@Option(names = { "-f", "--fragment",
54+
"--config" }, description = "Configuration fragment for values.yaml file.", paramLabel = "FILE", required = false)
4855
private List<File> fragmentConfigs;
4956

5057
@Option(names = {
@@ -53,7 +60,7 @@ public class ConfigCommand extends AbstractLookupEnabledCommand {
5360

5461
@Override
5562
public Integer call() throws Exception {
56-
initializeProviderManager(this.mode);
63+
initializeProviderManager(this.mode);
5764
Integer result = 0;
5865

5966
YamlEs es = new YamlEs(this.projectDir);
@@ -80,6 +87,12 @@ public Integer call() throws Exception {
8087
cc.apply(es);
8188
}
8289

90+
if (requiresFileGeneratoreConfig()) {
91+
FileGenerator fg = new FileGenerator(this.mode);
92+
fg.setFilesArgs(this.fileGeneratorConfigs);
93+
fg.apply();
94+
}
95+
8396
return result;
8497
}
8598

@@ -89,6 +102,9 @@ private boolean requiresFragmentConfig() {
89102

90103
private boolean requiresCertificateConfig() {
91104
return this.certConfigs != null && !this.certConfigs.isEmpty();
105+
}
92106

107+
private boolean requiresFileGeneratoreConfig() {
108+
return this.fileGeneratorConfigs != null && !this.fileGeneratorConfigs.isEmpty();
93109
}
94110
}

cmd/src/main/java/com/axway/yamles/utils/merge/MergeCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import com.axway.yamles.utils.merge.certs.MergeCertificatesCommand;
44
import com.axway.yamles.utils.merge.config.MergeConfigCommand;
5+
import com.axway.yamles.utils.merge.files.FilesCommand;
56
import com.axway.yamles.utils.plugins.ExecutionMode;
67

78
import picocli.CommandLine.Command;
89
import picocli.CommandLine.HelpCommand;
910
import picocli.CommandLine.Option;
1011

1112
@Command(name = "merge", description = "Merge configuration from various sources.", subcommands = { HelpCommand.class,
12-
MergeConfigCommand.class, MergeCertificatesCommand.class,
13+
MergeConfigCommand.class, MergeCertificatesCommand.class, FilesCommand.class,
1314
DescribeCommand.class }, mixinStandardHelpOptions = true)
1415
public class MergeCommand {
1516

cmd/src/main/java/com/axway/yamles/utils/merge/Mustache.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.axway.yamles.utils.merge;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.io.StringWriter;
56
import java.lang.reflect.Method;
@@ -131,8 +132,8 @@ public static Mustache getInstance() {
131132
if (instance == null) {
132133
instance = new Mustache();
133134
instance.refresh();
134-
Evaluator.setTemplateEngine(instance);
135135
}
136+
Evaluator.setTemplateEngine(instance);
136137
}
137138
return instance;
138139
}
@@ -147,14 +148,27 @@ public String evaluate(String template) throws TemplateEngineException {
147148
throw new IllegalStateException("Pebble engine not initialized");
148149
}
149150
PebbleTemplate pt = pe.getLiteralTemplate(template);
151+
return evaluate(pt);
152+
}
153+
154+
@Override
155+
public String evaluate(File template) throws TemplateEngineException {
156+
if (pe == null) {
157+
throw new IllegalStateException("Pebble engine not initialized");
158+
}
159+
PebbleTemplate pt = this.pe.getTemplate(template.getAbsolutePath());
160+
return evaluate(pt);
161+
}
162+
163+
private String evaluate(PebbleTemplate pt) {
150164
StringWriter result = new StringWriter();
151165
try {
152166
pt.evaluate(result);
153167
} catch (IOException e) {
154168
throw new TemplateEngineException("error on evaluating template", e);
155169
}
156170

157-
return result.toString();
171+
return result.toString();
158172
}
159173

160174
@Override
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.axway.yamles.utils.merge.files;
2+
3+
import java.io.File;
4+
import java.nio.charset.Charset;
5+
import java.util.Objects;
6+
import java.util.Optional;
7+
8+
import com.fasterxml.jackson.annotation.JsonCreator;
9+
import com.fasterxml.jackson.annotation.JsonProperty;
10+
11+
public class FileConfig {
12+
public static final String ENCODING_BINARY = "binary";
13+
14+
private File configSource = null;
15+
private final String path;
16+
private final Charset encoding;
17+
private final String content;
18+
private final File template;
19+
20+
@JsonCreator
21+
public FileConfig(@JsonProperty("path") String path, @JsonProperty("encoding") String encoding,
22+
@JsonProperty("content") String content, @JsonProperty("template") String template) {
23+
if (path == null || path.isEmpty())
24+
throw new IllegalArgumentException("non-empty 'path' property required");
25+
this.path = path;
26+
if (encoding == null || encoding.isEmpty())
27+
throw new IllegalArgumentException("non-empty 'encoding' property required");
28+
this.encoding = ENCODING_BINARY.equals(encoding) ? null : Charset.forName(encoding);
29+
if (content == null && template == null)
30+
throw new IllegalArgumentException("'content' or 'template' property required");
31+
if (content != null && template != null)
32+
throw new IllegalArgumentException("'content' and 'template' property are mutual exclusive");
33+
this.content = content;
34+
this.template = (template != null) ? new File(template) : null;
35+
}
36+
37+
void setConfigSource(File source) {
38+
this.configSource = Objects.requireNonNull(source);
39+
}
40+
41+
public File getConfigSource() {
42+
return this.configSource;
43+
}
44+
45+
public String getPath() {
46+
return this.path;
47+
}
48+
49+
public Optional<Charset> getEncoding() {
50+
return Optional.ofNullable(this.encoding);
51+
}
52+
53+
public String getEncodingName() {
54+
return (this.encoding == null) ? ENCODING_BINARY : this.encoding.name();
55+
}
56+
57+
public String getContent() {
58+
return this.content;
59+
}
60+
61+
public File getTemplate() {
62+
return this.template;
63+
}
64+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.axway.yamles.utils.merge.files;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.OutputStream;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
import java.util.Base64;
10+
import java.util.List;
11+
import java.util.Objects;
12+
13+
import com.axway.yamles.utils.audit.Audit;
14+
import com.axway.yamles.utils.merge.files.FilesCommand.FilesArg;
15+
import com.axway.yamles.utils.plugins.Evaluator;
16+
import com.axway.yamles.utils.plugins.ExecutionMode;
17+
18+
public class FileGenerator {
19+
private final ExecutionMode mode;
20+
private final List<FilesArg> filesArgs = new ArrayList<>();
21+
22+
public FileGenerator(ExecutionMode mode) {
23+
this.mode = Objects.requireNonNull(mode, "configuration mode required");
24+
}
25+
26+
public void setFilesArgs(List<FilesArg> args) {
27+
this.filesArgs.clear();
28+
if (args != null) {
29+
this.filesArgs.addAll(args);
30+
}
31+
}
32+
33+
public void apply() throws Exception {
34+
Audit.AUDIT_LOG.info(Audit.HEADER_PREFIX + "File Configuration");
35+
for (FilesArg fa : this.filesArgs) {
36+
FilesConfig fsc = FilesConfig.loadConfig(fa.files);
37+
fsc.setConfigSource(fa.files);
38+
apply(fa.baseDir, fsc);
39+
}
40+
}
41+
42+
private void apply(File baseDir, FilesConfig fsc) throws Exception {
43+
for (FileConfig fc : fsc.getFileConfigs()) {
44+
apply(baseDir, fc);
45+
}
46+
}
47+
48+
private void apply(File baseDir, FileConfig fc) throws Exception {
49+
File targetFile = null;
50+
String filePath = fc.getPath();
51+
52+
if (baseDir != null) {
53+
Path basePath = Paths.get(baseDir.toURI());
54+
targetFile = basePath.resolve(filePath).toFile();
55+
} else {
56+
targetFile = new File(filePath);
57+
}
58+
59+
String content = "";
60+
61+
if (fc.getContent() != null) {
62+
content = Evaluator.eval(fc.getContent());
63+
} else if (fc.getTemplate() != null) {
64+
content = Evaluator.eval(fc.getTemplate());
65+
} else {
66+
throw new IllegalStateException("content and template are null");
67+
}
68+
69+
if (this.mode == ExecutionMode.CONFIG) {
70+
Audit.AUDIT_LOG.info(" generate file {} with encoding {} from {}", targetFile.getAbsoluteFile(),
71+
fc.getEncodingName(), fc.getConfigSource().getAbsolutePath());
72+
73+
byte[] output;
74+
if (fc.getEncoding().isPresent()) {
75+
output = content.getBytes(fc.getEncoding().get());
76+
} else {
77+
output = Base64.getDecoder().decode(content);
78+
}
79+
80+
try (OutputStream out = new FileOutputStream(targetFile)) {
81+
out.write(output);
82+
}
83+
84+
} else {
85+
Audit.AUDIT_LOG.info(" file generation skipped (due to execution mode {}): {}", this.mode,
86+
targetFile.getAbsolutePath());
87+
}
88+
}
89+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.axway.yamles.utils.merge.files;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
6+
import com.axway.yamles.utils.merge.AbstractLookupEnabledCommand;
7+
import com.axway.yamles.utils.merge.MergeCommand;
8+
import com.axway.yamles.utils.merge.ProviderManager;
9+
import com.axway.yamles.utils.plugins.ExecutionMode;
10+
11+
import picocli.CommandLine.ArgGroup;
12+
import picocli.CommandLine.Command;
13+
import picocli.CommandLine.Option;
14+
import picocli.CommandLine.ParentCommand;
15+
16+
@Command(name = "files", description = "Generate files.", mixinStandardHelpOptions = true)
17+
public class FilesCommand extends AbstractLookupEnabledCommand {
18+
19+
public static class FilesArg {
20+
public FilesArg() {
21+
}
22+
23+
public FilesArg(File baseDir, File files) {
24+
this.baseDir = baseDir;
25+
this.files = files;
26+
}
27+
28+
@Option(names = {
29+
"--files-base-dir" }, description = "Base directory for generated files.", paramLabel = "DIR", required = false)
30+
public File baseDir;
31+
32+
@Option(names = {
33+
"--files" }, description = "Files generator configuration.", paramLabel = "FILE", required = true)
34+
public File files;
35+
}
36+
37+
@ArgGroup(exclusive = false, multiplicity = "1..*")
38+
private List<FilesArg> args;
39+
40+
@ParentCommand
41+
private MergeCommand parentCommand;
42+
43+
44+
public FilesCommand() {
45+
super();
46+
}
47+
48+
@Override
49+
public Integer call() throws Exception {
50+
initializeProviderManager(this.parentCommand.getMode());
51+
52+
ExecutionMode mode = ProviderManager.getInstance().getConfigMode();
53+
54+
FileGenerator fg = new FileGenerator(mode);
55+
fg.setFilesArgs(args);
56+
fg.apply();
57+
58+
return 0;
59+
}
60+
}

0 commit comments

Comments
 (0)