Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Optionally check configureExecutable config value path actually exists #348

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions core/src/main/scala/dagr/core/config/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,30 @@ private[config] trait ConfigurationLike extends LazyLogging {
}

/**
* Attempts to determine the path to an executable, first by looking it up in config,
* and if that fails, by attempting to locate it on the system path. If both fail then
* an exception is raised.
* Attempts to determine the path to an executable, first by looking it up in the config and optionally checking to
* see if the path exists. If that fails, we will then locate the executable on the system path. If both strategies
* fail, then an exception is raised.
*
* @param path the configuration path to look up
* @param executable the default name of the executable
* @return An absolute path to the executable to use
* @param mustExist ensure the configuration path to look up references a file which exists
clintval marked this conversation as resolved.
Show resolved Hide resolved
* @return an absolute path to the executable to use
* @throws Generic when the executable cannot be configured
*/
def configureExecutable(path: String, executable: String) : Path = {
def configureExecutable(path: String, executable: String, mustExist: Boolean = false) : Path = {
clintval marked this conversation as resolved.
Show resolved Hide resolved
Configuration.RequestedKeys += path

optionallyConfigure[Path](path) match {
case Some(exec) => exec
case None => findInPath(executable) match {
case Some(exec) => exec
case None => throw new Generic(s"Could not configurable executable. Config path '$path' is not defined and executable '$executable' is not in PATH.")
}
}
optionallyConfigure[Path](path)
.filterNot(mustExist && !Files.exists(_))
clintval marked this conversation as resolved.
Show resolved Hide resolved
.orElse(findInPath(executable))
.map(_.toAbsolutePath)
.getOrElse(
throw new Generic(
s"Could not configure executable."
+ s" Config path '$path' is not defined, or the config value references a file which does not exist,"
+ s" and executable '$executable' is not in the system path."
clintval marked this conversation as resolved.
Show resolved Hide resolved
)
)
}

/**
Expand Down
5 changes: 5 additions & 0 deletions core/src/test/scala/dagr/core/config/ConfigurationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class ConfigurationTest extends UnitSpec with CaptureSystemStreams {
java.getFileName.toString shouldBe "java"
Files.isExecutable(java) shouldBe true

// If the config key is found, but the value, as a path, does not exist, then fallback to the system path.
java = conf.configureExecutable("some-executable", "java", mustExist = true)
java.getFileName.toString shouldBe "java"
Files.isExecutable(java) shouldBe true

// the bin directory should not be found, and so should fall back on the system path, and find the java executable
java = conf.configureExecutableFromBinDirectory("path-does-not-exist", "java")
java.getFileName.toString shouldBe "java"
Expand Down