From 8dacf6cd15c3a719f0bed3819cad9fb2a0db80d4 Mon Sep 17 00:00:00 2001 From: luneko Date: Thu, 30 Dec 2021 16:23:46 +0100 Subject: [PATCH] added method to read config --- src/kotatsu.cr | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/kotatsu.cr b/src/kotatsu.cr index 1d155c8..e76559c 100644 --- a/src/kotatsu.cr +++ b/src/kotatsu.cr @@ -9,7 +9,7 @@ module Kotatsu CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu") CONFIG_FILE = Path.new(CONFIG_DIR, "config.json") - def self.buildConfig() + def self.buildConfigDefault() conf = JSON.build do |json| json.object do json.field "config_version", 1 @@ -34,6 +34,16 @@ module Kotatsu return conf end + def self.readConfig() + conf = JSON.parse(File.read(CONFIG_FILE)) + version = conf["config_version"].as_i + port = conf["port"].as_i + title = conf["title"].as_s + services = conf["services"].as_a + + return version, port, title, services + end + def self.makeConfig(file, dir) puts "Creating new config file in #{file}" if Dir.exists?(dir) == false @@ -43,7 +53,7 @@ module Kotatsu exit(1) end end - File.write(file, buildConfig()) + File.write(file, buildConfigDefault()) if File.exists?(file) == false STDERR.puts "ERROR! Could not create file: #{file}" exit(1) @@ -75,8 +85,7 @@ module Kotatsu parser.on "list", "Lists all services" do if File.exists?(CONFIG_FILE) == true - conf = JSON.parse(File.read(CONFIG_FILE)) - services = conf["services"].as_a + _, _, _, services = readConfig() i = 0 while services.size > i puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}"