added method to read config

This commit is contained in:
2021-12-30 16:23:46 +01:00
parent 0e38090ae9
commit 8dacf6cd15

View File

@@ -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]}"