This repository was archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Add a global configuration option to control when to fall back to system #350
Draft
nh13
wants to merge
1
commit into
main
Choose a base branch
from
nh_config_fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,15 +46,16 @@ object Configuration extends ConfigurationLike { | |
|
||
// Keys for configuration values used in dagr core | ||
object Keys { | ||
val CommandLineName = "dagr.command-line-name" | ||
val ColorStatus = "dagr.color-status" | ||
val SystemPath = "dagr.path" | ||
val PackageList = "dagr.package-list" | ||
val ScriptDirectory = "dagr.script-directory" | ||
val LogDirectory = "dagr.log-directory" | ||
val SystemCores = "dagr.system-cores" | ||
val SystemMemory = "dagr.system-memory" | ||
val PrintArgs = "dagr.print-args" | ||
val CommandLineName = "dagr.command-line-name" | ||
val ColorStatus = "dagr.color-status" | ||
val SystemPath = "dagr.path" | ||
val PackageList = "dagr.package-list" | ||
val ScriptDirectory = "dagr.script-directory" | ||
val LogDirectory = "dagr.log-directory" | ||
val SystemCores = "dagr.system-cores" | ||
val SystemMemory = "dagr.system-memory" | ||
val PrintArgs = "dagr.print-args" | ||
val FallBackToSystemPath = "dagr.fall-back-to-system-path" | ||
} | ||
|
||
// The global configuration instance | ||
|
@@ -165,25 +166,40 @@ private[config] trait ConfigurationLike extends LazyLogging { | |
} | ||
} | ||
|
||
/** True to fall back to the searching the system path when a configuration key exists but the executable does not. | ||
* False to throw an exception if the configuration key exists but the executable does not. */ | ||
private def fallBackToSystemPath: Boolean = configure(Configuration.Keys.FallBackToSystemPath, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to think about if this could be a lazy val (i.e. is config immutable) |
||
|
||
/** | ||
* 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. | ||
* | ||
* If the configuration key exists but the value (i.e. executable path) does not, the fallback behavior is controlled | ||
* by the `Configuration.Keys.FallBackToSystemPath` configuration value (default is false). If false, an exception | ||
* will be raised if the configuration key exists but the executable (value) does not exist, otherwise the system | ||
* path is searched. | ||
* | ||
* @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 | ||
*/ | ||
def configureExecutable(path: String, executable: String) : Path = { | ||
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) | ||
// always keep the value if we should not fall back, otherwise if we should fallback, keep the value if the file | ||
// exists | ||
.filter(!fallBackToSystemPath || Files.exists(_)) | ||
.orElse(findInPath(executable)) | ||
.map(_.toAbsolutePath) | ||
.getOrElse( | ||
throw new Generic( | ||
s"Could not configure executable. Config path `$path` is not defined " | ||
+ (if (fallBackToSystemPath) "" else s"or the config value references a file which does not exist, ") | ||
+ s"and executable '$executable' is not on the system path." | ||
) | ||
) | ||
} | ||
|
||
/** | ||
|
@@ -224,11 +240,6 @@ private[config] trait ConfigurationLike extends LazyLogging { | |
*/ | ||
protected def systemPath : Seq[Path] = config.getString(Configuration.Keys.SystemPath).split(File.pathSeparatorChar).view.map(pathTo(_)) | ||
|
||
/** Removes various characters from the simple class name, for scala class names. */ | ||
private def sanitizeSimpleClassName(className: String): String = { | ||
className.replaceFirst("[$].*$", "") | ||
} | ||
|
||
/** Searches the system path for the executable and return the full path. */ | ||
private def findInPath(executable: String) : Option[Path] = { | ||
systemPath.map(p => p.resolve(executable)).find(ex => Files.exists(ex)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?