added method to read config
This commit is contained in:
@@ -9,7 +9,7 @@ module Kotatsu
|
|||||||
CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu")
|
CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu")
|
||||||
CONFIG_FILE = Path.new(CONFIG_DIR, "config.json")
|
CONFIG_FILE = Path.new(CONFIG_DIR, "config.json")
|
||||||
|
|
||||||
def self.buildConfig()
|
def self.buildConfigDefault()
|
||||||
conf = JSON.build do |json|
|
conf = JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
json.field "config_version", 1
|
json.field "config_version", 1
|
||||||
@@ -34,6 +34,16 @@ module Kotatsu
|
|||||||
return conf
|
return conf
|
||||||
end
|
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)
|
def self.makeConfig(file, dir)
|
||||||
puts "Creating new config file in #{file}"
|
puts "Creating new config file in #{file}"
|
||||||
if Dir.exists?(dir) == false
|
if Dir.exists?(dir) == false
|
||||||
@@ -43,7 +53,7 @@ module Kotatsu
|
|||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
File.write(file, buildConfig())
|
File.write(file, buildConfigDefault())
|
||||||
if File.exists?(file) == false
|
if File.exists?(file) == false
|
||||||
STDERR.puts "ERROR! Could not create file: #{file}"
|
STDERR.puts "ERROR! Could not create file: #{file}"
|
||||||
exit(1)
|
exit(1)
|
||||||
@@ -75,8 +85,7 @@ module Kotatsu
|
|||||||
|
|
||||||
parser.on "list", "Lists all services" do
|
parser.on "list", "Lists all services" do
|
||||||
if File.exists?(CONFIG_FILE) == true
|
if File.exists?(CONFIG_FILE) == true
|
||||||
conf = JSON.parse(File.read(CONFIG_FILE))
|
_, _, _, services = readConfig()
|
||||||
services = conf["services"].as_a
|
|
||||||
i = 0
|
i = 0
|
||||||
while services.size > i
|
while services.size > i
|
||||||
puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}"
|
puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}"
|
||||||
|
|||||||
Reference in New Issue
Block a user