lib/uffizzi/config_file.rb in uffizzi-cli-0.1.4.3 vs lib/uffizzi/config_file.rb in uffizzi-cli-0.2.0
- old
+ new
@@ -21,34 +21,33 @@
File.exist?(CONFIG_PATH)
end
def read_option(option)
data = read
- return nil if data.nil?
+ return nil unless data.is_a?(Hash)
- puts "The option #{option} doesn't exist in config file" if data[option].nil?
data[option]
end
def option_exists?(option)
data = read
- return false if data.nil?
+ return false unless data.is_a?(Hash)
data.key?(option)
end
def write_option(key, value)
data = exists? ? read : {}
- return nil if data.nil?
+ return nil unless data.is_a?(Hash)
data[key] = value
write(data.to_json)
end
def delete_option(key)
data = read
- return nil if data.nil?
+ return nil unless data.is_a?(Hash)
new_data = data.except(key)
write(new_data.to_json)
end
@@ -56,25 +55,32 @@
write_option(:cookie, cookie)
end
def list
data = read
- return nil if data.nil?
+ return nil unless data.is_a?(Hash)
- data.each do |property, value|
- puts "#{property} - #{value}"
+ content = data.reduce('') do |acc, pair|
+ property, value = pair
+ "#{acc}#{property} - #{value}\n"
end
+
+ Uffizzi.ui.say(content)
+
+ data
end
private
def read
JSON.parse(File.read(CONFIG_PATH), symbolize_names: true)
rescue Errno::ENOENT => e
- puts e
+ Uffizzi.ui.say(e)
+ nil
rescue JSON::ParserError
- puts 'Config file is in incorrect format'
+ Uffizzi.ui.say('Config file is in incorrect format')
+ nil
end
def write(data)
file = create_file
file.write(data)
@@ -90,12 +96,10 @@
end
def create_file
dir = File.dirname(CONFIG_PATH)
- unless File.directory?(dir)
- FileUtils.mkdir_p(dir)
- end
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
File.new(CONFIG_PATH, 'w')
end
end
end