Skip to content

Commit e8e4d0c

Browse files
authored
feat: add support for getting all env vars and system props (#638)
1 parent c492b56 commit e8e4d0c

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "6b06d640-19b8-4fe7-86b1-cdbebce52b33",
3+
"type": "misc",
4+
"description": "Add support for getting all env vars and system properties (i.e., not just by a single key)",
5+
"issues": [
6+
"awslabs/aws-sdk-kotlin#575"
7+
]
8+
}

runtime/utils/common/src/aws/smithy/kotlin/runtime/util/EnvironmentProvider.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ package aws.smithy.kotlin.runtime.util
88
/**
99
* Provide a mapping from key to value
1010
*/
11-
public fun interface EnvironmentProvider {
11+
public interface EnvironmentProvider {
12+
/**
13+
* Get a map of all environment variables.
14+
* @return A map of string keys to string values.
15+
*/
16+
public fun getAllEnvVars(): Map<String, String>
17+
1218
/**
1319
* Get an environment variable or null
1420
*

runtime/utils/common/src/aws/smithy/kotlin/runtime/util/PropertyProvider.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ package aws.smithy.kotlin.runtime.util
88
/**
99
* Provide a mapping from (JVM) property key to value
1010
*/
11-
public fun interface PropertyProvider {
11+
public interface PropertyProvider {
12+
/**
13+
* Get a map of all JVM system properties.
14+
* @return A map of string keys to string values.
15+
*/
16+
public fun getAllProperties(): Map<String, String>
17+
1218
/**
1319
* Get a system property (on supported platforms)
1420
*

runtime/utils/jvm/src/aws/smithy/kotlin/runtime/util/PlatformJVM.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import java.nio.file.Path
1313
import java.util.*
1414

1515
public actual object Platform : PlatformProvider {
16+
override fun getAllEnvVars(): Map<String, String> = System.getenv()
17+
1618
/**
1719
* Get an environment variable or null
1820
*/
@@ -43,6 +45,11 @@ public actual object Platform : PlatformProvider {
4345
suspend fun readFileOrNull(path: Path): ByteArray? = readFileOrNull(path.toAbsolutePath().toString())
4446
suspend fun readFileOrNull(file: File): ByteArray? = readFileOrNull(file.absolutePath)
4547

48+
override fun getAllProperties(): Map<String, String> = System
49+
.getProperties()
50+
.entries
51+
.associate { (key, value) -> key.toString() to value.toString() }
52+
4653
/**
4754
* Get a system property or null
4855
*

runtime/utils/jvm/test/aws/smithy/kotlin/runtime/util/PlatformJVMTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,21 @@ class PlatformJVMTest {
3232
assertNotNull(actual)
3333
assertEquals(fileContent, actual.decodeToString())
3434
}
35+
36+
@Test
37+
fun testGetAllEnvVars() {
38+
val allEnvVarsFromSystem = System.getenv()
39+
val allEnvVarsFromPlatform = Platform.getAllEnvVars()
40+
assertEquals(allEnvVarsFromSystem, allEnvVarsFromPlatform)
41+
}
42+
43+
@Test
44+
fun testGetAllProperties() {
45+
val allPropertiesFromSystem = System
46+
.getProperties()
47+
.entries
48+
.associate { (key, value) -> key.toString() to value.toString() }
49+
val allPropertiesFromPlatform = Platform.getAllProperties()
50+
assertEquals(allPropertiesFromSystem, allPropertiesFromPlatform)
51+
}
3552
}

0 commit comments

Comments
 (0)