mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-27 07:33:05 +00:00
rewrite sync
This commit is contained in:
parent
e1de7b7704
commit
50422e0739
3 changed files with 96 additions and 135 deletions
|
@ -298,6 +298,101 @@ class MPW
|
|||
return 0
|
||||
end
|
||||
|
||||
# Sync data with remote file
|
||||
def sync
|
||||
return if @config['sync'].nil?
|
||||
|
||||
tmp_file = "#{@wallet_file}.sync"
|
||||
last_sync = @config['last_sync'].to_i
|
||||
|
||||
case @config['sync']['type']
|
||||
when 'sftp', 'scp', 'ssh'
|
||||
require "#{APP_ROOT}/../lib/mpw/sync/ssh.rb"
|
||||
sync = SyncSSH.new(@config['sync'])
|
||||
when 'ftp'
|
||||
require 'mpw/sync/ftp'
|
||||
sync = SyncFTP.new(@config['sync'])
|
||||
else
|
||||
raise I18n.t('error.unknown_type')
|
||||
end
|
||||
|
||||
sync.connect
|
||||
sync.get(tmp_file)
|
||||
|
||||
remote = MPW.new(@key, @wallet_file, @gpg_pass)
|
||||
remote.read_data
|
||||
|
||||
File.unlink(tmp_file) if File.exist?(tmp_file)
|
||||
|
||||
if not remote.to_s.empty?
|
||||
@data.each do |item|
|
||||
update = false
|
||||
|
||||
remote.list.each do |r|
|
||||
next if item.id != r.id
|
||||
|
||||
# Update item
|
||||
if item.last_edit < r.last_edit
|
||||
item.update(name: r.name,
|
||||
group: r.group,
|
||||
host: r.host,
|
||||
protocol: r.protocol,
|
||||
user: r.user,
|
||||
port: r.port,
|
||||
comment: r.comment
|
||||
)
|
||||
set_password(item.id, r.get_password(item.id))
|
||||
end
|
||||
|
||||
r.delete
|
||||
update = true
|
||||
|
||||
break
|
||||
end
|
||||
|
||||
# Remove an old item
|
||||
if not update and item.last_sync.to_i < last_sync and item.last_edit < last_sync
|
||||
item.delete
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Add item
|
||||
remote.list.each do |r|
|
||||
next if r.last_edit <= last_sync
|
||||
|
||||
item = Item.new(id: r.id,
|
||||
name: r.name,
|
||||
group: r.group,
|
||||
host: r.host,
|
||||
protocol: r.protocol,
|
||||
user: r.user,
|
||||
port: r.port,
|
||||
comment: r.comment,
|
||||
created: r.created,
|
||||
last_edit: r.last_edit
|
||||
)
|
||||
|
||||
set_password(item.id, r.get_password(item.id))
|
||||
add(item)
|
||||
end
|
||||
|
||||
remote = nil
|
||||
|
||||
@data.each do |item|
|
||||
item.set_last_sync
|
||||
end
|
||||
|
||||
@config['sync']['last_sync'] = Time.now.to_i
|
||||
|
||||
write_data
|
||||
sync.update(@wallet_file)
|
||||
rescue Exception => e
|
||||
File.unlink(tmp_file) if File.exist?(tmp_file)
|
||||
|
||||
raise "#{I18n.t('error.sync.unknown')}\n#{e}"
|
||||
end
|
||||
|
||||
# Generate a random password
|
||||
# @args: length -> the length password
|
||||
# @rtrn: a random string
|
||||
|
|
135
lib/mpw/sync.rb
135
lib/mpw/sync.rb
|
@ -1,135 +0,0 @@
|
|||
#!/usr/bin/ruby
|
||||
# author: nishiki
|
||||
# mail: nishiki@yaegashi.fr
|
||||
# info: a simple script who manage your passwords
|
||||
|
||||
require 'rubygems'
|
||||
require 'i18n'
|
||||
require 'yaml'
|
||||
require 'tempfile'
|
||||
require 'mpw/mpw'
|
||||
require 'mpw/item'
|
||||
|
||||
module MPW
|
||||
class Sync
|
||||
|
||||
attr_accessor :error_msg
|
||||
|
||||
# Constructor
|
||||
# raise an exception if there is a bad parameter
|
||||
def initialize(config, local, password=nil)
|
||||
@error_msg = nil
|
||||
@config = config
|
||||
@local = local
|
||||
@password = password
|
||||
|
||||
raise I18n.t('error.class') if not @local.instance_of?(MPW)
|
||||
end
|
||||
|
||||
# Get the data on remote host
|
||||
# @rtrn: true if get the date, else false
|
||||
def get_remote
|
||||
case @config.sync_type
|
||||
when 'sftp', 'scp', 'ssh'
|
||||
require 'mpw/sync/ssh'
|
||||
@sync = SyncSSH.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
|
||||
when 'ftp'
|
||||
require 'mpw/sync/ftp'
|
||||
@sync = SyncFTP.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
|
||||
else
|
||||
@error_msg = I18n.t('error.unknown_type')
|
||||
return false
|
||||
end
|
||||
|
||||
if not @sync.connect
|
||||
@error_msg = @sync.error_msg
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
file_tmp = Tempfile.new('mpw-')
|
||||
raise @sync.error_msg if not @sync.get(file_tmp.path)
|
||||
|
||||
@remote = MPW.new(file_tmp.path, @config.key)
|
||||
raise @remote.error_msg if not @remote.decrypt(@password)
|
||||
|
||||
file_tmp.close(true)
|
||||
return true
|
||||
rescue Exception => e
|
||||
@error_msg = "#{I18n.t('error.sync.download')} #{e}"
|
||||
file_tmp.close(true)
|
||||
return false
|
||||
end
|
||||
|
||||
# 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
|
||||
@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
|
||||
)
|
||||
end
|
||||
|
||||
r.delete
|
||||
update = true
|
||||
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
# Remove an old item
|
||||
if not update and item.last_sync.to_i < @config.last_sync and item.last_edit < @config.last_sync
|
||||
item.delete
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Add item
|
||||
@remote.list.each do |r|
|
||||
if r.last_edit > @config.last_sync
|
||||
item = Item.new(id: r.id,
|
||||
name: r.name,
|
||||
group: r.group,
|
||||
host: r.host,
|
||||
protocol: r.protocol,
|
||||
user: r.user,
|
||||
password: r.password,
|
||||
port: r.port,
|
||||
comment: r.comment,
|
||||
created: r.created,
|
||||
last_edit: r.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_sync
|
||||
|
||||
return true
|
||||
rescue Exception => e
|
||||
@error_msg = "#{I18n.t('error.sync.unknown')} #{e}"
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
|
@ -113,6 +113,7 @@ class Cli
|
|||
end
|
||||
|
||||
@mpw.read_data
|
||||
@mpw.sync
|
||||
rescue Exception => e
|
||||
puts "#{I18n.t('display.error')} #11: #{e}".red
|
||||
exit 2
|
||||
|
|
Loading…
Reference in a new issue