From 1b48b5448206cbfe2d2cff1c8f3cb6aff487f826 Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Tue, 12 Nov 2019 17:47:33 +0400 Subject: [PATCH 1/8] Add "Authentication from environment" --- src/main/kotlin/kscript/app/DependencyUtil.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/kscript/app/DependencyUtil.kt b/src/main/kotlin/kscript/app/DependencyUtil.kt index a1681550..c2c2615e 100644 --- a/src/main/kotlin/kscript/app/DependencyUtil.kt +++ b/src/main/kotlin/kscript/app/DependencyUtil.kt @@ -68,12 +68,20 @@ fun resolveDependencies(depIds: List, customRepos: List = emp } } +fun decodeEnv(value: String): String { + if (value.startsWith("@")) { + return System.getenv(value.substring(1)) + } else { + return value + } +} + fun resolveDependenciesViaAether(depIds: List, customRepos: List, loggingEnabled: Boolean): List { val jcenter = RemoteRepository("jcenter", "default", "http://jcenter.bintray.com/") val customRemoteRepos = customRepos.map { mavenRepo -> RemoteRepository(mavenRepo.id, "default", mavenRepo.url).apply { if (!mavenRepo.user.isNullOrEmpty() && !mavenRepo.password.isNullOrEmpty()) { - authentication = Authentication(mavenRepo.user, mavenRepo.password) + authentication = Authentication(decodeEnv(mavenRepo.user), decodeEnv(mavenRepo.password)) } } } From e6fbf1ca81a383721380e998004d059abdeeb29d Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Tue, 12 Nov 2019 17:57:48 +0400 Subject: [PATCH 2/8] Add "Authentication from environment" readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 749d1722..56cdcc6d 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,9 @@ Using annotations instead of comment directives to configure scripts is cleaner // also protected artifact repositories are supported, see // @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="auth_user", password="password") +// You can use environment variables for user and password +// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="@artifactory_user", password="@artifactory_password") +// will be use 'artifactory_user' and 'artifactory_password' environment variables // Include helper scripts without deployment or prior compilation From 87088d65b65e21590a48a8059ff4f5e7cfa6c29c Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Fri, 15 Nov 2019 08:49:56 +0400 Subject: [PATCH 3/8] Change `meta` marker --- README.md | 6 +++--- src/main/kotlin/kscript/app/DependencyUtil.kt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 56cdcc6d..c31a6044 100644 --- a/README.md +++ b/README.md @@ -316,10 +316,10 @@ Using annotations instead of comment directives to configure scripts is cleaner // also protected artifact repositories are supported, see // @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="auth_user", password="password") -// You can use environment variables for user and password -// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="@artifactory_user", password="@artifactory_password") +// You can use environment variables for user and password when string surrounded by double {} brackets +// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="{{artifactory_user}}", password="{{artifactory_password}}") // will be use 'artifactory_user' and 'artifactory_password' environment variables - +// if the value doesn't found in the script environment will fail // Include helper scripts without deployment or prior compilation @file:Include("util.kt") diff --git a/src/main/kotlin/kscript/app/DependencyUtil.kt b/src/main/kotlin/kscript/app/DependencyUtil.kt index c2c2615e..0b82f0da 100644 --- a/src/main/kotlin/kscript/app/DependencyUtil.kt +++ b/src/main/kotlin/kscript/app/DependencyUtil.kt @@ -6,7 +6,6 @@ import org.sonatype.aether.artifact.Artifact import org.sonatype.aether.repository.Authentication import org.sonatype.aether.repository.RemoteRepository import org.sonatype.aether.util.artifact.DefaultArtifact -import org.sonatype.aether.util.artifact.JavaScopes.COMPILE import org.sonatype.aether.util.artifact.JavaScopes.RUNTIME import java.io.File @@ -69,8 +68,9 @@ fun resolveDependencies(depIds: List, customRepos: List = emp } fun decodeEnv(value: String): String { - if (value.startsWith("@")) { - return System.getenv(value.substring(1)) + if (value.startsWith("{{") && value.endsWith("}}")) { + val envKey = value.substring(2, value.length - 2) + return System.getenv(envKey) } else { return value } From 189a5d9d5fb63a0f45e5992cc00a9e7985a61cba Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Fri, 15 Nov 2019 08:50:22 +0400 Subject: [PATCH 4/8] Fix error message --- src/main/kotlin/kscript/app/DependencyUtil.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/kscript/app/DependencyUtil.kt b/src/main/kotlin/kscript/app/DependencyUtil.kt index 0b82f0da..73bb7d3a 100644 --- a/src/main/kotlin/kscript/app/DependencyUtil.kt +++ b/src/main/kotlin/kscript/app/DependencyUtil.kt @@ -70,7 +70,11 @@ fun resolveDependencies(depIds: List, customRepos: List = emp fun decodeEnv(value: String): String { if (value.startsWith("{{") && value.endsWith("}}")) { val envKey = value.substring(2, value.length - 2) - return System.getenv(envKey) + try { + return System.getenv(envKey) + } catch (e: RuntimeException) { + throw RuntimeException("environment variable '$envKey' doesn't found") + } } else { return value } From 83d8c1f91a2c44b0af6a3a6389f91e22c0d5ae23 Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Fri, 15 Nov 2019 08:58:02 +0400 Subject: [PATCH 5/8] Fix codestyle --- src/main/kotlin/kscript/app/DependencyUtil.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/kscript/app/DependencyUtil.kt b/src/main/kotlin/kscript/app/DependencyUtil.kt index 73bb7d3a..07825154 100644 --- a/src/main/kotlin/kscript/app/DependencyUtil.kt +++ b/src/main/kotlin/kscript/app/DependencyUtil.kt @@ -68,15 +68,15 @@ fun resolveDependencies(depIds: List, customRepos: List = emp } fun decodeEnv(value: String): String { - if (value.startsWith("{{") && value.endsWith("}}")) { + return if (value.startsWith("{{") && value.endsWith("}}")) { val envKey = value.substring(2, value.length - 2) try { - return System.getenv(envKey) + System.getenv(envKey) } catch (e: RuntimeException) { throw RuntimeException("environment variable '$envKey' doesn't found") } } else { - return value + value } } From c8fc911e05009936e1697cc188a8794ff9dd4292 Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Fri, 15 Nov 2019 11:11:24 +0400 Subject: [PATCH 6/8] Don't use method with wrong java-to-kotlin conversion --- src/main/kotlin/kscript/app/DependencyUtil.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/kscript/app/DependencyUtil.kt b/src/main/kotlin/kscript/app/DependencyUtil.kt index 07825154..ebb9bf4c 100644 --- a/src/main/kotlin/kscript/app/DependencyUtil.kt +++ b/src/main/kotlin/kscript/app/DependencyUtil.kt @@ -70,11 +70,11 @@ fun resolveDependencies(depIds: List, customRepos: List = emp fun decodeEnv(value: String): String { return if (value.startsWith("{{") && value.endsWith("}}")) { val envKey = value.substring(2, value.length - 2) - try { - System.getenv(envKey) - } catch (e: RuntimeException) { + val envValue = System.getenv()[envKey] + if (null == envValue) { throw RuntimeException("environment variable '$envKey' doesn't found") } + envValue } else { value } From b9555ca59c782cc61b11ecbba641b2e88ccda4f2 Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Fri, 15 Nov 2019 11:11:49 +0400 Subject: [PATCH 7/8] Write message and exit instead of throw error --- src/main/kotlin/kscript/app/DependencyUtil.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/kscript/app/DependencyUtil.kt b/src/main/kotlin/kscript/app/DependencyUtil.kt index ebb9bf4c..e892d154 100644 --- a/src/main/kotlin/kscript/app/DependencyUtil.kt +++ b/src/main/kotlin/kscript/app/DependencyUtil.kt @@ -72,7 +72,8 @@ fun decodeEnv(value: String): String { val envKey = value.substring(2, value.length - 2) val envValue = System.getenv()[envKey] if (null == envValue) { - throw RuntimeException("environment variable '$envKey' doesn't found") + errorMsg("Could not resolve environment variable {{$envKey}} in maven repository credentials") + quit(1) } envValue } else { From 5e581e72a4e8dfd902767ca629ac323b2c33dda9 Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Fri, 15 Nov 2019 11:12:05 +0400 Subject: [PATCH 8/8] Uppercase variable names --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c31a6044..23a9178c 100644 --- a/README.md +++ b/README.md @@ -317,8 +317,8 @@ Using annotations instead of comment directives to configure scripts is cleaner // also protected artifact repositories are supported, see // @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="auth_user", password="password") // You can use environment variables for user and password when string surrounded by double {} brackets -// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="{{artifactory_user}}", password="{{artifactory_password}}") -// will be use 'artifactory_user' and 'artifactory_password' environment variables +// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="{{ARTIFACTORY_USER}}", password="{{ARTIFACTORY_PASSWORD}}") +// will be use 'ARTIFACTORY_USER' and 'ARTIFACTORY_PASSWORD' environment variables // if the value doesn't found in the script environment will fail // Include helper scripts without deployment or prior compilation