Skip to content

Commit

Permalink
Fix #64: Support reading values containing line separators
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 20, 2024
1 parent 4d7faa6 commit b213372
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
31 changes: 27 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,38 @@ jobs:
- uses: actions/checkout@v4

- uses: ./src/test/resources/
id: success
id: simple
with:
file: src/test/resources/test.properties
property: name
- run: '[[ "Darth Vader" == "${{ steps.success.outputs.value }}" ]]'
- run: ${{ 'Darth Vader' == steps.simple.outputs.value }}

- uses: ./src/test/resources/
id: default
id: multiline
with:
file: src/test/resources/test.properties
property: affiliation
- run: 'echo ${{ toJSON(steps.multiline) }}'
# - run: ${{ 'Galactic\nEmpire' == steps.multiline.outputs.value }}

- uses: ./src/test/resources/
id: simple-default
with:
file: src/test/resources/test.properties
property: version
default: 1.0.0
- run: '[[ "1.0.0" == "${{ steps.default.outputs.value }}" ]]'
- run: '[[ "1.0.0" == "${{ steps.simple-default.outputs.value }}" ]]'

- uses: ./src/test/resources/
id: multiline-default
with:
file: src/test/resources/test.properties
property: version
default: |-
1
2
3
- run: '[[ "1\n2\n3" == "${{ steps.multiline-default.outputs.value }}" ]]'

- uses: ./src/test/resources/
id: unexisting-file
Expand All @@ -42,7 +61,11 @@ jobs:
all: true
- run: '[[ "Darth Vader" == "${{ steps.all.outputs.name }}" ]]'
- run: '[[ "Sith Lord" == "${{ steps.all.outputs.occupation }}" ]]'
- run: '[[ "Galactic\nEmpire" == "${{ steps.all.outputs.affiliation }}" ]]'
- run: '[[ "Luke Skywalker" == "${{ steps.all.outputs.son }}" ]]'
- run: '[[ "1" == "${{ steps.all.outputs.read-java-properties-delimiter }}" ]]'
- run: '[[ "2" == "${{ steps.all.outputs.read-java-properties-delimiter-x }}" ]]'
- run: '[[ "3\n4" == "${{ steps.all.outputs.read-java-properties-delimiter-x-x }}" ]]'

- uses: ./src/test/resources/
id: failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.lang.System.lineSeparator
import java.util.Properties
import kotlin.io.path.Path
import kotlin.io.path.appendText
import kotlin.io.path.useLines

fun main(args: Array<String>) {
val file = args[0]
Expand All @@ -21,14 +22,19 @@ fun main(args: Array<String>) {
properties.load(fis)

if ("true".equals(all, ignoreCase = true)) {
properties.entries.forEach { (key, value) ->
githubOutput.appendText("$key=$value${lineSeparator()}")
properties.every { key, value ->
githubOutput.appendText(packKeyValue(key, value))
}
} else {
properties.getProperty(property)?.let { value ->
githubOutput.appendText("value=$value${lineSeparator()}")
githubOutput.appendText(packKeyValue("value", value))
} ?: run {
githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")}${lineSeparator()}")
githubOutput.appendText(
packKeyValue(
"value",
default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")
)
)
}
}
}
Expand All @@ -38,8 +44,31 @@ fun main(args: Array<String>) {
if ("true".equals(all, ignoreCase = true)) {
throw e
} else {
githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw e}${lineSeparator()}")
githubOutput.appendText(packKeyValue("value", default.takeIf { it.isNotEmpty() } ?: throw e))
}
}

println("DEBUG")
githubOutput.useLines {
it.forEach { println(it) }
}
println("DEBUG")
}

private fun Properties.every(action: (String, String) -> Unit) {
for (element in this) action(element.key.toString(), element.value.toString())
}

private fun packKeyValue(key: String, value: String): String {
if (value.isMultiline()) {
var delimiter = "read-java-properties-delimiter"
while (value.contains(delimiter)) {
delimiter += "-x"
}
return "$key<<$delimiter${lineSeparator()}$value${lineSeparator()}$delimiter"
} else {
return "$key=$value${lineSeparator()}"
}
}

private fun String.isMultiline(): Boolean = this.split(Regex("^(.*)$", RegexOption.MULTILINE)).isNotEmpty()
4 changes: 4 additions & 0 deletions src/test/resources/test.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
name=Darth Vader
occupation=Sith Lord
affiliation=Galactic\nEmpire
son=Luke \
Skywalker
read-java-properties-delimiter=1
read-java-properties-delimiter-x=2
read-java-properties-delimiter-x-x=3\n4

0 comments on commit b213372

Please sign in to comment.