From 2f790c15ba5cd5a20207edffbbee1e325b46f07d Mon Sep 17 00:00:00 2001 From: kjaniak Date: Thu, 6 Sep 2018 10:07:17 +0200 Subject: Enable env parameters read Disabling require option on mandatory parameters to fetch them from env variables. Change-Id: I007dea1a7f369a04479801aa508cf1034ac1341a Issue-ID: DCAEGEN2-741 Signed-off-by: kjaniak --- .../impl/config/ArgDcaeAppSimConfigurationTest.kt | 2 +- .../onap/dcae/collectors/veshv/utils/arrow/core.kt | 3 -- .../utils/commandline/ArgBasedConfiguration.kt | 8 +++--- .../veshv/utils/commandline/CommandLineOption.kt | 26 ++++++++--------- .../veshv/utils/commandline/WrongArgumentError.kt | 33 +++++++++++++++++++--- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/config/ArgDcaeAppSimConfigurationTest.kt b/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/config/ArgDcaeAppSimConfigurationTest.kt index e7a22fcf..7137fe12 100644 --- a/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/config/ArgDcaeAppSimConfigurationTest.kt +++ b/hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/config/ArgDcaeAppSimConfigurationTest.kt @@ -115,7 +115,7 @@ internal class ArgDcaeAppSimConfigurationTest : Spek({ given("listen port is missing") { it("should throw exception") { assertThat(cut.parseExpectingFailure( - "-p", kafkaTopics, + "-p", listenPort, "-s", kafkaBootstrapServers )).isInstanceOf(WrongArgumentError::class.java) } 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 a99fef5e..7381592d 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,12 +20,9 @@ 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 /** 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 1ebe4e48..da6f2d0a 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 @@ -36,19 +36,19 @@ abstract class ArgBasedConfiguration(private val parser: CommandLineParser) { abstract val cmdLineOptionsList: List fun parse(args: Array): Either { - val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption) val parseResult = Try { + val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption) parser.parse(commandLineOptions, args) } return parseResult .toEither() - .mapLeft { ex -> WrongArgumentError(ex, commandLineOptions) } + .mapLeft { ex -> WrongArgumentError(ex, cmdLineOptionsList) } .map(this::getConfiguration) .flatMap { it.toEither { WrongArgumentError( - "Unexpected error when parsing command line arguments", - commandLineOptions) + message = "Unexpected error when parsing command line arguments", + cmdLineOptionsList = cmdLineOptionsList) } } } 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 3a154db2..9379c409 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 @@ -22,7 +22,7 @@ package org.onap.dcae.collectors.veshv.utils.commandline import org.apache.commons.cli.Option -enum class CommandLineOption(val option: Option) { +enum class CommandLineOption(val option: Option, val required: Boolean = false) { HEALTH_CHECK_API_PORT(Option.builder("H") .longOpt("health-check-api-port") .hasArg() @@ -31,17 +31,17 @@ enum class CommandLineOption(val option: Option) { ), LISTEN_PORT(Option.builder("p") .longOpt("listen-port") - .required() .hasArg() .desc("Listen port") - .build() + .build(), + required = true ), CONSUL_CONFIG_URL(Option.builder("c") .longOpt("config-url") - .required() .hasArg() .desc("URL of ves configuration on consul") - .build() + .build(), + required = true ), CONSUL_FIRST_REQUEST_DELAY(Option.builder("d") .longOpt("first-request-delay") @@ -57,31 +57,31 @@ enum class CommandLineOption(val option: Option) { ), VES_HV_PORT(Option.builder("v") .longOpt("ves-port") - .required() .hasArg() .desc("VesHvCollector port") - .build() + .build(), + required = true ), VES_HV_HOST(Option.builder("h") .longOpt("ves-host") - .required() .hasArg() .desc("VesHvCollector host") - .build() + .build(), + required = true ), KAFKA_SERVERS(Option.builder("s") .longOpt("kafka-bootstrap-servers") - .required() .hasArg() .desc("Comma-separated Kafka bootstrap servers in : format") - .build() + .build(), + required = true ), KAFKA_TOPICS(Option.builder("f") .longOpt("kafka-topics") - .required() .hasArg() .desc("Comma-separated Kafka topics") - .build() + .build(), + required = true ), SSL_DISABLE(Option.builder("l") .longOpt("ssl-disable") diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentError.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentError.kt index 632858b2..61a461f5 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentError.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentError.kt @@ -19,16 +19,20 @@ */ package org.onap.dcae.collectors.veshv.utils.commandline +import arrow.core.Option import org.apache.commons.cli.HelpFormatter import org.apache.commons.cli.Options data class WrongArgumentError( val message: String, - private val options: Options, - val cause: Throwable? = null) { + val cause: Throwable? = null, + val cmdLineOptionsList: List) { - constructor(par: Throwable, options: Options) : this(par.message ?: "", options, par) + constructor(par: Throwable, cmdLineOptionsList: List) : + this(par.message ?: "", + par, + cmdLineOptionsList) fun printMessage() { println(message) @@ -36,7 +40,28 @@ data class WrongArgumentError( fun printHelp(programName: String) { val formatter = HelpFormatter() - formatter.printHelp(programName, options) + val footer = "All parameters can be specified as environment variables using upper-snake-case full " + + "name with prefix `VESHV_`." + + formatter.printHelp( + programName, + generateRequiredParametersNote(), + getOptions(), + footer) } + + private fun getOptions(): Options = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption) + + private fun generateRequiredParametersNote(): String { + val requiredParams = Option.fromNullable(cmdLineOptionsList.filter { it.required } + .takeUnless { it.isEmpty() }) + return requiredParams.fold( + { "" }, + { it.map {commandLineOption -> commandLineOption.option.opt } + .joinToString(prefix = "Required parameters: ", separator = ", ") + } + ) + } + } -- cgit 1.2.3-korg