From 199edc49a418ab015ad3a54a5750f1a3f485b7e7 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Tue, 4 Sep 2018 09:59:20 +0200 Subject: Read config from environment variables The application configuration should be read from command line options as well as from environment variables. The priority: cmd-line over env over defaults. Change-Id: I785fd1fbaf66f3eab84a162f037153f1688ed791 Issue-ID: DCAEGEN2-748 Signed-off-by: Piotr Jaszczyk --- .../org/onap/dcae/collectors/veshv/utils/arrow/core.kt | 12 ++++++++++++ .../veshv/utils/commandline/ArgBasedConfiguration.kt | 6 ++++-- .../collectors/veshv/utils/commandline/CommandLineOption.kt | 11 ++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) (limited to 'hv-collector-utils/src/main/kotlin') diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt index 844d18d8..2d538b72 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt @@ -20,8 +20,12 @@ package org.onap.dcae.collectors.veshv.utils.arrow import arrow.core.Either +import arrow.core.None import arrow.core.Option +import arrow.core.Some import arrow.core.identity +import arrow.syntax.collections.firstOption +import java.util.* import java.util.concurrent.atomic.AtomicReference /** @@ -32,3 +36,11 @@ import java.util.concurrent.atomic.AtomicReference fun Either.flatten() = fold(::identity, ::identity) fun AtomicReference.getOption() = Option.fromNullable(get()) + +fun Option.Companion.fromNullablesChain(firstValue: A?, vararg nextValues: () -> A?): Option = + if (firstValue != null) + Option.just(firstValue) + else nextValues.asSequence() + .map { it() } + .filter { it != null } + .firstOption() diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt index 16634889..1ebe4e48 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt @@ -27,6 +27,7 @@ import arrow.core.getOrElse import org.apache.commons.cli.CommandLine import org.apache.commons.cli.CommandLineParser import org.apache.commons.cli.Options +import org.onap.dcae.collectors.veshv.utils.arrow.fromNullablesChain import java.io.File import java.nio.file.Path import java.nio.file.Paths @@ -77,6 +78,7 @@ abstract class ArgBasedConfiguration(private val parser: CommandLineParser) { protected fun stringPathToPath(path: String): Path = Paths.get(File(path).toURI()) - private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption): Option = - Option.fromNullable(getOptionValue(cmdLineOpt.option.opt)) + private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption) = Option.fromNullablesChain( + getOptionValue(cmdLineOpt.option.opt), + { System.getenv(cmdLineOpt.environmentVariableName()) }) } diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt index 836a05df..3a154db2 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt @@ -117,5 +117,14 @@ enum class CommandLineOption(val option: Option) { .longOpt("dummy") .desc("If present will start in dummy mode (dummy external services)") .build() - ), + ); + + fun environmentVariableName(prefix: String = DEFAULT_ENV_PREFIX): String = + option.longOpt.toUpperCase().replace('-', '_').let { mainPart -> + "${prefix}_${mainPart}" + } + + companion object { + private const val DEFAULT_ENV_PREFIX = "VESHV" + } } -- cgit 1.2.3-korg