Skip to content

Commit aac75af

Browse files
alekseytominholgerbrandl
authored andcommitted
Support environment variables in repository credentials (#248)
1 parent a39a129 commit aac75af

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ Using annotations instead of comment directives to configure scripts is cleaner
316316

317317
// also protected artifact repositories are supported, see <https://github.com/holgerbrandl/kscript/blob/master/test/TestsReadme.md#manual-testing>
318318
// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="auth_user", password="password")
319-
319+
// You can use environment variables for user and password when string surrounded by double {} brackets
320+
// @file:MavenRepository("my-art", "http://localhost:8081/artifactory/authenticated_repo", user="{{ARTIFACTORY_USER}}", password="{{ARTIFACTORY_PASSWORD}}")
321+
// will be use 'ARTIFACTORY_USER' and 'ARTIFACTORY_PASSWORD' environment variables
322+
// if the value doesn't found in the script environment will fail
320323

321324
// Include helper scripts without deployment or prior compilation
322325
@file:Include("util.kt")

src/main/kotlin/kscript/app/DependencyUtil.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.sonatype.aether.artifact.Artifact
66
import org.sonatype.aether.repository.Authentication
77
import org.sonatype.aether.repository.RemoteRepository
88
import org.sonatype.aether.util.artifact.DefaultArtifact
9-
import org.sonatype.aether.util.artifact.JavaScopes.COMPILE
109
import org.sonatype.aether.util.artifact.JavaScopes.RUNTIME
1110
import java.io.File
1211

@@ -68,12 +67,26 @@ fun resolveDependencies(depIds: List<String>, customRepos: List<MavenRepo> = emp
6867
}
6968
}
7069

70+
fun decodeEnv(value: String): String {
71+
return if (value.startsWith("{{") && value.endsWith("}}")) {
72+
val envKey = value.substring(2, value.length - 2)
73+
val envValue = System.getenv()[envKey]
74+
if (null == envValue) {
75+
errorMsg("Could not resolve environment variable {{$envKey}} in maven repository credentials")
76+
quit(1)
77+
}
78+
envValue
79+
} else {
80+
value
81+
}
82+
}
83+
7184
fun resolveDependenciesViaAether(depIds: List<String>, customRepos: List<MavenRepo>, loggingEnabled: Boolean): List<Artifact> {
7285
val jcenter = RemoteRepository("jcenter", "default", "http://jcenter.bintray.com/")
7386
val customRemoteRepos = customRepos.map { mavenRepo ->
7487
RemoteRepository(mavenRepo.id, "default", mavenRepo.url).apply {
7588
if (!mavenRepo.user.isNullOrEmpty() && !mavenRepo.password.isNullOrEmpty()) {
76-
authentication = Authentication(mavenRepo.user, mavenRepo.password)
89+
authentication = Authentication(decodeEnv(mavenRepo.user), decodeEnv(mavenRepo.password))
7790
}
7891
}
7992
}

0 commit comments

Comments
 (0)