2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.collectors.veshv.config.impl
22 import arrow.core.Either
23 import arrow.core.Option
25 import arrow.core.flatMap
26 import org.apache.commons.cli.CommandLine
27 import org.apache.commons.cli.CommandLineParser
28 import org.apache.commons.cli.DefaultParser
29 import org.apache.commons.cli.Options
30 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE
31 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.HEALTH_CHECK_API_PORT
32 import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError
33 import org.onap.dcae.collectors.veshv.commandline.intValue
34 import org.onap.dcae.collectors.veshv.commandline.stringValue
37 class ArgHvVesConfiguration(private val parser: CommandLineParser = DefaultParser()) {
38 val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT)
40 fun getConfiguration(cmdLine: CommandLine): Option<File> =
41 cmdLine.stringValue(CONFIGURATION_FILE).map(::File)
43 fun getHealthcheckPort(cmdLine: CommandLine): Option<Int> =
44 cmdLine.intValue(HEALTH_CHECK_API_PORT)
46 lateinit var parsingResult: CommandLine
48 fun parseToFile(args: Array<out String>): Either<WrongArgumentError, File> {
50 return Try { parsingResult }.toEither()
51 .mapLeft { WrongArgumentError(it, cmdLineOptionsList) }
52 .map(this::getConfiguration)
56 message = "Unexpected error when parsing command line arguments",
57 cmdLineOptionsList = cmdLineOptionsList)
62 fun parseToInt(args: Array<out String>): Either<WrongArgumentError, Int> {
64 return Try { parsingResult }.toEither()
65 .mapLeft { WrongArgumentError(it, cmdLineOptionsList) }
66 .map(this::getHealthcheckPort)
70 message = "Unexpected error when parsing command line arguments",
71 cmdLineOptionsList = cmdLineOptionsList)
76 private fun parseIfEmpty(args: Array<out String>) {
77 if (!this::parsingResult.isInitialized) {
78 parsingResult = parseArgumentsArray(args)
82 private fun parseArgumentsArray(args: Array<out String>) =
85 .fold(Options(), Options::addOption)
86 .let { parser.parse(it, args) }