Add support for global and user configuration files (#73)

* [core] ConfigurationBuilder reads user and global config files

* [changelog] updated

* [readme] updated
This commit is contained in:
Paul Campbell 2019-06-20 17:41:08 +01:00 committed by GitHub
parent 910688ee32
commit 0a92667d3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 6 deletions

View file

@ -9,15 +9,31 @@ The format is based on [[https://keepachangelog.com/en/1.0.0/][Keep a Changelog]
** Added
- ~-d~, ~--debug~ flag for log messages
- Add ~thorp-lib~ module (#66)
- Enable running outside of sbt (#55)
- ~-d~, ~--debug~ flag for log messages (#60)
- Read config from ~.thorp.conf~ in source directory (#71)
- Read config from ~$HOME/.config/thorp.conf~ and ~/etc/thorp.conf~
(#73)
** Changed
- Suppress Transfer event messages
- Rename project as 'thorp' (#75)
- Suppress Transfer event messages (#64)
- Better error message when source not found (#51)
- Reduced logging (#59)
** Fixed
- Error when calculating md5 hash for large files (#56)
** Removed
- ~-v~ verbosity flag
- ~-v~ verbosity flag (#63)
** Dependencies
- Upgrade ~aws-java-sdk-s3~ from ~1.11.569~ to ~1.11.570~ (#57)
* [0.4.0] - 2019-06-11

View file

@ -24,8 +24,28 @@ hash of the file contents.
-d, --debug Enable debug logging
#+end_example
If you don't provide a ~source~ the current diretory will be used.
The ~--include~ and ~--exclude~ parameters can be used more than once.
* Configuration
Configuration will be read from these files:
- Global: ~/etc/thorp.conf~
- User: ~~/.config/thorp.conf~
- Source: ~${source}/.thorp.conf~
Command line arguments override those in Source, which override those
in User, which override those Global, which override any built-in
config.
Built-in config consists of using the current working directory as the
~source~.
Note, that ~include~ and ~exclude~ are cumulative across all
configuration files.
* Behaviour
When considering a local file, the following table governs what should happen:

View file

@ -6,8 +6,8 @@ import java.nio.file.Paths
import cats.data.NonEmptyChain
import cats.effect.IO
import net.kemitix.thorp.core.ConfigValidator.validateConfig
import net.kemitix.thorp.domain.Config
import net.kemitix.thorp.core.ParseConfigFile.parseFile
import net.kemitix.thorp.domain.Config
/**
* Builds a configuration from settings in a file within the
@ -22,8 +22,10 @@ trait ConfigurationBuilder {
def buildConfig(priorityOptions: Seq[ConfigOption]): IO[Either[NonEmptyChain[ConfigValidation], Config]] = {
val source = findSource(priorityOptions)
for {
options <- sourceOptions(source)
collected = priorityOptions ++ options
sourceOptions <- sourceOptions(source)
userOptions <- userOptions()
globalOptions <- globalOptions()
collected = priorityOptions ++ sourceOptions ++ userOptions ++ globalOptions
config = collateOptions(collected)
} yield validateConfig(config).toEither
}
@ -37,6 +39,14 @@ trait ConfigurationBuilder {
private def sourceOptions(source: File): IO[Seq[ConfigOption]] =
readFile(source, ".thorp.conf")
private def userOptions(): IO[Seq[ConfigOption]] =
readFile(userHome, ".config/thorp.conf")
private def globalOptions(): IO[Seq[ConfigOption]] =
parseFile(Paths.get("/etc/thorp.conf"))
private def userHome = new File(System.getProperty("user.home"))
private def readFile(source: File, filename: String): IO[Seq[ConfigOption]] =
parseFile(source.toPath.resolve(filename))