From 38fce0ee5c1af7cf97a0c2c59aa44550f504e1b0 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 11 Apr 2018 05:50:26 +0200 Subject: [PATCH] Some more refactoring of Settings. --- .../telegram_backup/CommandLineController.kt | 32 +++++++++---------- .../telegram_backup/CommandLineOptions.kt | 12 +++++-- .../telegram_backup/CommandLineRunner.kt | 8 ++--- .../fabianonline/telegram_backup/Settings.kt | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt index c17e9cb..d2291c3 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt @@ -46,12 +46,12 @@ class CommandLineController(val options: CommandLineOptions) { logger.info("CommandLineController started. App version {}", Config.APP_APPVER) printHeader() - if (options.booleans.contains("version")) { + if (options.isSet("version")) { System.exit(0) - } else if (options.booleans.contains("help")) { + } else if (options.isSet("help")) { show_help() System.exit(0) - } else if (options.booleans.contains("license")) { + } else if (options.isSet("license")) { show_license() System.exit(0) } @@ -62,26 +62,26 @@ class CommandLineController(val options: CommandLineOptions) { // Setup file_base logger.debug("Target dir from Config: {}", Config.TARGET_DIR.anonymize()) - target_dir = options.values.get("target") ?: Config.TARGET_DIR + target_dir = options.get("target") ?: Config.TARGET_DIR logger.debug("Target dir after options: {}", target_dir) println("Base directory for files: ${target_dir.anonymize()}") - if (options.booleans.contains("list_accounts")) { + if (options.isSet("list_accounts")) { Utils.print_accounts(target_dir) System.exit(0) } - if (options.booleans.contains("login")) { - cmd_login(app, target_dir, options.values.get("account")) + if (options.isSet("login")) { + cmd_login(app, target_dir, options.get("account")) } logger.trace("Checking accounts") - phone_number = try { selectAccount(target_dir, options.values.get("account")) + phone_number = try { selectAccount(target_dir, options.get("account")) } catch(e: AccountNotFoundException) { show_error("The specified account could not be found.") } catch(e: NoAccountsException) { println("No accounts found. Starting login process...") - cmd_login(app, target_dir, options.values.get("account")) + cmd_login(app, target_dir, options.get("account")) } // TODO: Create a new TelegramApp if the user set his/her own TelegramApp credentials @@ -118,15 +118,15 @@ class CommandLineController(val options: CommandLineOptions) { // Load the settings and stuff. settings = Settings(file_base, database, options) - if (options.booleans.contains("stats")) { + if (options.isSet("stats")) { cmd_stats(file_base, database) System.exit(0) - } else if (options.booleans.contains("settings")) { + } else if (options.isSet("settings")) { settings.print() System.exit(0) } - val export = options.values["export"] + val export = options.get("export") logger.debug("options.val_export: {}", export) if (export != null) { if (export.toLowerCase() == "html") { @@ -142,7 +142,7 @@ class CommandLineController(val options: CommandLineOptions) { logger.info("Initializing Download Manager") val d = DownloadManager(client, CommandLineDownloadProgress(), database, user_manager, settings, file_base) - if (options.booleans.contains("list_channels")) { + if (options.isSet("list_channels")) { val chats = d.getChats() val print_header = {download: Boolean -> println("%-15s %-40s %s".format("ID", "Title", if (download) "Download" else "")); println("-".repeat(65)) } val format = {c: DownloadManager.Channel, download: Boolean -> "%-15s %-40s %s".format(c.id.toString().anonymize(), c.title.anonymize(), if (download) (if(c.download) "YES" else "no") else "")} @@ -166,8 +166,8 @@ class CommandLineController(val options: CommandLineOptions) { System.exit(0) } - logger.debug("Calling DownloadManager.downloadMessages with limit {}", options.values.get("limit_messages")?.last()) - d.downloadMessages(options.values.get("limit_messages")?.last()?.toInt()) + logger.debug("Calling DownloadManager.downloadMessages with limit {}", options.get("limit_messages")) + d.downloadMessages(options.get("limit_messages")?.toInt()) logger.debug("IniSettings#download_media: {}", settings.download_media) if (settings.download_media) { logger.debug("Calling DownloadManager.downloadMedia") @@ -176,7 +176,7 @@ class CommandLineController(val options: CommandLineOptions) { println("Skipping media download because download_media is set to false.") } - if (options.booleans.contains("daemon")) { + if (options.isSet("daemon")) { logger.info("Initializing TelegramUpdateHandler") handler = TelegramUpdateHandler(user_manager, database, file_base, settings) client.close() diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt index d26d99f..bd16d5d 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt @@ -16,8 +16,7 @@ package de.fabianonline.telegram_backup class CommandLineOptions(args: Array) { - val booleans = mutableListOf() - val values = mutableMapOf() + private val values = mutableMapOf() var last_key: String? = null val substitutions = mapOf("-t" to "--target") @@ -49,12 +48,19 @@ class CommandLineOptions(args: Array) { if (next_arg == null) { // current_arg seems to be a boolean value - booleans.add(current_arg) values.put(current_arg, "true") + if (current_arg.startsWith("no-")) { + current_arg = current_arg.substring(3) + values.put(current_arg, "false") + } } else { // current_arg has the value next_arg values.put(current_arg, next_arg) } } + println(values) } + + operator fun get(name: String): String? = values[name] + fun isSet(name: String): Boolean = values[name]=="true" } diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineRunner.kt b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineRunner.kt index e517da7..9c58b0f 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineRunner.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineRunner.kt @@ -53,7 +53,7 @@ class CommandLineRunner(args: Array) { } fun setupLogging() { - if (options.booleans.contains("anonymize")) { + if (options.isSet("anonymize")) { Utils.anonymize = true } @@ -76,13 +76,13 @@ class CommandLineRunner(args: Array) { rootLogger.addAppender(appender) rootLogger.setLevel(Level.OFF) - if (options.booleans.contains("trace")) { + if (options.isSet("trace")) { (LoggerFactory.getLogger("de.fabianonline.telegram_backup") as Logger).setLevel(Level.TRACE) - } else if (options.booleans.contains("debug")) { + } else if (options.isSet("debug")) { (LoggerFactory.getLogger("de.fabianonline.telegram_backup") as Logger).setLevel(Level.DEBUG) } - if (options.booleans.contains("trace_telegram")) { + if (options.isSet("trace_telegram")) { (LoggerFactory.getLogger("com.github.badoualy") as Logger).setLevel(Level.TRACE) } } diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt index cfae075..c479e21 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Settings.kt @@ -115,7 +115,7 @@ class Setting(val ini: Map>, val cli: CommandLineOptions, v } fun getCli(name: String): String? { - return cli.values[name] + return cli.get(name) } fun print() {