1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +00:00

fix sync with new data format

This commit is contained in:
nishiki 2014-12-06 21:37:18 +01:00
parent 291d68a1f7
commit 568a8514cf
3 changed files with 18 additions and 19 deletions

View file

@ -26,12 +26,12 @@ module MPW
# @args: password -> the GPG key password
# @rtrn: true if data has been decrypted
def decrypt(passwd=nil)
@data = []
@data = {}
if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(armor: true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: passwd).read.force_encoding('utf-8')
@data = YAML.load(data_decrypt)
@data = YAML.load(data_decrypt) if not data_decrypt.to_s.empty?
end
return true
@ -119,7 +119,7 @@ module MPW
update = false
i = 0
if @data.has_key?(id)
if @data.instance_of?(Hash) and @data.has_key?(id)
row = @data[id]
end
@ -229,7 +229,7 @@ module MPW
# Return a preview import
# @args: file -> path to file import
# @rtrn: an array with the items to import, if there is an error return false
# @rtrn: a hash with the items to import, if there is an error return false
def import_preview(file, type=:csv)
result = []
case type
@ -257,23 +257,22 @@ module MPW
# last_update -> last update
# @rtrn: false if data_remote is nil
def sync(data_remote, last_update)
if not data_remote.instance_of?(Hash)
@error_msg = I18n.t('error.sync.hash')
if not data_remote.instance_of?(Array)
@error_msg = I18n.t('error.sync.array')
return false
else not data_remote.to_s.empty?
@data.each do |lk, l|
j = 0
update = false
# Update item
data_remote.each do |rk, r|
data_remote.each do |r|
if l['id'] == r['id']
if l['date'].to_i < r['date'].to_i
update(r['name'], r['group'], r['host'], r['protocol'], r['login'], r['password'], r['port'], r['comment'], l['id'])
end
update = true
data_remote.delete(rk)
data_remote.delete(r['id'])
break
end
j += 1
@ -287,7 +286,7 @@ module MPW
end
# Add item
data_remote.each do |rk, r|
data_remote.each do |r|
if r['date'].to_i > last_update
update(r['name'], r['group'], r['host'], r['protocol'], r['login'], r['password'], r['port'], r['comment'], r['id'])
end

View file

@ -63,7 +63,7 @@ module MPW
# @rtrn: nil if nothing data or error
def get(gpg_password)
return nil if not @enable
msg = nil
TCPSocket.open(@host, @port) do |socket|
send_msg = {action: 'get',
@ -75,7 +75,7 @@ module MPW
socket.puts send_msg.to_json
msg = JSON.parse(socket.gets)
end
if not defined?(msg['error'])
@error_msg = I18n.t('error.sync.communication')
return nil
@ -83,7 +83,7 @@ module MPW
@error_msg = I18n.t(msg['error'])
return nil
elsif msg['data'].nil? or msg['data'].empty?
return []
return {}
else
tmp_file = tmpfile
File.open(tmp_file, 'w') do |file|

View file

@ -48,8 +48,8 @@ class Cli
if @sync.enable
if not @mpw.sync(@sync.get(@passwd), @config.last_update)
puts "#{I18n.t('display.error')} #2: #{@mpw.error_msg}" if !@mpw.error_msg.nil?
puts "#{I18n.t('display.error')} #3: #{@sync.error_msg}" if !@sync.error_msg.nil?
puts "#{I18n.t('display.error')} #2: #{@mpw.error_msg}" if not @mpw.error_msg.nil?
puts "#{I18n.t('display.error')} #3: #{@sync.error_msg}" if not @sync.error_msg.nil?
elsif not @sync.update(File.open(@config.file_gpg).read)
puts "#{I18n.t('display.error')} #4: #{@sync.error_msg}"
elsif not @config.set_last_update
@ -62,9 +62,9 @@ class Cli
end
rescue Exception => e
puts "#{I18n.t('display.error')} #7: #{e}"
puts @sync.error_msg if @sync.error_msg.nil?
puts @config.error_msg if @config.error_msg.nil?
puts @mpw.error_msg if @mpw.error_msg.nil?
puts @sync.error_msg if not @sync.error_msg.nil?
puts @config.error_msg if not @config.error_msg.nil?
puts @mpw.error_msg if not @mpw.error_msg.nil?
else
return false
end
@ -323,7 +323,7 @@ class Cli
if not force
result = @mpw.import_preview(file)
if result.is_a?(Array) && !result.empty?
if result.is_a?(Array) and not result.empty?
result.each do |r|
displayFormat(r)
end