diff options
Diffstat (limited to 'hv-collector-utils/src/main')
3 files changed, 26 insertions, 3 deletions
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 <A> Either<A, A>.flatten() = fold(::identity, ::identity) fun <A> AtomicReference<A>.getOption() = Option.fromNullable(get()) + +fun <A> Option.Companion.fromNullablesChain(firstValue: A?, vararg nextValues: () -> A?): Option<A> = + 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<T>(private val parser: CommandLineParser) { protected fun stringPathToPath(path: String): Path = Paths.get(File(path).toURI()) - private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption): Option<String> = - 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" + } } |