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

View file

@ -83,7 +83,7 @@ module MPW
@error_msg = I18n.t(msg['error']) @error_msg = I18n.t(msg['error'])
return nil return nil
elsif msg['data'].nil? or msg['data'].empty? elsif msg['data'].nil? or msg['data'].empty?
return [] return {}
else else
tmp_file = tmpfile tmp_file = tmpfile
File.open(tmp_file, 'w') do |file| File.open(tmp_file, 'w') do |file|

View file

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