lib/uffizzi/config_file.rb in uffizzi-cli-0.11.0 vs lib/uffizzi/config_file.rb in uffizzi-cli-0.11.1

- old
+ new

@@ -90,28 +90,33 @@ 'To configure the uffizzi CLI interactively, run $ uffizzi config' raise Uffizzi::Error.new(message) end def write(data) - file = create_file prepared_data = prepare_data(data) - file.write(prepared_data) - file.close + + lock(config_path) { atomic_write(config_path, "#{config_path}.tmp", prepared_data) } end def prepare_data(data) data.reduce('') do |acc, option| key, value = option "#{acc}#{key} = #{value}\n" end end - def create_file - dir = File.dirname(config_path) + def atomic_write(path, temp_path, content) + File.open(temp_path, 'w') { |f| f.write(content) } + FileUtils.mv(temp_path, path) + end + def lock(path) + dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.directory?(dir) - File.new(config_path, 'w') + File.open(path).flock(File::LOCK_EX) if File.exist?(path) + yield + File.open(path).flock(File::LOCK_UN) end end end end