lib/uffizzi/config_file.rb in uffizzi-cli-0.5.2 vs lib/uffizzi/config_file.rb in uffizzi-cli-0.6.0
- old
+ new
@@ -3,15 +3,15 @@
require 'json'
require 'fileutils'
module Uffizzi
class ConfigFile
- CONFIG_PATH = "#{Dir.home}/.uffizzi/config.json"
+ CONFIG_PATH = "#{Dir.home}/.config/uffizzi/config_default.json"
class << self
- def create(account_id, cookie, hostname)
- data = prepare_config_data(account_id, cookie, hostname)
+ def create(account_id, cookie, server)
+ data = prepare_config_data(account_id, cookie, server)
data.each_pair { |key, value| write_option(key, value) }
end
def delete
File.delete(CONFIG_PATH) if exists?
@@ -26,31 +26,31 @@
return nil unless data.is_a?(Hash)
data[option]
end
- def option_exists?(option)
+ def option_has_value?(option)
data = read
- return false unless data.is_a?(Hash)
+ return false if !data.is_a?(Hash) || !option_exists?(option)
- data.key?(option)
+ !data[option].empty?
end
def write_option(key, value)
data = exists? ? read : {}
return nil unless data.is_a?(Hash)
data[key] = value
- write(data.to_json)
+ write(data)
end
- def delete_option(key)
+ def unset_option(key)
data = read
- return nil unless data.is_a?(Hash)
+ return nil unless data.is_a?(Hash) || !option_exists?(key)
- new_data = data.except(key)
- write(new_data.to_json)
+ data[key] = ''
+ write(data)
end
def rewrite_cookie(cookie)
write_option(:cookie, cookie)
end
@@ -59,39 +59,55 @@
data = read
return nil unless data.is_a?(Hash)
content = data.reduce('') do |acc, pair|
property, value = pair
- "#{acc}#{property} - #{value}\n"
+ "#{acc}#{property} = #{value}\n"
end
Uffizzi.ui.say(content)
data
end
private
+ def option_exists?(option)
+ data = read
+ return false unless data.is_a?(Hash)
+
+ data.key?(option)
+ end
+
def read
- JSON.parse(File.read(CONFIG_PATH), symbolize_names: true)
+ data = File.read(CONFIG_PATH)
+ options = data.split("\n")
+ options.reduce({}) do |acc, option|
+ key, value = option.split('=', 2)
+ acc.merge({ key.strip.to_sym => value.strip })
+ end
rescue Errno::ENOENT => e
Uffizzi.ui.say(e)
- nil
- rescue JSON::ParserError
- Uffizzi.ui.say('Config file is in incorrect format')
- nil
end
def write(data)
file = create_file
- file.write(data)
+ prepared_data = prepare_data(data)
+ file.write(prepared_data)
file.close
end
- def prepare_config_data(account_id, cookie, hostname)
+ def prepare_data(data)
+ data.reduce('') do |acc, option|
+ key, value = option
+ "#{acc}#{key} = #{value}\n"
+ end
+ end
+
+ def prepare_config_data(account_id, cookie, server)
{
account_id: account_id,
- hostname: hostname,
+ server: server,
cookie: cookie,
}
end
def create_file