Last active
August 25, 2019 19:57
-
-
Save uroshekic/dd1026bc5b3916337fc2875489784869 to your computer and use it in GitHub Desktop.
This script transforms Spring's .properties file into different formats: Docker Compose environment variables/env-file or Asciidoctor Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
if (args.size < 2) { | |
println("This script transforms a .properties file in different formats.") | |
println("Please provide arguments: <file> <mode>") | |
println("Modes:") | |
println(" - environment: Print out properties in a Docker Compose environment attribute format") | |
println(" - env-file: Print out properties in a Docker Compose environment file attribute format") | |
println(" - configmap: Print out properties in a Kubernetes Configmap (YAML) format") | |
println(" - adoc-table: Print out an ASCII Doctor table") | |
println(" - adoc-table-small-example: Print out an ASCII Doctor table with small example") | |
} else { | |
val filePath = args[0] | |
val mode = args[1] | |
var previousLine = "" | |
File(filePath).forEachLine { | |
// println("Prev: " + previousLine) | |
if (it.isEmpty()) { | |
if (!mode.startsWith("adoc-table")) println() | |
} | |
else if (it.startsWith("#")) { | |
if (!mode.startsWith("adoc-table")) println(it) | |
} | |
else { | |
val regex = """([a-zA-Z_\.-]*)=([^#]*)\w*(# (.*))?""".toRegex() | |
val matchResult = regex.find(it) | |
if (matchResult != null) { | |
val name = matchResult.groupValues?.get(1) | |
val value = matchResult.groupValues?.get(2) | |
val _comment = matchResult.groupValues?.get(4) // Comments are not allowed in .properties file in the same line as key-value definition! | |
val comment = if (_comment == null || _comment == "") { if (previousLine.startsWith("# ")) previousLine.substring(2) else "" } else _comment | |
val env_name = name.replace(".", "_").replace("-", "_") | |
val env_value = if (mode.startsWith("adoc-table") && env_name == "infrastructure_keycloak_realmKey") "/" else value | |
when(mode) { | |
"environment" -> println("- " + env_name + "=" + env_value) // + (if (!comment.isEmpty()) " # " + comment else comment)) | |
"env-file" -> println(env_name + "=" + env_value) // + (if (!comment.isEmpty()) " # " + comment else comment)) | |
"configmap" -> println(env_name + ": \"" + env_value + "\"") // + (if (!comment.isEmpty()) " # " + comment else comment)) | |
"adoc-table" -> println("|" + env_name + "\n|" + comment + "\n\n" + "Example value: " + env_value?.trimEnd() + "\n\n") | |
"adoc-table-small-example" -> println("|" + env_name + "\n|" + (if (!comment.isEmpty()) comment + " +\n" else "") + "[.small]#Example value: " + env_value?.trimEnd() + "#\n") | |
} | |
} | |
else { println("?") } | |
} | |
previousLine = it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment