1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 08:46:15 +00:00

Some more refactoring of Settings.

This commit is contained in:
Fabian Schlenz 2018-04-11 05:50:26 +02:00
parent f24e66271f
commit 38fce0ee5c
4 changed files with 30 additions and 24 deletions

View File

@ -46,12 +46,12 @@ class CommandLineController(val options: CommandLineOptions) {
logger.info("CommandLineController started. App version {}", Config.APP_APPVER) logger.info("CommandLineController started. App version {}", Config.APP_APPVER)
printHeader() printHeader()
if (options.booleans.contains("version")) { if (options.isSet("version")) {
System.exit(0) System.exit(0)
} else if (options.booleans.contains("help")) { } else if (options.isSet("help")) {
show_help() show_help()
System.exit(0) System.exit(0)
} else if (options.booleans.contains("license")) { } else if (options.isSet("license")) {
show_license() show_license()
System.exit(0) System.exit(0)
} }
@ -62,26 +62,26 @@ class CommandLineController(val options: CommandLineOptions) {
// Setup file_base // Setup file_base
logger.debug("Target dir from Config: {}", Config.TARGET_DIR.anonymize()) 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) logger.debug("Target dir after options: {}", target_dir)
println("Base directory for files: ${target_dir.anonymize()}") println("Base directory for files: ${target_dir.anonymize()}")
if (options.booleans.contains("list_accounts")) { if (options.isSet("list_accounts")) {
Utils.print_accounts(target_dir) Utils.print_accounts(target_dir)
System.exit(0) System.exit(0)
} }
if (options.booleans.contains("login")) { if (options.isSet("login")) {
cmd_login(app, target_dir, options.values.get("account")) cmd_login(app, target_dir, options.get("account"))
} }
logger.trace("Checking accounts") 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) { } catch(e: AccountNotFoundException) {
show_error("The specified account could not be found.") show_error("The specified account could not be found.")
} catch(e: NoAccountsException) { } catch(e: NoAccountsException) {
println("No accounts found. Starting login process...") 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 // 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. // Load the settings and stuff.
settings = Settings(file_base, database, options) settings = Settings(file_base, database, options)
if (options.booleans.contains("stats")) { if (options.isSet("stats")) {
cmd_stats(file_base, database) cmd_stats(file_base, database)
System.exit(0) System.exit(0)
} else if (options.booleans.contains("settings")) { } else if (options.isSet("settings")) {
settings.print() settings.print()
System.exit(0) System.exit(0)
} }
val export = options.values["export"] val export = options.get("export")
logger.debug("options.val_export: {}", export) logger.debug("options.val_export: {}", export)
if (export != null) { if (export != null) {
if (export.toLowerCase() == "html") { if (export.toLowerCase() == "html") {
@ -142,7 +142,7 @@ class CommandLineController(val options: CommandLineOptions) {
logger.info("Initializing Download Manager") logger.info("Initializing Download Manager")
val d = DownloadManager(client, CommandLineDownloadProgress(), database, user_manager, settings, file_base) 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 chats = d.getChats()
val print_header = {download: Boolean -> println("%-15s %-40s %s".format("ID", "Title", if (download) "Download" else "")); println("-".repeat(65)) } 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 "")} 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) System.exit(0)
} }
logger.debug("Calling DownloadManager.downloadMessages with limit {}", options.values.get("limit_messages")?.last()) logger.debug("Calling DownloadManager.downloadMessages with limit {}", options.get("limit_messages"))
d.downloadMessages(options.values.get("limit_messages")?.last()?.toInt()) d.downloadMessages(options.get("limit_messages")?.toInt())
logger.debug("IniSettings#download_media: {}", settings.download_media) logger.debug("IniSettings#download_media: {}", settings.download_media)
if (settings.download_media) { if (settings.download_media) {
logger.debug("Calling DownloadManager.downloadMedia") 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.") println("Skipping media download because download_media is set to false.")
} }
if (options.booleans.contains("daemon")) { if (options.isSet("daemon")) {
logger.info("Initializing TelegramUpdateHandler") logger.info("Initializing TelegramUpdateHandler")
handler = TelegramUpdateHandler(user_manager, database, file_base, settings) handler = TelegramUpdateHandler(user_manager, database, file_base, settings)
client.close() client.close()

View File

@ -16,8 +16,7 @@
package de.fabianonline.telegram_backup package de.fabianonline.telegram_backup
class CommandLineOptions(args: Array<String>) { class CommandLineOptions(args: Array<String>) {
val booleans = mutableListOf<String>() private val values = mutableMapOf<String, String>()
val values = mutableMapOf<String, String>()
var last_key: String? = null var last_key: String? = null
val substitutions = mapOf("-t" to "--target") val substitutions = mapOf("-t" to "--target")
@ -49,12 +48,19 @@ class CommandLineOptions(args: Array<String>) {
if (next_arg == null) { if (next_arg == null) {
// current_arg seems to be a boolean value // current_arg seems to be a boolean value
booleans.add(current_arg)
values.put(current_arg, "true") values.put(current_arg, "true")
if (current_arg.startsWith("no-")) {
current_arg = current_arg.substring(3)
values.put(current_arg, "false")
}
} else { } else {
// current_arg has the value next_arg // current_arg has the value next_arg
values.put(current_arg, 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"
} }

View File

@ -53,7 +53,7 @@ class CommandLineRunner(args: Array<String>) {
} }
fun setupLogging() { fun setupLogging() {
if (options.booleans.contains("anonymize")) { if (options.isSet("anonymize")) {
Utils.anonymize = true Utils.anonymize = true
} }
@ -76,13 +76,13 @@ class CommandLineRunner(args: Array<String>) {
rootLogger.addAppender(appender) rootLogger.addAppender(appender)
rootLogger.setLevel(Level.OFF) rootLogger.setLevel(Level.OFF)
if (options.booleans.contains("trace")) { if (options.isSet("trace")) {
(LoggerFactory.getLogger("de.fabianonline.telegram_backup") as Logger).setLevel(Level.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) (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) (LoggerFactory.getLogger("com.github.badoualy") as Logger).setLevel(Level.TRACE)
} }
} }

View File

@ -115,7 +115,7 @@ class Setting(val ini: Map<String, List<String>>, val cli: CommandLineOptions, v
} }
fun getCli(name: String): String? { fun getCli(name: String): String? {
return cli.values[name] return cli.get(name)
} }
fun print() { fun print() {