mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-27 07:33:05 +00:00
fix bug sync when a device add an iten whitout sync
This commit is contained in:
parent
7a222798c5
commit
2d7dacb02c
3 changed files with 41 additions and 33 deletions
|
@ -23,7 +23,7 @@ module MPW
|
||||||
attr_accessor :sync_user
|
attr_accessor :sync_user
|
||||||
attr_accessor :sync_pwd
|
attr_accessor :sync_pwd
|
||||||
attr_accessor :sync_path
|
attr_accessor :sync_path
|
||||||
attr_accessor :last_update
|
attr_accessor :last_sync
|
||||||
attr_accessor :dir_config
|
attr_accessor :dir_config
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
|
@ -82,7 +82,7 @@ module MPW
|
||||||
'sync_user' => sync_user,
|
'sync_user' => sync_user,
|
||||||
'sync_pwd' => sync_pwd,
|
'sync_pwd' => sync_pwd,
|
||||||
'sync_path' => sync_path,
|
'sync_path' => sync_path,
|
||||||
'last_update' => 0
|
'last_sync' => 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,17 +139,17 @@ module MPW
|
||||||
# @rtrn: true if the config file is correct
|
# @rtrn: true if the config file is correct
|
||||||
def checkconfig
|
def checkconfig
|
||||||
config = YAML::load_file(@file_config)
|
config = YAML::load_file(@file_config)
|
||||||
@key = config['config']['key']
|
@key = config['config']['key']
|
||||||
@share_keys = config['config']['share_keys']
|
@share_keys = config['config']['share_keys']
|
||||||
@lang = config['config']['lang']
|
@lang = config['config']['lang']
|
||||||
@file_gpg = config['config']['file_gpg']
|
@file_gpg = config['config']['file_gpg']
|
||||||
@sync_type = config['config']['sync_type']
|
@sync_type = config['config']['sync_type']
|
||||||
@sync_host = config['config']['sync_host']
|
@sync_host = config['config']['sync_host']
|
||||||
@sync_port = config['config']['sync_port']
|
@sync_port = config['config']['sync_port']
|
||||||
@sync_user = config['config']['sync_user']
|
@sync_user = config['config']['sync_user']
|
||||||
@sync_pwd = config['config']['sync_pwd']
|
@sync_pwd = config['config']['sync_pwd']
|
||||||
@sync_path = config['config']['sync_path']
|
@sync_path = config['config']['sync_path']
|
||||||
@last_update = config['config']['last_update'].to_i
|
@last_sync = config['config']['last_sync'].to_i
|
||||||
|
|
||||||
if @key.empty? or @file_gpg.empty?
|
if @key.empty? or @file_gpg.empty?
|
||||||
@error_msg = I18n.t('error.config.check')
|
@error_msg = I18n.t('error.config.check')
|
||||||
|
@ -203,7 +203,7 @@ module MPW
|
||||||
|
|
||||||
# Set the last update when there is a sync
|
# Set the last update when there is a sync
|
||||||
# @rtrn: true is the file has been updated
|
# @rtrn: true is the file has been updated
|
||||||
def set_last_update
|
def set_last_sync
|
||||||
config = {'config' => {'key' => @key,
|
config = {'config' => {'key' => @key,
|
||||||
'share_keys' => @share_keys,
|
'share_keys' => @share_keys,
|
||||||
'lang' => @lang,
|
'lang' => @lang,
|
||||||
|
@ -214,7 +214,7 @@ module MPW
|
||||||
'sync_user' => @sync_user,
|
'sync_user' => @sync_user,
|
||||||
'sync_pwd' => @sync_pwd,
|
'sync_pwd' => @sync_pwd,
|
||||||
'sync_path' => @sync_path,
|
'sync_path' => @sync_path,
|
||||||
'last_update' => Time.now.to_i
|
'last_sync' => Time.now.to_i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ module MPW
|
||||||
attr_accessor :port
|
attr_accessor :port
|
||||||
attr_accessor :comment
|
attr_accessor :comment
|
||||||
attr_accessor :last_edit
|
attr_accessor :last_edit
|
||||||
|
attr_accessor :last_sync
|
||||||
attr_accessor :created
|
attr_accessor :created
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
|
@ -67,6 +68,11 @@ module MPW
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Update last_sync
|
||||||
|
def set_last_sync
|
||||||
|
@last_sync = Time.now.to_i
|
||||||
|
end
|
||||||
|
|
||||||
# Delete all data
|
# Delete all data
|
||||||
# @rtrn: true
|
# @rtrn: true
|
||||||
def delete
|
def delete
|
||||||
|
@ -81,6 +87,7 @@ module MPW
|
||||||
@comment = nil
|
@comment = nil
|
||||||
@created = nil
|
@created = nil
|
||||||
@last_edit = nil
|
@last_edit = nil
|
||||||
|
@last_sync = nil
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
37
lib/Sync.rb
37
lib/Sync.rb
|
@ -68,34 +68,32 @@ module MPW
|
||||||
# Sync remote data and local data
|
# Sync remote data and local data
|
||||||
# raise an exception if there is a problem
|
# raise an exception if there is a problem
|
||||||
def sync
|
def sync
|
||||||
|
|
||||||
if not @remote.to_s.empty?
|
if not @remote.to_s.empty?
|
||||||
@local.list.each do |item|
|
@local.list.each do |item|
|
||||||
update = false
|
|
||||||
|
|
||||||
# Update item
|
|
||||||
@remote.list.each do |r|
|
@remote.list.each do |r|
|
||||||
|
|
||||||
|
# Update item
|
||||||
if item.id == r.id
|
if item.id == r.id
|
||||||
if item.last_edit < r.last_edit
|
if item.last_edit < r.last_edit
|
||||||
raise item.error_msg if not item.update(name: r.name,
|
raise item.error_msg if not item.update(name: r.name,
|
||||||
group: r.group,
|
group: r.group,
|
||||||
host: r.host,
|
host: r.host,
|
||||||
protocol: r.protocol,
|
protocol: r.protocol,
|
||||||
user: r.user,
|
user: r.user,
|
||||||
password: r.password,
|
password: r.password,
|
||||||
port: r.port,
|
port: r.port,
|
||||||
comment: r.comment
|
comment: r.comment
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
update = true
|
|
||||||
r.delete
|
r.delete
|
||||||
|
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Delete an old item
|
# Remove an old item
|
||||||
if not update and item.last_edit < @config.last_update
|
if item.last_sync < @config.last_sync
|
||||||
item.delete
|
item.delete
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -116,15 +114,18 @@ module MPW
|
||||||
created: r.created,
|
created: r.created,
|
||||||
last_edit: r.last_edit
|
last_edit: r.last_edit
|
||||||
)
|
)
|
||||||
puts item.last_edit
|
|
||||||
raise @local.error_msg if not @local.add(item)
|
raise @local.error_msg if not @local.add(item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@local.list.each do |item|
|
||||||
|
item.set_last_sync
|
||||||
|
end
|
||||||
|
|
||||||
raise @mpw.error_msg if not @local.encrypt
|
raise @mpw.error_msg if not @local.encrypt
|
||||||
raise @sync.error_msg if not @sync.update(@config.file_gpg)
|
raise @sync.error_msg if not @sync.update(@config.file_gpg)
|
||||||
|
|
||||||
@config.set_last_update
|
@config.set_last_sync
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
|
|
Loading…
Reference in a new issue