Skip to content

Commit

Permalink
#156 Split DatamaintainConfig (#205)
Browse files Browse the repository at this point in the history
feat: #156 Split DatamaintainConfig
  • Loading branch information
nroulon authored Feb 7, 2023
1 parent 93a9c38 commit 3a3d7cd
Show file tree
Hide file tree
Showing 29 changed files with 548 additions and 331 deletions.
6 changes: 4 additions & 2 deletions docs/as-dependency.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ You can create `Config objects` directly from constructor in pure kotlin:
```kotlin
val driverConfig: DatamaintainDriverConfig = MongoDriverConfig(uri = "mongodb://localhost:27017/datamaintain")
val config = DatamaintainConfig(
path = Paths.get("/scripts"),
identifierRegex = "(.*?)_.*",
scanner = DatamaintainScannerConfig(
path = Paths.get("/scripts"),
identifierRegex = "(.*?)_.*",
),
driverConfig = driverConfig
)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import datamaintain.core.exception.DatamaintainException
import kotlin.system.exitProcess

fun defaultUpdateDbRunner(config: DatamaintainConfig) {
Datamaintain(config).updateDatabase().print(config.verbose, porcelain = config.porcelain)
Datamaintain(config).updateDatabase().print(config.logs.verbose, porcelain = config.logs.porcelain)
}

abstract class DatamaintainCliUpdateDbCommand(
Expand All @@ -19,8 +19,8 @@ abstract class DatamaintainCliUpdateDbCommand(
try {
runner(config)
} catch (e: DatamaintainException) {
val verbose: Boolean = config.verbose
val porcelain: Boolean = config.porcelain
val verbose: Boolean = config.logs.verbose
val porcelain: Boolean = config.logs.porcelain

echo("Error at step ${e.step}", err = true)
e.report.print(verbose, porcelain = porcelain)
Expand Down
32 changes: 20 additions & 12 deletions modules/cli/src/test/kotlin/datamaintain/cli/app/PrintConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ internal class PrintConfigTest : BaseCliTest() {
get { get(index++).message }.matches("- working directory -> .*/datamaintain/modules/cli".toRegex())
get { get(index++).message }.isEqualTo("- path -> /myPath")
get { get(index++).message }.isEqualTo("- identifier regex -> (.*)")
get { get(index++).message }.isEqualTo("- script action -> MARK_AS_EXECUTED")
get { get(index++).message }.isEqualTo("- execution mode -> NORMAL")
get { get(index++).message }.isEqualTo("- create tags from folder -> false")
get { get(index++).message }.isEqualTo("- tags -> []")
get { get(index++).message }.isEqualTo("- whitelisted tags -> []")
get { get(index++).message }.isEqualTo("- blacklisted tags -> []")
get { get(index++).message }.isEqualTo("- tags to play again -> []")
get { get(index++).message }.isEqualTo("- Allow override executed script -> false")
get { get(index++).message }.isEqualTo("- rules -> []")
get { get(index++).message }.isEqualTo("- execution mode -> NORMAL")
get { get(index++).message }.isEqualTo("- allow override executed script -> false")
get { get(index++).message }.isEqualTo("- script action -> MARK_AS_EXECUTED")
get { get(index++).message }.isEqualTo("- flags -> []")
get { get(index++).message }.isEqualTo("- verbose -> false")
get { get(index++).message }.isEqualTo("- porcelain -> false")
}
Expand Down Expand Up @@ -82,14 +84,16 @@ internal class PrintConfigTest : BaseCliTest() {
get { get(index++).message }.isEqualTo("- name -> childConfig")
get { get(index++).message }.isEqualTo("- path -> /myPath")
get { get(index++).message }.isEqualTo("- identifier regex -> (.*)")
get { get(index++).message }.isEqualTo("- script action -> MARK_AS_EXECUTED")
get { get(index++).message }.isEqualTo("- execution mode -> NORMAL")
get { get(index++).message }.isEqualTo("- create tags from folder -> false")
get { get(index++).message }.isEqualTo("- tags -> [TagMatcher(tag=Tag(name=MYTAG2), globPaths=[/pathMatcher2])]")
get { get(index++).message }.isEqualTo("- whitelisted tags -> []")
get { get(index++).message }.isEqualTo("- blacklisted tags -> []")
get { get(index++).message }.isEqualTo("- tags to play again -> []")
get { get(index++).message }.isEqualTo("- Allow override executed script -> false")
get { get(index++).message }.isEqualTo("- rules -> []")
get { get(index++).message }.isEqualTo("- execution mode -> NORMAL")
get { get(index++).message }.isEqualTo("- allow override executed script -> false")
get { get(index++).message }.isEqualTo("- script action -> MARK_AS_EXECUTED")
get { get(index++).message }.isEqualTo("- flags -> []")
get { get(index++).message }.isEqualTo("- verbose -> false")
get { get(index++).message }.isEqualTo("- porcelain -> false")
}
Expand Down Expand Up @@ -128,14 +132,16 @@ internal class PrintConfigTest : BaseCliTest() {
get { get(index++).message }.matches("- working directory -> .*/datamaintain/modules/cli".toRegex())
get { get(index++).message }.isEqualTo("- path -> /myPath")
get { get(index++).message }.isEqualTo("- identifier regex -> v.*_")
get { get(index++).message }.isEqualTo("- script action -> RUN")
get { get(index++).message }.isEqualTo("- execution mode -> NORMAL")
get { get(index++).message }.isEqualTo("- create tags from folder -> false")
get { get(index++).message }.isEqualTo("- tags -> [TagMatcher(tag=Tag(name=MYTAG2), globPaths=[/pathMatcher2]), TagMatcher(tag=Tag(name=MYTAG1), globPaths=[/pathMatcher1])]")
get { get(index++).message }.isEqualTo("- whitelisted tags -> [Tag(name=MYTAG2)]")
get { get(index++).message }.isEqualTo("- blacklisted tags -> [Tag(name=MYTAG1)]")
get { get(index++).message }.isEqualTo("- tags to play again -> []")
get { get(index++).message }.isEqualTo("- Allow override executed script -> false")
get { get(index++).message }.isEqualTo("- rules -> []")
get { get(index++).message }.isEqualTo("- execution mode -> NORMAL")
get { get(index++).message }.isEqualTo("- allow override executed script -> false")
get { get(index++).message }.isEqualTo("- script action -> RUN")
get { get(index++).message }.isEqualTo("- flags -> []")
get { get(index++).message }.isEqualTo("- verbose -> true")
get { get(index++).message }.isEqualTo("- porcelain -> true")
}
Expand Down Expand Up @@ -176,14 +182,16 @@ internal class PrintConfigTest : BaseCliTest() {
get { get(index++).message }.isEqualTo("- name -> childConfig")
get { get(index++).message }.isEqualTo("- path -> /myPath")
get { get(index++).message }.isEqualTo("- identifier regex -> v.*_")
get { get(index++).message }.isEqualTo("- script action -> RUN")
get { get(index++).message }.isEqualTo("- execution mode -> DRY")
get { get(index++).message }.isEqualTo("- create tags from folder -> false")
get { get(index++).message }.isEqualTo("- tags -> [TagMatcher(tag=Tag(name=MYTAG2), globPaths=[/pathMatcher2]), TagMatcher(tag=Tag(name=MYTAG1), globPaths=[/pathMatcher1])]")
get { get(index++).message }.isEqualTo("- whitelisted tags -> [Tag(name=MYTAG2)]")
get { get(index++).message }.isEqualTo("- blacklisted tags -> [Tag(name=MYTAG1)]")
get { get(index++).message }.isEqualTo("- tags to play again -> []")
get { get(index++).message }.isEqualTo("- Allow override executed script -> false")
get { get(index++).message }.isEqualTo("- rules -> []")
get { get(index++).message }.isEqualTo("- execution mode -> DRY")
get { get(index++).message }.isEqualTo("- allow override executed script -> false")
get { get(index++).message }.isEqualTo("- script action -> RUN")
get { get(index++).message }.isEqualTo("- flags -> []")
get { get(index++).message }.isEqualTo("- verbose -> true")
get { get(index++).message }.isEqualTo("- porcelain -> true")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class MarkOneScriptAsExecutedTest : BaseCliTest() {
runMarkScriptAsExecuted(markScriptAsExecutedArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.path).isEqualTo(Paths.get(path))
expectThat(configWrapper.datamaintainConfig!!.scanner.path).isEqualTo(Paths.get(path))
}

@Nested
Expand All @@ -43,7 +43,7 @@ internal class MarkOneScriptAsExecutedTest : BaseCliTest() {
runMarkScriptAsExecuted(markScriptAsExecutedArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.verbose).isTrue()
expectThat(configWrapper.datamaintainConfig!!.logs.verbose).isTrue()
}

@Test
Expand All @@ -54,7 +54,7 @@ internal class MarkOneScriptAsExecutedTest : BaseCliTest() {
runMarkScriptAsExecuted()

// Then
expectThat(configWrapper.datamaintainConfig!!.verbose).isFalse()
expectThat(configWrapper.datamaintainConfig!!.logs.verbose).isFalse()
}
}

Expand All @@ -66,7 +66,7 @@ internal class MarkOneScriptAsExecutedTest : BaseCliTest() {
runMarkScriptAsExecuted()

// Then
expectThat(configWrapper.datamaintainConfig!!.defaultScriptAction).isEqualTo(ScriptAction.MARK_AS_EXECUTED)
expectThat(configWrapper.datamaintainConfig!!.executor.defaultScriptAction).isEqualTo(ScriptAction.MARK_AS_EXECUTED)
}
}
}
Expand All @@ -79,4 +79,4 @@ internal class MarkOneScriptAsExecutedTest : BaseCliTest() {
), markScriptAsExecutedArguments
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datamaintain.cli.app.update.db

import datamaintain.cli.app.BaseCliTest
import datamaintain.core.config.DatamaintainConfig
import datamaintain.core.script.ScriptAction
import datamaintain.core.script.Tag
import datamaintain.core.script.TagMatcher
Expand Down Expand Up @@ -36,7 +35,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.path).isEqualTo(Paths.get(path))
expectThat(configWrapper.datamaintainConfig!!.scanner.path).isEqualTo(Paths.get(path))
}

@Test
Expand All @@ -52,7 +51,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.identifierRegex.pattern).isEqualTo(identifierRegex)
expectThat(configWrapper.datamaintainConfig!!.scanner.identifierRegex.pattern).isEqualTo(identifierRegex)
}

@Nested
Expand All @@ -72,7 +71,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.executionMode).isEqualTo(executionMode)
expectThat(configWrapper.datamaintainConfig!!.executor.executionMode).isEqualTo(executionMode)
}
}

Expand All @@ -87,7 +86,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.verbose).isTrue()
expectThat(configWrapper.datamaintainConfig!!.logs.verbose).isTrue()
}

@Test
Expand All @@ -98,7 +97,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb()

// Then
expectThat(configWrapper.datamaintainConfig!!.verbose).isFalse()
expectThat(configWrapper.datamaintainConfig!!.logs.verbose).isFalse()
}
}

Expand All @@ -113,7 +112,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.doesCreateTagsFromFolder).isTrue()
expectThat(configWrapper.datamaintainConfig!!.scanner.doesCreateTagsFromFolder).isTrue()
}

@Test
Expand All @@ -124,7 +123,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb()

// Then
expectThat(configWrapper.datamaintainConfig!!.doesCreateTagsFromFolder).isFalse()
expectThat(configWrapper.datamaintainConfig!!.scanner.doesCreateTagsFromFolder).isFalse()
}
}

Expand All @@ -145,7 +144,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.tagsMatchers)
expectThat(configWrapper.datamaintainConfig!!.scanner.tagsMatchers)
.containsExactlyInAnyOrder(tagMatcher1, tagMatcher2)
}

Expand All @@ -156,20 +155,20 @@ internal class UpdateDbTest : BaseCliTest() {
inner class TagsList {
@Test
fun `should build config with blacklisted tags`() {
testBuildConfigWithTagsList("--blacklisted-tags", DatamaintainConfig::blacklistedTags)
testBuildConfigWithTagsList("--blacklisted-tags", "blacklisted")
}

@Test
fun `should build config with whitelisted tags`() {
testBuildConfigWithTagsList("--whitelisted-tags", DatamaintainConfig::whitelistedTags)
testBuildConfigWithTagsList("--whitelisted-tags", "whitelisted")
}

@Test
fun `should build config with tags to play again`() {
testBuildConfigWithTagsList("--tags-to-play-again", DatamaintainConfig::tagsToPlayAgain)
testBuildConfigWithTagsList("--tags-to-play-again", "play-again")
}

private fun testBuildConfigWithTagsList(key: String, getter: KProperty1<DatamaintainConfig, Set<Tag>>) {
private fun testBuildConfigWithTagsList(key: String, tagsToTest: String) {
// Given
val tagsList = setOf("MYTAG", "MYOTHERTAG")

Expand All @@ -182,7 +181,14 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(getter.get(configWrapper.datamaintainConfig!!))
val tags = when (tagsToTest) {
"blacklisted" -> configWrapper.datamaintainConfig!!.filter.blacklistedTags
"whitelisted" -> configWrapper.datamaintainConfig!!.filter.whitelistedTags
"play-again" -> configWrapper.datamaintainConfig!!.pruner.tagsToPlayAgain
else -> error("unknown tagsToTest $tagsToTest")
}

expectThat(tags)
.map { it.name }
.containsExactlyInAnyOrder(tagsList)
}
Expand All @@ -205,7 +211,7 @@ internal class UpdateDbTest : BaseCliTest() {
expectThat(configWrapper) {
get { datamaintainConfig }.isNotNull()
}
expectThat(configWrapper.datamaintainConfig!!.checkRules.toList()) {
expectThat(configWrapper.datamaintainConfig!!.checker.rules) {
hasSize(1)
first().isEqualTo(SameScriptsAsExecutedCheck.NAME)
}
Expand All @@ -227,7 +233,7 @@ internal class UpdateDbTest : BaseCliTest() {
expectThat(configWrapper) {
get { datamaintainConfig }.isNotNull()
}
expectThat(configWrapper.datamaintainConfig!!.checkRules.toList()) {
expectThat(configWrapper.datamaintainConfig!!.checker.rules) {
hasSize(2)
first().isEqualTo(SameScriptsAsExecutedCheck.NAME)
last().isEqualTo(SameScriptsAsExecutedCheck.NAME)
Expand All @@ -248,7 +254,7 @@ internal class UpdateDbTest : BaseCliTest() {
expectThat(configWrapper) {
get { datamaintainConfig }.isNotNull()
}
expectThat(configWrapper.datamaintainConfig!!.overrideExecutedScripts) {
expectThat(configWrapper.datamaintainConfig!!.executor.overrideExecutedScripts) {
isFalse()
}
}
Expand All @@ -265,7 +271,7 @@ internal class UpdateDbTest : BaseCliTest() {
expectThat(configWrapper) {
get { datamaintainConfig }.isNotNull()
}
expectThat(configWrapper.datamaintainConfig!!.overrideExecutedScripts) {
expectThat(configWrapper.datamaintainConfig!!.executor.overrideExecutedScripts) {
isTrue()
}
}
Expand All @@ -284,7 +290,7 @@ internal class UpdateDbTest : BaseCliTest() {
expectThat(configWrapper) {
get { datamaintainConfig }.isNotNull()
}
expectThat(configWrapper.datamaintainConfig!!.porcelain) {
expectThat(configWrapper.datamaintainConfig!!.logs.porcelain) {
isFalse()
}
}
Expand All @@ -301,7 +307,7 @@ internal class UpdateDbTest : BaseCliTest() {
expectThat(configWrapper) {
get { datamaintainConfig }.isNotNull()
}
expectThat(configWrapper.datamaintainConfig!!.porcelain) {
expectThat(configWrapper.datamaintainConfig!!.logs.porcelain) {
isTrue()
}
}
Expand All @@ -321,7 +327,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.defaultScriptAction).isEqualTo(scriptAction)
expectThat(configWrapper.datamaintainConfig!!.executor.defaultScriptAction).isEqualTo(scriptAction)
}
}

Expand Down Expand Up @@ -434,7 +440,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.flags) {
expectThat(configWrapper.datamaintainConfig!!.executor.flags) {
containsExactly("MY_TEST_FLAG")
}
}
Expand All @@ -448,7 +454,7 @@ internal class UpdateDbTest : BaseCliTest() {
runUpdateDb(updateDbArguments)

// Then
expectThat(configWrapper.datamaintainConfig!!.flags) {
expectThat(configWrapper.datamaintainConfig!!.executor.flags) {
containsExactly("MY_TEST_FLAG1", "MY_TEST_FLAG2")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ private val logger = KotlinLogging.logger {}
class Datamaintain(config: DatamaintainConfig) {

init {
if (config.verbose && !config.porcelain) {
if (config.logs.verbose && !config.logs.porcelain) {
config.log()
config.driverConfig.log()
config.scanner.log()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package datamaintain.core.config

import mu.KotlinLogging

private val logger = KotlinLogging.logger {}

data class DatamaintainCheckerConfig @JvmOverloads constructor(
val rules: List<String> = emptyList(),
) {
fun log() {
rules.let { logger.info { "- rules -> $it" } }
}
}

Loading

0 comments on commit 3a3d7cd

Please sign in to comment.