diff --git a/lib/Config.rb b/lib/Config.rb index 7663809..698efe1 100644 --- a/lib/Config.rb +++ b/lib/Config.rb @@ -23,7 +23,7 @@ module MPW attr_accessor :sync_user attr_accessor :sync_pwd attr_accessor :sync_path - attr_accessor :last_update + attr_accessor :last_sync attr_accessor :dir_config # Constructor @@ -82,7 +82,7 @@ module MPW 'sync_user' => sync_user, 'sync_pwd' => sync_pwd, 'sync_path' => sync_path, - 'last_update' => 0 + 'last_sync' => 0 } } @@ -139,17 +139,17 @@ module MPW # @rtrn: true if the config file is correct def checkconfig config = YAML::load_file(@file_config) - @key = config['config']['key'] - @share_keys = config['config']['share_keys'] - @lang = config['config']['lang'] - @file_gpg = config['config']['file_gpg'] - @sync_type = config['config']['sync_type'] - @sync_host = config['config']['sync_host'] - @sync_port = config['config']['sync_port'] - @sync_user = config['config']['sync_user'] - @sync_pwd = config['config']['sync_pwd'] - @sync_path = config['config']['sync_path'] - @last_update = config['config']['last_update'].to_i + @key = config['config']['key'] + @share_keys = config['config']['share_keys'] + @lang = config['config']['lang'] + @file_gpg = config['config']['file_gpg'] + @sync_type = config['config']['sync_type'] + @sync_host = config['config']['sync_host'] + @sync_port = config['config']['sync_port'] + @sync_user = config['config']['sync_user'] + @sync_pwd = config['config']['sync_pwd'] + @sync_path = config['config']['sync_path'] + @last_sync = config['config']['last_sync'].to_i if @key.empty? or @file_gpg.empty? @error_msg = I18n.t('error.config.check') @@ -203,7 +203,7 @@ module MPW # Set the last update when there is a sync # @rtrn: true is the file has been updated - def set_last_update + def set_last_sync config = {'config' => {'key' => @key, 'share_keys' => @share_keys, 'lang' => @lang, @@ -214,7 +214,7 @@ module MPW 'sync_user' => @sync_user, 'sync_pwd' => @sync_pwd, 'sync_path' => @sync_path, - 'last_update' => Time.now.to_i + 'last_sync' => Time.now.to_i } } diff --git a/lib/Item.rb b/lib/Item.rb index ca4555e..666d5b3 100644 --- a/lib/Item.rb +++ b/lib/Item.rb @@ -20,6 +20,7 @@ module MPW attr_accessor :port attr_accessor :comment attr_accessor :last_edit + attr_accessor :last_sync attr_accessor :created # Constructor @@ -67,6 +68,11 @@ module MPW return true end + # Update last_sync + def set_last_sync + @last_sync = Time.now.to_i + end + # Delete all data # @rtrn: true def delete @@ -81,6 +87,7 @@ module MPW @comment = nil @created = nil @last_edit = nil + @last_sync = nil return true end diff --git a/lib/Sync.rb b/lib/Sync.rb index 2c6db1b..ce98596 100644 --- a/lib/Sync.rb +++ b/lib/Sync.rb @@ -68,34 +68,32 @@ module MPW # Sync remote data and local data # raise an exception if there is a problem def sync + if not @remote.to_s.empty? @local.list.each do |item| - update = false - - # Update item @remote.list.each do |r| + + # Update item if item.id == r.id if item.last_edit < r.last_edit - raise item.error_msg if not item.update(name: r.name, - group: r.group, - host: r.host, - protocol: r.protocol, - user: r.user, - password: r.password, - port: r.port, - comment: r.comment + raise item.error_msg if not item.update(name: r.name, + group: r.group, + host: r.host, + protocol: r.protocol, + user: r.user, + password: r.password, + port: r.port, + comment: r.comment ) end - update = true r.delete - break end end - - # Delete an old item - if not update and item.last_edit < @config.last_update + + # Remove an old item + if item.last_sync < @config.last_sync item.delete end end @@ -116,15 +114,18 @@ module MPW created: r.created, last_edit: r.last_edit ) - puts item.last_edit raise @local.error_msg if not @local.add(item) end end + @local.list.each do |item| + item.set_last_sync + end + raise @mpw.error_msg if not @local.encrypt raise @sync.error_msg if not @sync.update(@config.file_gpg) - @config.set_last_update + @config.set_last_sync return true rescue Exception => e