From f7cde961aeea8640440e89b823052a496779edb7 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 22 Feb 2022 01:47:14 +0100 Subject: [PATCH] Added command line options to native app to set retrieveBalance, retrieveTransactions, retrieveTransactionsFrom, retrieveTransactionsTo, preferredTanMethod and abortIfRequiresTan --- fints4k/build.gradle | 2 + .../kotlin/{Application.kt => NativeApp.kt} | 19 +++------ .../kotlin/fints4kCommandLineInterface.kt | 42 +++++++++++++++++++ fints4k/src/nativeMain/kotlin/main.kt | 4 ++ 4 files changed, 54 insertions(+), 13 deletions(-) rename fints4k/src/nativeMain/kotlin/{Application.kt => NativeApp.kt} (85%) create mode 100644 fints4k/src/nativeMain/kotlin/fints4kCommandLineInterface.kt create mode 100644 fints4k/src/nativeMain/kotlin/main.kt diff --git a/fints4k/build.gradle b/fints4k/build.gradle index 7e153139..8a63676b 100644 --- a/fints4k/build.gradle +++ b/fints4k/build.gradle @@ -142,6 +142,8 @@ kotlin { // requires cURL to be installed on your system implementation "io.ktor:ktor-client-curl:$ktorVersion" + + implementation "com.github.ajalt.clikt:clikt:3.4.0" } } diff --git a/fints4k/src/nativeMain/kotlin/Application.kt b/fints4k/src/nativeMain/kotlin/NativeApp.kt similarity index 85% rename from fints4k/src/nativeMain/kotlin/Application.kt rename to fints4k/src/nativeMain/kotlin/NativeApp.kt index 7c811a47..1e66319b 100644 --- a/fints4k/src/nativeMain/kotlin/Application.kt +++ b/fints4k/src/nativeMain/kotlin/NativeApp.kt @@ -1,4 +1,3 @@ -import kotlinx.coroutines.runBlocking import kotlinx.datetime.LocalDate import net.dankito.banking.client.model.AccountTransaction import net.dankito.banking.client.model.CustomerAccount @@ -8,24 +7,18 @@ import net.dankito.banking.fints.callback.SimpleFinTsClientCallback import net.dankito.banking.fints.getAccountData import net.dankito.banking.fints.model.TanChallenge import net.dankito.utils.multiplatform.extensions.* -import platform.posix.exit -fun main(args: Array) { - if (args.size < 3) { - println("Bitte geben Sie Ihre Bankzugangsdaten ein in der Reihenfolge: \r\n" + - "Z. B.: ./fints4k.kexe 10050000 \"Mein Loginname\" GeheimesPasswort") - exit(0) - } - Application().retrieveAccountData(args[0], args[1], args[2]) -} - -class Application { +class NativeApp { fun retrieveAccountData(bankCode: String, loginName: String, password: String) { + retrieveAccountData(GetAccountDataParameter(bankCode, loginName, password)) + } + + fun retrieveAccountData(param: GetAccountDataParameter) { val client = FinTsClient(SimpleFinTsClientCallback { tanChallenge -> enterTan(tanChallenge) }) - val response = client.getAccountData(GetAccountDataParameter(bankCode, loginName, password)) + val response = client.getAccountData(param) if (response.error != null) { println("An error occurred: ${response.error}${response.errorMessage?.let { " $it" }}") diff --git a/fints4k/src/nativeMain/kotlin/fints4kCommandLineInterface.kt b/fints4k/src/nativeMain/kotlin/fints4kCommandLineInterface.kt new file mode 100644 index 00000000..8ddfddd0 --- /dev/null +++ b/fints4k/src/nativeMain/kotlin/fints4kCommandLineInterface.kt @@ -0,0 +1,42 @@ +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.parameters.arguments.argument +import com.github.ajalt.clikt.parameters.options.* +import com.github.ajalt.clikt.parameters.types.enum +import kotlinx.datetime.LocalDate +import net.dankito.banking.client.model.parameter.GetAccountDataParameter +import net.dankito.banking.client.model.parameter.RetrieveTransactions +import net.dankito.banking.fints.model.TanMethodType + +class fints4kCommandLineInterface : CliktCommand(name = "fints", printHelpOnEmptyArgs = true) { + init { + versionOption("1.0.0 Alpha-10", names = setOf("-v", "--version")) + } + + val bankCode by argument("Bankleitzahl", "Die Bankleitzahl deiner Bank") + val loginName by argument("Loginname", "Dein Onlinebanking Loginname / Anmeldename") + val password by argument("Passwort", "Dein Onlinebanking Passwort") +// val password by option("-p", "--password", help = "Dein Onlinebanking Passwort").prompt("Passwort", hideInput = true) + + val retrieveBalance by option("-b", "--balance", help = "Den Kontostand abrufen. Defaults to true").flag(default = true) + val retrieveTransactions by option("-t", "--transactions", help = "Die Kontoumsätze abrufen. Default: OfLast90Days. As most banks don't afford a TAN to get transactions of last 90 days") + .enum().default(RetrieveTransactions.OfLast90Days) + + val retrieveTransactionsFrom by option("--from", help = "The day as ISO date from which transactions should be retrieved like 2022-02-22. If set sets 'retrieveTransactions' to '${RetrieveTransactions.AccordingToRetrieveFromAndTo}'") + .validate { it.isNullOrBlank() || LocalDate.parse(it) != null } + val retrieveTransactionsTo by option("--to", help = "The day as ISO date up to which account transactions are to be received like 2022-02-22. If set sets 'retrieveTransactions' to '${RetrieveTransactions.AccordingToRetrieveFromAndTo}'") + .validate { it.isNullOrBlank() || LocalDate.parse(it) != null } + + val preferredTanMethod by option("-m", "--tan-method", help = "Your preferred TAN methods to use if action affords a TAN. Can be repeated like '-m AppTan -m SmsTan'").enum().multiple() + + val abortIfRequiresTan by option("-a", "--abort-if-requires-tan", help = "If actions should be aborted if it affords a TAN. Defaults to false").flag(default = false) + + override fun run() { + val retrieveTransactionsFromDate = if (retrieveTransactionsFrom.isNullOrBlank()) null else LocalDate.parse(retrieveTransactionsFrom!!) + val retrieveTransactionsToDate = if (retrieveTransactionsTo.isNullOrBlank()) null else LocalDate.parse(retrieveTransactionsTo!!) + val effectiveRetrieveTransactions = if (retrieveTransactionsFromDate != null || retrieveTransactionsToDate != null) RetrieveTransactions.AccordingToRetrieveFromAndTo + else retrieveTransactions + + NativeApp().retrieveAccountData(GetAccountDataParameter(bankCode, loginName, password, null, retrieveBalance, effectiveRetrieveTransactions, + retrieveTransactionsFromDate, retrieveTransactionsToDate, preferredTanMethod, abortIfTanIsRequired = abortIfRequiresTan)) + } +} \ No newline at end of file diff --git a/fints4k/src/nativeMain/kotlin/main.kt b/fints4k/src/nativeMain/kotlin/main.kt new file mode 100644 index 00000000..0bd29b05 --- /dev/null +++ b/fints4k/src/nativeMain/kotlin/main.kt @@ -0,0 +1,4 @@ + +fun main(args: Array) { + fints4kCommandLineInterface().main(args) +} \ No newline at end of file