diff --git a/README.md b/README.md index 749d1722..23a9178c 100644 --- a/README.md +++ b/README.md @@ -316,7 +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 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 a1681550..e892d154 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 @@ -68,12 +67,26 @@ 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) + val envValue = System.getenv()[envKey] + if (null == envValue) { + errorMsg("Could not resolve environment variable {{$envKey}} in maven repository credentials") + quit(1) + } + envValue + } else { + 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)) } } }