From 66590416310fb6401b224bc145ab67cca3b15409 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Fri, 28 Feb 2020 16:24:46 +0000 Subject: [PATCH] Allow missing keys in the "mangadex" dictionary in the config file --- src/config.cr | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/config.cr b/src/config.cr index 7915695..9555a2b 100644 --- a/src/config.cr +++ b/src/config.cr @@ -21,10 +21,13 @@ class Config property log_level : String = "info" @[YAML::Field(key: "mangadex")] - property mangadex = { + property mangadex = Hash(String, String|Int32).new + + @mangadex_defaults = { "base_url" => "https://mangadex.org", "api_url" => "https://mangadex.org/api", "download_wait_seconds" => 5, + "download_retries" => 4, "download_queue_db_path" => File.expand_path "~/mango/queue.db", home: true } @@ -33,7 +36,9 @@ class Config path = "~/.config/mango/config.yml" if path.nil? cfg_path = File.expand_path path, home: true if File.exists? cfg_path - return self.from_yaml File.read cfg_path + config = self.from_yaml File.read cfg_path + config.fill_defaults + return config end puts "The config file #{cfg_path} does not exist." \ " Do you want mango to dump the default config there? [Y/n]" @@ -50,4 +55,14 @@ class Config puts "The config file has been created at #{cfg_path}." default end + + def fill_defaults + {% for hash_name in ["mangadex"] %} + @{{hash_name.id}}_defaults.map do |k, v| + if @{{hash_name.id}}[k]?.nil? + @{{hash_name.id}}[k] = v + end + end + {% end %} + end end