Skip to content

Commit 77a4699

Browse files
committed
Settings: move fromCliArgs into class
Otherwise, we could inadvertently completely override all settings instead of modifying them.
1 parent 3fd6737 commit 77a4699

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

cli/src/main/scala/mdoc/internal/cli/Settings.scala

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,19 @@ case class Settings(
247247
cwd = dir
248248
)
249249
}
250+
251+
def withCliArgs(args: List[String]): Configured[Settings] = {
252+
Conf.parseCliArgs[Settings](args).andThen { conf =>
253+
val withCwd = conf.get[String]("cwd").map { x =>
254+
implicit val defaultCwd: AbsolutePath = this.cwd
255+
withWorkingDirectory(AbsolutePath(x))
256+
}.getOrElse(this)
257+
val base = conf.get[String]("propertyFileName")
258+
.map(withCwd.withPropertiesFromFile).getOrElse(withCwd)
259+
conf.as[Settings](Settings.decoder(base)).map(_.addSite(base.site))
260+
}
261+
}
262+
250263
}
251264

252265
object Settings { // extends MetaconfigScalametaImplicits with Decoders with SettingsGeneric {
@@ -306,20 +319,8 @@ object Settings { // extends MetaconfigScalametaImplicits with Decoders with Set
306319
generic.deriveDecoder[Settings](base)
307320
}
308321

309-
def fromCliArgs(args: List[String], workingDirectory: => AbsolutePath): Configured[Settings] = {
310-
Conf
311-
.parseCliArgs[Settings](args)
312-
.andThen { conf =>
313-
implicit val defaultCwd: AbsolutePath = workingDirectory
314-
val base = Settings(
315-
conf.get[String]("cwd").map(AbsolutePath(_)).getOrElse(defaultCwd),
316-
conf.get[String]("propertyFileName").getOrElse("mdoc.properties")
317-
)
318-
conf
319-
.as[Settings](decoder(base))
320-
.map(_.addSite(base.site))
321-
}
322-
}
322+
def fromCliArgs(args: List[String], workingDirectory: AbsolutePath): Configured[Settings] =
323+
Settings(workingDirectory, "mdoc.properties").withCliArgs(args)
323324

324325
def fromCliArgs(args: List[String], workingDirectory: Path): Configured[Settings] =
325326
fromCliArgs(args, AbsolutePath(workingDirectory))

mdoc/src/main/scala/mdoc/MainSettings.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ final class MainSettings private (
2323
def withArgs(args: List[String]): MainSettings = {
2424
if (args.isEmpty) this
2525
else {
26-
Settings.fromCliArgs(args, settings.cwd.toNIO) match {
26+
settings.withCliArgs(args) match {
2727
case Configured.Ok(newSettings) =>
28-
copy(settings = newSettings)
28+
if (newSettings eq settings) this else copy(settings = newSettings)
2929
case Configured.NotOk(error) =>
3030
throw new IllegalArgumentException(error.toString())
3131
}

0 commit comments

Comments
 (0)