Skip to content

Commit 5f57adb

Browse files
authored
Make credential entries respect the --password-value option (#1949)
* Make credential entries respect the --password-value option * Update documentation
1 parent 67677a3 commit 5f57adb

File tree

6 files changed

+77
-20
lines changed

6 files changed

+77
-20
lines changed

modules/cli/src/main/scala/scala/cli/commands/config/Config.scala

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,29 @@ object Config extends ScalaCommand[ConfigOptions] {
186186
.traverseN
187187
.left.map(CompositeBuildException(_))
188188
.orExit(logger)
189-
val credentials = RepositoryCredentials(
190-
host,
191-
userOpt,
192-
passwordOpt,
193-
realm = realmOpt,
194-
optional = options.optional,
195-
matchHost = options.matchHost.orElse(Some(true)),
196-
httpsOnly = options.httpsOnly,
197-
passOnRedirect = options.passOnRedirect
198-
)
189+
val credentials =
190+
if (options.passwordValue)
191+
RepositoryCredentials(
192+
host,
193+
userOpt.map(user => PasswordOption.Value(user.get())),
194+
passwordOpt.map(password => PasswordOption.Value(password.get())),
195+
realm = realmOpt,
196+
optional = options.optional,
197+
matchHost = options.matchHost.orElse(Some(true)),
198+
httpsOnly = options.httpsOnly,
199+
passOnRedirect = options.passOnRedirect
200+
)
201+
else
202+
RepositoryCredentials(
203+
host,
204+
userOpt,
205+
passwordOpt,
206+
realm = realmOpt,
207+
optional = options.optional,
208+
matchHost = options.matchHost.orElse(Some(true)),
209+
httpsOnly = options.httpsOnly,
210+
passOnRedirect = options.passOnRedirect
211+
)
199212
val previousValueOpt =
200213
db.get(Keys.repositoryCredentials).wrapConfigException.orExit(logger)
201214
val newValue = credentials :: previousValueOpt.getOrElse(Nil)
@@ -221,7 +234,15 @@ object Config extends ScalaCommand[ConfigOptions] {
221234
.left.map(CompositeBuildException(_))
222235
.orExit(logger)
223236
val credentials =
224-
PublishCredentials(host, userOpt, passwordOpt, realm = realmOpt)
237+
if (options.passwordValue)
238+
PublishCredentials(
239+
host,
240+
userOpt.map(user => PasswordOption.Value(user.get())),
241+
passwordOpt.map(password => PasswordOption.Value(password.get())),
242+
realm = realmOpt
243+
)
244+
else
245+
PublishCredentials(host, userOpt, passwordOpt, realm = realmOpt)
225246
val previousValueOpt =
226247
db.get(Keys.publishCredentials).wrapConfigException.orExit(logger)
227248
val newValue = credentials :: previousValueOpt.getOrElse(Nil)

modules/config/src/main/scala/scala/cli/config/Keys.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ object Keys {
216216

217217
val repositoryCredentials: Key[List[RepositoryCredentials]] =
218218
new Key[List[RepositoryCredentials]] {
219-
override val description: String = "Repository credentials, syntax: value:user value:password"
220-
override val hidden: Boolean = true
219+
override val description: String =
220+
"Repository credentials, syntax: repositoryAddress value:user value:password [realm]"
221+
override val hidden: Boolean = true
221222

222223
private def asJson(credentials: RepositoryCredentials): RepositoryCredentialsAsJson =
223224
RepositoryCredentialsAsJson(
@@ -341,7 +342,7 @@ object Keys {
341342

342343
val publishCredentials: Key[List[PublishCredentials]] = new Key[List[PublishCredentials]] {
343344
override val description: String =
344-
"Publishing credentials, syntax: s1.oss.sonatype.org value:user value:password"
345+
"Publishing credentials, syntax: repositoryAddress value:user value:password [realm]"
345346
override val hidden: Boolean = true
346347

347348
private def asJson(credentials: PublishCredentials): PublishCredentialsAsJson =

modules/integration/src/test/scala/scala/cli/integration/ConfigTests.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,39 @@ class ConfigTests extends ScalaCliSuite {
290290
}
291291
}
292292

293+
test("password-value in credentials") {
294+
val configFile = os.rel / "config" / "config.json"
295+
val passwordEnvVarName = "REPO_PASSWORD"
296+
val userEnvVarName = "REPO_USER"
297+
val password = "1234"
298+
val user = "user"
299+
val envVars = Map(
300+
userEnvVarName -> user,
301+
passwordEnvVarName -> password
302+
)
303+
val configEnv = Map("SCALA_CLI_CONFIG" -> configFile.toString)
304+
305+
val keys = List("repositories.credentials", "publish.credentials")
306+
TestInputs.empty.fromRoot { root =>
307+
for (key <- keys) {
308+
os.proc(
309+
TestUtil.cli,
310+
"--power",
311+
"config",
312+
key,
313+
"s1.oss.sonatype.org",
314+
s"env:$userEnvVarName",
315+
s"env:$passwordEnvVarName",
316+
"--password-value"
317+
)
318+
.call(cwd = root, env = configEnv ++ envVars)
319+
val credsFromConfig = os.proc(TestUtil.cli, "config", key)
320+
.call(cwd = root, env = configEnv)
321+
.out.trim()
322+
expect(credsFromConfig.contains(password))
323+
expect(credsFromConfig.contains(user))
324+
}
325+
}
326+
}
327+
293328
}

website/docs/reference/commands.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ Available keys:
5959
- pgp.secret-key The PGP secret key, used for signing.
6060
- pgp.secret-key-password The PGP secret key password, used for signing.
6161
- power Globally enables power mode (the '--power' launcher flag).
62-
- publish.credentials Publishing credentials, syntax: s1.oss.sonatype.org value:user value:password
62+
- publish.credentials Publishing credentials, syntax: repositoryAddress value:user value:password [realm]
6363
- publish.user.email The 'email' user detail, used for publishing.
6464
- publish.user.name The 'name' user detail, used for publishing.
6565
- publish.user.url The 'url' user detail, used for publishing.
66-
- repositories.credentials Repository credentials, syntax: value:user value:password
66+
- repositories.credentials Repository credentials, syntax: repositoryAddress value:user value:password [realm]
6767
- repositories.default Default repository, syntax: https://first-repo.company.com https://second-repo.company.com
6868
- repositories.mirrors Repository mirrors, syntax: repositories.mirrors maven:*=https://repository.company.com/maven
6969
- suppress-warning.directives-in-multiple-files Globally suppresses warnings about directives declared in multiple source files.

website/docs/reference/scala-command/commands.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ Available keys:
5858
- pgp.secret-key The PGP secret key, used for signing.
5959
- pgp.secret-key-password The PGP secret key password, used for signing.
6060
- power Globally enables power mode (the '--power' launcher flag).
61-
- publish.credentials Publishing credentials, syntax: s1.oss.sonatype.org value:user value:password
61+
- publish.credentials Publishing credentials, syntax: repositoryAddress value:user value:password [realm]
6262
- publish.user.email The 'email' user detail, used for publishing.
6363
- publish.user.name The 'name' user detail, used for publishing.
6464
- publish.user.url The 'url' user detail, used for publishing.
65-
- repositories.credentials Repository credentials, syntax: value:user value:password
65+
- repositories.credentials Repository credentials, syntax: repositoryAddress value:user value:password [realm]
6666
- repositories.default Default repository, syntax: https://first-repo.company.com https://second-repo.company.com
6767
- repositories.mirrors Repository mirrors, syntax: repositories.mirrors maven:*=https://repository.company.com/maven
6868
- suppress-warning.directives-in-multiple-files Globally suppresses warnings about directives declared in multiple source files.

website/docs/reference/scala-command/runner-specification.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,11 @@ Available keys:
598598
- pgp.secret-key The PGP secret key, used for signing.
599599
- pgp.secret-key-password The PGP secret key password, used for signing.
600600
- power Globally enables power mode (the '--power' launcher flag).
601-
- publish.credentials Publishing credentials, syntax: s1.oss.sonatype.org value:user value:password
601+
- publish.credentials Publishing credentials, syntax: repositoryAddress value:user value:password [realm]
602602
- publish.user.email The 'email' user detail, used for publishing.
603603
- publish.user.name The 'name' user detail, used for publishing.
604604
- publish.user.url The 'url' user detail, used for publishing.
605-
- repositories.credentials Repository credentials, syntax: value:user value:password
605+
- repositories.credentials Repository credentials, syntax: repositoryAddress value:user value:password [realm]
606606
- repositories.default Default repository, syntax: https://first-repo.company.com https://second-repo.company.com
607607
- repositories.mirrors Repository mirrors, syntax: repositories.mirrors maven:*=https://repository.company.com/maven
608608
- suppress-warning.directives-in-multiple-files Globally suppresses warnings about directives declared in multiple source files.

0 commit comments

Comments
 (0)