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 801c576
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 34 deletions.
22 changes: 22 additions & 0 deletions .github/actions/compare/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: compare
description: Compare two strings
inputs:
a:
required: true
description: A
b:
required: true
description: B
runs:
using: composite
steps:
- run: 'echo $a > /tmp/a'
shell: bash
env:
a: ${{ toJSON(inputs.a) }}
- run: 'echo $b > /tmp/b'
shell: bash
env:
b: ${{ toJSON(inputs.b) }}
- run: diff /tmp/a /tmp/b
shell: bash
100 changes: 71 additions & 29 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,86 @@ 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 }}" ]]'

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

- uses: ./src/test/resources/
id: unexisting-file
with:
file: src/test/resources/unexisting.properties
property: name
default: Anakin Skywalker
- run: '[[ "Anakin Skywalker" == "${{ steps.unexisting-file.outputs.value }}" ]]'

- uses: ./src/test/resources/
id: all
id: multiline
with:
file: src/test/resources/test.properties
all: true
- run: '[[ "Darth Vader" == "${{ steps.all.outputs.name }}" ]]'
- run: '[[ "Sith Lord" == "${{ steps.all.outputs.occupation }}" ]]'
- run: '[[ "Luke Skywalker" == "${{ steps.all.outputs.son }}" ]]'

- uses: ./src/test/resources/
id: failure
property: affiliation
- uses: ./.github/actions/compare
with:
file: src/test/resources/unexisting.properties
property: name
continue-on-error: true
- run: '[[ "failure" == "${{ steps.failure.outcome }}" ]]'
a: |-
Galactic
Empire
b: ${{ steps.multiline.outputs.value }}
# - run: '[[ "Galactic\nEmpire" == $VALUE ]]'
## - run: 'echo $value'
# env:
# VALUE: ${{ toJSON(steps.multiline.outputs.value) }}
#
## - name: Dump job context
## env:
## MULTILINE: ${{ toJson(steps.multiline) }}
## run: echo "$MULTILINE"
##
## - run: ${{ steps.multiline.outputs.value == 'Galactic\nEmpire' }}
#
# - 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.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
# with:
# file: src/test/resources/unexisting.properties
# property: name
# default: Anakin Skywalker
# - run: '[[ "Anakin Skywalker" == "${{ steps.unexisting-file.outputs.value }}" ]]'
#
# - uses: ./src/test/resources/
# id: all
# with:
# file: src/test/resources/test.properties
# 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
# with:
# file: src/test/resources/unexisting.properties
# property: name
# continue-on-error: true
# - run: '[[ "failure" == "${{ steps.failure.outcome }}" ]]'
actions-typing:
name: Validate actions typing
runs-on: ubuntu-latest
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 801c576

Please sign in to comment.