Get rid of arrow-effects usage
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-commandline / src / main / kotlin / org / onap / dcae / collectors / veshv / commandline / extensions.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.commandline
21
22 import arrow.core.Option
23 import arrow.core.getOrElse
24 import arrow.syntax.function.curried
25 import org.apache.commons.cli.CommandLine
26 import org.onap.dcae.collectors.veshv.utils.process.ExitCode
27 import org.onap.dcae.collectors.veshv.utils.process.ExitFailure
28 import org.onap.dcae.collectors.veshv.utils.arrow.fromNullablesChain
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since June 2018
33  */
34
35 val handleWrongArgumentErrorCurried = ::handleWrongArgumentError.curried()
36
37 fun handleWrongArgumentError(programName: String, err: WrongArgumentError): ExitCode {
38     err.printMessage()
39     err.printHelp(programName)
40     return ExitFailure(2)
41 }
42
43 fun CommandLine.longValue(cmdLineOpt: CommandLineOption, default: Long): Long =
44         longValue(cmdLineOpt).getOrElse { default }
45
46 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption, default: String): String =
47         optionValue(cmdLineOpt).getOrElse { default }
48
49 fun CommandLine.intValue(cmdLineOpt: CommandLineOption, default: Int): Int =
50         intValue(cmdLineOpt).getOrElse { default }
51
52 fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Option<Int> =
53         optionValue(cmdLineOpt).map(String::toInt)
54
55 fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Option<Long> =
56         optionValue(cmdLineOpt).map(String::toLong)
57
58 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): Option<String> =
59         optionValue(cmdLineOpt)
60
61 fun CommandLine.hasOption(cmdLineOpt: CommandLineOption): Boolean =
62         this.hasOption(cmdLineOpt.option.opt) ||
63                 System.getenv(cmdLineOpt.environmentVariableName()) != null
64
65 private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption) = Option.fromNullablesChain(
66         getOptionValue(cmdLineOpt.option.opt),
67         { System.getenv(cmdLineOpt.environmentVariableName()) })